got_mp3 0.1.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 838f650beb51908cd19f1c17ea92c14fc017311a27294e1870f65ba5910110e2
4
- data.tar.gz: 79bc380538aa93ebd4ecc1e007699cad0d1f01f0082c85a7077d1ca2ae52c7dd
3
+ metadata.gz: 44c046df30aa09ee8f417c5151a511c16a55758cb324b64c9b8bb376f68b14ac
4
+ data.tar.gz: d6b18ee2753e965126a74218816625f48c36bdb09ca3b0b91b822236934f4158
5
5
  SHA512:
6
- metadata.gz: '09309170f4a66c730c1e1a7dca2c27a67a65576eac016b7f8808301d2378cf859b42971661dabb18571e1e6c46cfd865955ba5b35cf4ef6af36a214edcf85ed5'
7
- data.tar.gz: 12f09a40443a40ed6524107b98fb984afce361a721af3584324401cbde2f7e5b60a2ef85000fcfee7dad2bc052306041d212f7fd962dedb7ae24a477f030c225
6
+ metadata.gz: 3b8136cf7e4e913de207514afbe0c24295e95a18131c64aa49c57a5ce8c554cb9411ac638626c840aa3a0653f01391fd7e43f5d1aabafc0c090521a7dd58cffa
7
+ data.tar.gz: db25f65976be5cccda5a2ef7a56a194ae08577c56e916fdcd368de15cc8a5b70e563e6e5792ce7179a4f70a0df1fdc5074e6bb334636257b9f6590500c15aa79
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/got_mp3.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  # file: got_mp3.rb
4
4
 
5
+ require 'ostruct'
5
6
  require "mp3info"
6
7
 
7
8
 
@@ -21,6 +22,8 @@ class GotMP3
21
22
  def add_jpg()
22
23
 
23
24
  find_by_ext('.jpg').each do |directory, img_filename|
25
+
26
+ puts 'add_jpg to directory: ' + directory.inspect if @debug
24
27
  add_image directory, img_filename
25
28
  end
26
29
 
@@ -40,8 +43,160 @@ class GotMP3
40
43
 
41
44
  end
42
45
 
43
- #def go()
44
- #end
46
+ # copy all MP3 directories through the category file-directory stucture
47
+ #
48
+ def compile(source_directory: '', target_directory: '')
49
+
50
+ raise 'target_directory cannot be empty' if target_directory.empty?
51
+
52
+ find_by_ext('.txt').each do |directory, _ |
53
+
54
+ Dir[File.join(directory, '*.txt')].each do |txt_filename|
55
+
56
+ album = File.join(source_directory,
57
+ File.basename(txt_filename).sub(/\.txt$/,''))
58
+ library_dir = File.join(target_directory, File.basename(directory))
59
+ FileUtils.mkdir_p library_dir
60
+
61
+ if @debug then
62
+ puts 'copying from:' + album.inspect
63
+ puts 'copying to: ' + library_dir.inspect
64
+ end
65
+
66
+ puts 'copying ' + album + ' ...'
67
+ FileUtils.cp_r album, library_dir, remove_destination: true
68
+
69
+ end
70
+ end
71
+
72
+ end
73
+
74
+ def consolidate_txt(target_directory: '')
75
+
76
+ raise 'target_directory cannot be empty' if target_directory.empty?
77
+
78
+ find_by_ext('.mp3').each do |directory, _ |
79
+
80
+ puts 'write_titles() - directory: ' + directory.inspect if @debug
81
+ txt_filename = Dir[File.join(directory, '*.txt')].first
82
+
83
+ next unless txt_filename
84
+
85
+ target_file = File.basename(directory)
86
+
87
+ FileUtils.cp txt_filename, File.join(target_directory,
88
+ target_file + '.txt')
89
+
90
+ end
91
+
92
+ end
93
+
94
+ def each_mp3_file(directory='.', &blk)
95
+
96
+ puts 'each_mp3 - directory: ' + directory.inspect if @debug
97
+ found = Dir[File.join(directory, "*.mp3")].sort_by { |x| File.mtime(x) }
98
+ puts 'each_mp3 - found: ' + found.inspect if @debug
99
+
100
+ found.reverse.each.with_index do |mp3_filepath, i|
101
+
102
+ puts 'each_mp3 - mp3_filepath: ' + mp3_filepath.inspect if @debug
103
+
104
+ next unless File.exists? mp3_filepath
105
+
106
+ blk.call(mp3_filepath, i )
107
+
108
+ end
109
+
110
+ end
111
+
112
+ # Adds the album art, track title, renames the MP3 file, and adds a playlist
113
+ #
114
+ def go()
115
+
116
+ find_by_ext('.mp3').each do |directory, _ |
117
+
118
+ # find the image file
119
+ img_filename = Dir[File.join(directory, '*.jpg')].first
120
+ puts 'img_filename: ' + img_filename.inspect if @debug
121
+
122
+ # find the text file
123
+ txt_filename = Dir[File.join(directory, '*.txt')].first
124
+ next unless txt_filename
125
+
126
+ add_image_and_titles(directory, img_filename, txt_filename)
127
+
128
+ end
129
+
130
+ end
131
+
132
+ # rename 1 or more mp3 files within 1 or more file directories
133
+ #
134
+ # example usage:
135
+ # rename() {|mp3file| mp3files.sub(/Disc \d - /,'')}
136
+ # rename() {|mp3file| mp3file.sub(/Disc \d - (\d+) - /,'\1. ')}
137
+ #
138
+ def rename()
139
+
140
+ each_mp3_file do |mp3_filepath|
141
+
142
+ mp3_directory = File.dirname(mp3_filepath)
143
+ mp3_filename = File.basename(mp3_filepath)
144
+
145
+ newname = yield(mp3_filename)
146
+ File.rename(mp3_filepath, File.join(mp3_directory, newname))
147
+
148
+ end
149
+
150
+ end
151
+
152
+ def write_titles()
153
+
154
+ puts 'inside write_titles()' if @debug
155
+
156
+ find_by_ext('.mp3').each do |directory, _ |
157
+
158
+ puts 'write_titles() - directory: ' + directory.inspect if @debug
159
+ txt_filename = Dir[File.join(directory, '*.txt')].first
160
+
161
+ next if txt_filename
162
+
163
+ tracks = []
164
+
165
+ each_mp3_track(directory) do |mp3, trackno, mp3_filepath|
166
+
167
+ tracks << OpenStruct.new({
168
+ title: mp3.tag.title,
169
+ artist: mp3.tag.artist,
170
+ album: mp3.tag.album,
171
+ album_artist: mp3.tag2['TPE2'],
172
+ disc: mp3.tag2['TPOS'],
173
+ tracknum: mp3.tag.tracknum,
174
+ filename: File.basename(mp3_filepath)
175
+ })
176
+
177
+ end
178
+
179
+ heading = tracks[0].album_artist + ' - ' + tracks[0].album
180
+ s = "# %s\n\n" % [heading]
181
+ h = tracks.group_by(&:disc)
182
+
183
+ body = if h.length == 1 then
184
+
185
+ list(tracks)
186
+
187
+ else
188
+
189
+ "\n" + h.map do |disc, tracks2|
190
+ ("## Disc %d\n\n" % disc) + list(tracks2) + "\n\n"
191
+ end.join("\n")
192
+
193
+ end
194
+
195
+ File.write File.join(directory, heading + '.txt'), s + body
196
+
197
+ end
198
+
199
+ end
45
200
 
