track_list 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c32d55d4cb898bc51d11d9405f0ad05b0f50a6e85813e8efa4ef3ee31f8ece31
4
- data.tar.gz: 4ab44145070f803818b6a5c81f3150582a4fb9355b032c09aa0283437bee4196
3
+ metadata.gz: 479a619595efec9dc0388637532e62eb2d85f49d1286c5632d14c44d9d45e373
4
+ data.tar.gz: ce39a80fae12241bce71c8527c9cd83ca0c237473e2fabcd7c22ed2a3a63df3d
5
5
  SHA512:
6
- metadata.gz: 66cd3907f193b343e8547460aebf8254786a6d3f228800cabb51c8a11355dc9fdd0f07d11c6044fb289b85b8541ea207e4ad71b856e679e91ce82102ead7a791
7
- data.tar.gz: 8c2ce484fe8c0cdcd954f9b57ec447f79c6004138146fbb0f532646306196c1d8336da48d81ffc192aae023ac24c9c5483b25774b21f16c412f3c30a0cdff8cc
6
+ metadata.gz: 8d64d0707d4b68b5471574b88e7b5b4c14fdf1fa49c79c64c1246ba99cd56e848e59e5e305fc02c5ad4aea2c07f5d1f39e00c26e534ba375eb6c7f4f445f8370
7
+ data.tar.gz: 9f626e37d349ca83a565eeb0a2d60a52dab46be1fd8419b2f4c7369290be48d228807eec3f1fa668299a5082446d5cb5f30f49cae0d7673a032c07688232156f
@@ -4,9 +4,20 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.2.0] - 2019-08-09
8
+ - Added custom templating so a user can define what is output to their tracklist.
9
+ - TemplateParser class that helps with reading/writing to a config/template file.
10
+ - Commented code with RDoc.
11
+ - Cleaned up codebase more.
12
+
13
+ ## [0.1.1] - 2019-08-08
14
+ ### Fixed
15
+ - Missing ARGV argument no longer throws a fatal error but shows a helpful message instead.
16
+ - Cleaned up codebase a bit.
17
+
7
18
  ## [0.1.0] - 2019-08-08
8
19
  ### Added
9
20
  - Separation of concerns compared to old codebase. Current codebase is cleaner.
10
- -- TrackParser class that parses a single track.
11
- -- DirectoryParser class that parses a directory, using TrackParser.
12
- -- TimeConverter class that puts seconds into hour:minute:seconds for easy readability on track lengths.
21
+ - TrackParser class that parses a single track.
22
+ - DirectoryParser class that parses a directory, using TrackParser.
23
+ - TimeConverter class that puts seconds into hour:minute:seconds for easy readability on track lengths.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- track_list (0.1.1)
4
+ track_list (0.2.0)
5
5
  taglib-ruby (~> 0.7)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # TrackList
2
2
 