46
201
  private
47
202
 
@@ -49,10 +204,12 @@ class GotMP3
49
204
  #
50
205
  def add_image(directory, img_filename)
51
206
 
207
+ puts 'inside add_image - directory: ' + directory.inspect if @debug
208
+ puts 'img_filename: ' + img_filename.inspect if @debug
52
209
  image_file = File.new(File.join(directory, img_filename),'rb')
53
210
  img = image_file.read
54
211
 
55
- each_mp3(directory) do |mp3, _, _|
212
+ each_mp3_track(directory) do |mp3, _, _|
56
213
 
57
214
  mp3.tag2.remove_pictures
58
215
  mp3.tag2.add_picture img
@@ -66,40 +223,49 @@ class GotMP3
66
223
  txt_file = File.join(directory, txt_filename)
67
224
  track_titles = File.read(txt_file).lines[1..-1].map(&:strip)
68
225
 
69
- each_mp3(directory) do |mp3, trackno, _|
226
+ each_mp3_track(directory) do |mp3, trackno, _|
70
227
 
71
228
  mp3.tag.title = track_titles[trackno-1]
72
229
 
73
230
  end
74
231
  end
75
232
 
76
- def add_image_and_titles(directory, img_filename, txt_filename)
233
+ def add_image_and_titles(directory, img_file, txt_file)
77
234
 
78
- image_file = File.new(File.join(directory, img_filename),'rb')
79
- img = image_file.read
235
+ if img_file then
236
+ img = File.new(img_file,'rb').read
237
+ end
80
238
 
81
- txt_file = File.join(directory, txt_filename)
82
239
  track_titles = File.read(txt_file).lines[1..-1].map(&:strip)
83
240
 
241
+ titles_mp3 = track_titles.map do |title|
242
+ title[/^[^\/]+/].gsub(/:/,'_').rstrip + '.mp3'
243
+ end
244
+
84
245
  found = Dir[File.join(directory, "*.mp3")].sort_by { |x| File.mtime(x) }
85
246
  found.each.with_index do |mp3_filepath, i|
86
247
 
87
248
  Mp3Info.open(mp3_filepath) do |mp3|
88
- mp3.tag2.remove_pictures
89
- mp3.tag2.add_picture img
90
- mp3.tag.title = track_titles[trackno-1]
249
+
250
+ if img_file then
251
+ mp3.tag2.remove_pictures
252
+ mp3.tag2.add_picture img
253
+ end
254
+
255
+ mp3.tag.title = track_titles[i]
91
256
  end
92
257
 
93
- File.rename(mp3_filepath, File.join(directory,
94
- track_titles[i][/^[^\/]+/].gsub(/\W/,'').rstrip + '.mp3'))
258
+ File.rename(mp3_filepath, File.join(directory, titles_mp3[i]))
259
+
95
260
  end
96
261
 
262
+ File.write File.join(directory, 'playlist.m3u'), titles_mp3.join("\n")
263
+
97
264
  end
98
265
 
99
- def each_mp3(directory, &blk)
266
+ def each_mp3_track(directory, &blk)
100
267
 
101
- found = Dir[File.join(directory, "*.mp3")].sort_by { |x| File.mtime(x) }
102
- found.each.with_index do |mp3_filepath, i|
268
+ each_mp3_file(directory) do |mp3_filepath, i|
103
269
 
104
270
  Mp3Info.open(mp3_filepath) {|mp3| blk.call(mp3, i+1, mp3_filepath) }
105
271
 
@@ -109,6 +275,7 @@ class GotMP3
109
275
 
110
276
  def find_by_ext(extension)
111
277
 
278
+ puts 'find_by_ext() - @dir' + @dir.inspect if @debug
112
279
  a = Dir[File.join(@dir, "**", "*" + extension)]
113
280
  puts 'a: ' + a.inspect if @debug
114
281
 
@@ -120,4 +287,56 @@ class GotMP3
120
287
  end
121
288
  end
122
289
 
290
+ def list(tracks)
291
+
292
+ a = if tracks.map(&:artist).uniq.length < 2 then
293
+ tracks.map {|x| "%02d. %s" % [x.tracknum, x.title] }
294
+ else
295
+ tracks.map {|x| "%02d. %s - %s" % [x.tracknum, x.title, x.artist] }
296
+ end
297
+
298
+ a.join("\n")
299
+
300
+ end
301
+
302
+ end
303
+
304
+ class Titles
305
+
306
+ def initialize(filename, target_directory: 'titles')
307
+ @titles = File.read filename
308
+ @target_directory = target_directory
309
+ end
310
+
311
+ def titleize()
312
+ @titles.gsub(/[\w']+/) {|x| x[0].upcase + x[1..-1]}
313
+ end
314
+
315
+ def titleize!()
316
+ @titles.gsub!(/[\w']+/) {|x| x[0].upcase + x[1..-1]}
317
+ end
318
+
319
+ def split(target_directory: @target_directory)
320
+
321
+ FileUtils.mkdir_p @target_directory
322
+ a = @titles.strip.split(/(?=^#)/)
323
+
324
+ a.each do |x|
325
+
326
+ filename = x.lines.first.chomp[/(?<=# cd ).*/i].strip + '.txt'
327
+ puts 'processing file ' + filename.inspect
328
+ heading = x.lstrip.lines.first
329
+
330
+ tracks = x.strip.lines[1..-1].map.with_index do |line, i|
331
+ "%02d. %s" % [i+1, line]
332
+ end
333
+
334
+ File.write File.join(target_directory, filename), heading + tracks.join
335
+
336
+ end
337
+
338
+ puts 'split done'
339
+
340
+ end
341
+
123
342
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: got_mp3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -35,7 +35,7 @@ cert_chain:
35
35
  RFrZsrDO1Cwk4v03qCUvuZ8H/nCojYYJGMrJE1JhBBJNJXCAULivh/zlOYcDhG69
36
36
  uPtZOD7YKrG45OftX8aNxlNv
37
37
  -----END CERTIFICATE-----
38
- date: 2021-11-25 00:00:00.000000000 Z
38
+ date: 2021-11-26 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: ruby-mp3info
@@ -87,6 +87,6 @@ rubyforge_project:
87
87
  rubygems_version: 2.7.10
88
88
  signing_key:
89
89
  specification_version: 4
90
- summary: A ruby-mp3info wrapper to make it convenient to update the metadata of multiple
91
- MP3 files at once.
90
+ summary: A ruby-mp3info wrapper to make it convenient to update the album art and
91
+ track titles of multiple MP3 files in a batch process.
92
92
  test_files: []
metadata.gz.sig CHANGED
@@ -1 +1,4 @@
1
- �٠�">��ievjj�O̜ld���_���,�)aϵ�8�
1
+ |?:��/�����uư;����N*f�%Z��.�8=��5V�ȥ̸1����#d�h6��3@��h��<�F���� &I?���S6��Y{O�y~����J1pT-�`
2
+ ������gk���!���(^;V8�[k��2��5V~W�+G�>mFO��38�)w�}�Pyb���\�R�#��biR۫��.G6?>���"���
3
+ !  SfP�xўd$�$T�d
4
+ ֗\�A��R?%�{���]q7٨���h�ݲA�|*p�8�7IM�$M�uҮ̧.�.�M�t�;7i��"-��[�����