3
- This program aims to provide an easy way to get a tracklist for an album in your terminal, or you could use it in your own program if you wish. At the moment, there is a pre-defined format that should work for importing tracklists into MusicBrainz easily. Customizable output is a future goal of this project.
3
+ [![Gem Version](https://badge.fury.io/rb/track_list.svg)](https://badge.fury.io/rb/track_list)
4
+
5
+ This program aims to provide an easy way to get a tracklist for an album in your terminal, or you could use it in your own program if you wish. Define your own custom template to output your tracklists or use the predefined one. The predefined one is below. If you'd like to customize this output, scroll down to Configuration.
4
6
 
5
7
  ## Sample Output
6
8
  ```
@@ -43,6 +45,23 @@ Or install it yourself as:
43
45
 
44
46
  $ gem install track_list
45
47
 
48
+ ## Configuration
49
+
50
+ You can define custom output for your tracklist. On first run of the program, the program will create a .track_list.yaml file in your home directory. The default output is as follows:
51
+
52
+ `output: "%TRACK%. %TITLE% (%LENGTH%)"`
53
+
54
+ You can change this string with the following tags to format it the way you want it:
55
+ - `%TRACK%` - The track number of a given audio file.
56
+ - `%TITLE%` - The title tag of a given audio file.
57
+ - `%LENGTH%` - The length of a given audio file in `[hours:minutes:seconds]`
58
+ - `%ARTIST%` - The artist tag of a given audio file.
59
+ - `%ALBUM%` - The album tag of a given audio file.
60
+ - `%YEAR%` - The year tag of a given audio file.
61
+ - `%GENRE%` - The genre tag of a given audio file.
62
+ - `%COMMENT%` - The comment tag of a given audio file.
63
+
64
+ If a tag does not exist, you should see an empty space in the output. If you are getting some sort of error, please file an issue.
46
65
 
47
66
  ## Usage
48
67
 
@@ -2,13 +2,8 @@
2
2
 
3
3
  require "track_list"
4
4
  require 'getoptlong'
5
+ require 'track_list/template_parser'
5
6
 
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
7
  opts = GetoptLong.new(
13
8
  [ '--help', '-h', GetoptLong::NO_ARGUMENT ]
14
9
  )
@@ -33,7 +28,14 @@ if ARGV.length != 1
33
28
  end
34
29
 
35
30
  dir = TrackList::DirectoryParser.new(ARGV[0])
31
+ template_parser = TrackList::TemplateParser.new('~/.track_list.yaml')
32
+
33
+ # Let's create a default config file if one does not exist.
34
+ unless template_parser.config_exists?
35
+ template_parser.create_default_config
36
+ end
36
37
 
38
+ # Loop over each track and output to terminal.
37
39
  dir.parse.each do |track|
38
40
  puts track.parse if track.parse.is_a? String
39
41
  end
@@ -2,6 +2,10 @@ require 'track_list/version'
2
2
  require 'track_list/track_parser'
3
3
  require 'track_list/directory_parser'
4
4
  require 'track_list/time_converter'
5
+ require 'track_list/template_parser'
6
+ require 'yaml'
7
+
8
+ # This module defines the base of our program.
5
9
 
6
10
  module TrackList
7
11
  class Error < StandardError; end
@@ -1,11 +1,25 @@
1
1
  require 'track_list/track_parser'
2
2
 
3
3
  module TrackList
4
+
5
+ # This class is a helper to parse through an entire directory of tracks
6
+ # and return an array of parsed, formatted tracks.
7
+
4
8
  class DirectoryParser
9
+
10
+ # When initializing the class, pass in a directory with audio files
11
+ # to be parsed.
12
+ #
13
+ # @param [String] dir The directory to be parsed.
14
+
5
15
  def initialize(dir)
6
16
  @dir = dir
7
17
  end
8
18
 
19
+ # This method gets a list of files to be parsed.
20
+ #
21
+ # @return [Array] Files to be parsed.
22
+
9
23
  def list
10
24
  files = []
11
25
  Dir.foreach(@dir) do |file|
@@ -14,6 +28,12 @@ module TrackList
14
28
  return files
15
29
  end
16
30
 
31
+ # This method does the bulk of the work. It loops over each of the files we
32
+ # got in +self.list+ and returns an array of +TrackList::TrackParser+ objects
33
+ # to be looped over elsewhere.
34
+ #
35
+ # @return [Array] of +TrackList::TrackParser+ objects.
36
+
17
37
  def parse
18
38
  files = self.list.sort
19
39
  parsed_tracks = []
@@ -28,4 +48,4 @@ module TrackList
28
48
 
29
49
 
30
50
  end
31
- end
51
+ end
@@ -0,0 +1,41 @@
1
+ require 'yaml'
2
+
3
+ module TrackList
4
+
5
+ # A helper class to read/write a config file.
6
+
7
+ class TemplateParser
8
+
9
+ # Pass in a file path to read/write the config file.
10
+
11
+ def initialize(config_path)
12
+ @config_path = config_path
13
+ end
14
+
15
+ # Return the config file in a Ruby-readable format.
16
+
17
+ def load
18
+ return YAML.load(File.open(File.expand_path(@config_path)))
19
+ end
20
+
21
+ # Check if the config file already exists.
22
+
23
+ def config_exists?
24
+ if (File.exist?(File.expand_path(@config_path)))
25
+ return true
26
+ else
27
+ return false
28
+ end
29
+ end
30
+
31
+ # Create a default config file with some basic settings.
32
+
33
+ def create_default_config
34
+ config = { "output" => "%TRACK%. %TITLE% (%LENGTH%)" }
35
+ out_file = File.new(File.expand_path(@config_path), 'w')
36
+ out_file.puts(config.to_yaml)
37
+ out_file.close
38
+ end
39
+
40
+ end
41
+ end
@@ -1,21 +1,29 @@
1
1
  module TrackList
2
+
3
+ # A helper class to convert an audio track's length in seconds to a more readable format.
4
+
2
5
  class TimeConverter
6
+
7
+ # Pass in an integer of seconds to this class.
8
+
3
9
  def initialize(time)
4
10
  @time = time
5
11
  end
6
12
 
7
- # https://www.codethought.com/2010/01/seconds-minutes-hours-converting-time-units-in-ruby/
13
+ # This method formats the time into a readable format.
14
+ # See: https://www.codethought.com/2010/01/seconds-minutes-hours-converting-time-units-in-ruby/
15
+
8
16
  def format_time
9
- #find the seconds
17
+ # Find the seconds.
10
18
  seconds = @time % 60
11
19
 
12
- #find the minutes
20
+ # Find the minutes.
13
21
  minutes = (@time / 60) % 60
14
22
 
15
- #find the hours
23
+ # Find the hours.
16
24
  hours = (@time / 3600)
17
25
 
18
- #format the time
26
+ # Format the time.
19
27
  return hours.to_s + ":" + format("%02d", minutes.to_s) + ":" + format("%02d", seconds.to_s)
20
28
  end
21
29
  end
@@ -1,13 +1,30 @@
1
1
  require 'taglib'
2
2
  require 'track_list/time_converter'
3
+ require 'track_list/template_parser'
3
4
 
4
5
  module TrackList
6
+ ##
7
+ # The purpose of this class is to parse a single track and return a formatted
8
+ # string.
9
+
5
10
  class TrackParser
6
11
 
12
+ ##
13
+ # When initializing the class, you must pass in a file path to the +track+
14
+ # you want parsed.
15
+ #
16
+ # @param [String] track The track to be parsed.
17
+
7
18
  def initialize(track)
8
19
  @track = track
9
20
  end
10
21
 
22
+ ##
23
+ # The bulk of the work is done in this method. Calling this method returns
24
+ # a parsed track in a formatted string.
25
+ #
26
+ # @return [String] if valid audio file.
27
+
11
28
  def parse
12
29
  TagLib::FileRef.open(@track) do |fileref|
13
30
  unless fileref.null?
@@ -16,9 +33,28 @@ module TrackList
16
33
  time_conversion = TrackList::TimeConverter.new(properties.length)
17
34
  length = time_conversion.format_time
18
35
 
19
- #@tracks[tag.track] = "#{tag.track}. #{tag.title} (#{length})"
36
+ template_parser = TrackList::TemplateParser.new('~/.track_list.yaml')
37
+ template = template_parser.load
38
+
39
+ template_strings = {
40
+ '%TRACK%' => tag.track.to_s,
41
+ '%TITLE%' => tag.title,
42
+ '%LENGTH%' => length,
43
+ '%ARTIST%' => tag.artist,
44
+ '%ALBUM%' => tag.album,
45
+ '%YEAR%' => tag.year.to_s,
46
+ '%GENRE%' => tag.genre,
47
+ '%COMMENT%' => tag.comment
48
+ }
49
+
50
+ parsed_string = ''
51
+
52
+ template_strings.each do |key, val|
53
+ if template['output'].include? key
54
+ parsed_string = template['output'].gsub!(key, val)
55
+ end
56
+ end
20
57
 
21
- parsed_string = "#{tag.track}. #{tag.title} (#{length})"
22
58
  return parsed_string
23
59
  end
24
60
  end
@@ -1,3 +1,3 @@
1
1
  module TrackList
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: track_list
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Domenic Fiore
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-08 00:00:00.000000000 Z
11
+ date: 2019-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: taglib-ruby
@@ -72,6 +72,7 @@ files:
72
72
  - bin/tracklist
73
73
  - lib/track_list.rb
74
74
  - lib/track_list/directory_parser.rb
75
+ - lib/track_list/template_parser.rb
75
76
  - lib/track_list/time_converter.rb
76
77
  - lib/track_list/track_parser.rb
77
78
  - lib/track_list/version.rb