forked from thebigmunch/gmusicapi-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmwrapper.py
484 lines (343 loc) · 13.9 KB
/
gmwrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
from __future__ import print_function, unicode_literals
import getpass
import logging
import os
import re
import shutil
import sys
import tempfile
import mutagen
from gmusicapi import CallFailure
from gmusicapi.clients import Mobileclient, Musicmanager, OAUTH_FILEPATH
from gmusicapi.utils import utils as gm_utils
from utils import safe_print
SUPPORTED_FORMATS = ('.mp3', '.flac', '.ogg', '.m4a')
CHARACTER_REPLACEMENTS = {
'\\': '-', '/': ',', ':': '-', '*': 'x', '<': '[',
'>': ']', '|': '!', '?': '', '"': "''"
}
TEMPLATE_PATTERNS = {
'%artist%': 'artist', '%title%': 'title', '%track%': 'tracknumber',
'%track2%': 'tracknumber', '%album%': 'album', '%date%': 'date',
'%genre%': 'genre', '%albumartist%': 'performer', '%disc%': 'discnumber'
}
def _mutagen_fields_to_single_value(file):
"""Replace field list values in mutagen tags with the first list value."""
return {k: v[0] for k, v in mutagen.File(file, easy=True).iteritems()}
def compare_song_collections(src_songs, dest_songs):
"""Compare two song collections.
Returns a list of songs from source missing in destination.
"""
missing_songs = []
src_songs_keyed = {}
dest_songs_keyed = {}
for src_song in src_songs:
src_key = create_song_key(src_song)
src_songs_keyed[src_key] = src_song
for dest_song in dest_songs:
dest_key = create_song_key(dest_song)
dest_songs_keyed[dest_key] = dest_song
for src_key, src_song in src_songs_keyed.iteritems():
if src_key not in dest_songs_keyed:
missing_songs.append(src_song)
return missing_songs
def create_song_key(song):
"""Create dict key for song based on metadata."""
metadata = []
song = song if isinstance(song, dict) else _mutagen_fields_to_single_value(song)
# Replace track numbers with 0 if no tag exists.
if song.get('id'):
if not song.get('track_number'):
song['track_number'] = '0'
else:
if not song.get('tracknumber'):
song['tracknumber'] = '0'
for field in filter_fields(song):
metadata.append(normalize_metadata(song[field]))
key = '|'.join(metadata)
return key
def exclude_path(path, exclude_patterns):
"""Exclude file paths based on regex patterns."""
if exclude_patterns and re.search(exclude_patterns, path):
return True
else:
return False
def filter_fields(song):
"""Filter missing artist, album, title, or track fields to improve match accuracy."""
# Need both tracknumber (mutagen) and track_number (Google Music) here.
return [field for field in ['artist', 'album', 'title', 'tracknumber', 'track_number'] if field in song and song[field]]
# Shamelessly taken from gmusicapi itself.
def _get_valid_filter_fields():
shared_fields = ['artist', 'title', 'album']
diff_fields = {'albumartist': 'album_artist'}
return dict(dict((shared, shared) for shared in shared_fields).items() + diff_fields.items())
def _match_filters(song, filters, filter_all):
"""Match a song metadata dict against a set of metadata filters."""
if filter_all:
if not all(field in song and re.search(value, song[field], re.I) for field, value in filters):
return False
else:
if not any(field in song and re.search(value, song[field], re.I) for field, value in filters):
return False
return True
def match_filters_google(songs, filters, filter_all):
"""Match a Google Music song against a set of metadata filters."""
match_songs = []
filter_songs = []
if filters:
split_filters = []
for filter in filters:
filter_field, filter_value = filter.split(':', 1)
valid_fields = _get_valid_filter_fields().items()
for mutagen_field, google_field in valid_fields:
if filter_field == mutagen_field or filter_field == google_field:
split_filters.append((google_field, filter_value))
for song in songs:
if _match_filters(song, split_filters, filter_all):
match_songs.append(song)
else:
filter_songs.append(song)
else:
match_songs += songs
return match_songs, filter_songs
def match_filters_local(files, filters, filter_all):
"""Match a local file against a set of metadata filters."""
match_songs = []
filter_songs = []
if filters:
split_filters = []
for filter in filters:
filter_field, filter_value = filter.split(':', 1)
valid_fields = _get_valid_filter_fields().items()
for mutagen_field, google_field in valid_fields:
if filter_field == mutagen_field or filter_field == google_field:
split_filters.append((mutagen_field, filter_value))
for file in files:
song = _mutagen_fields_to_single_value(file)
if _match_filters(song, split_filters, filter_all):
match_songs.append(file)
else:
filter_songs.append(file)
else:
match_songs += files
return match_songs, filter_songs
def normalize_metadata(metadata):
"""Normalize metadata to improve match accuracy."""
metadata = unicode(metadata) # Convert metadata to unicode.
metadata = metadata.lower() # Convert to lower case.
metadata = re.sub('\/\s*\d+', '', metadata) # Remove "/<totaltracks>" from track number.
metadata = re.sub('^0+([0-9]+)', r'\1', metadata) # Remove leading zero(s) from track number.
metadata = re.sub('^\d+\.+', '', metadata) # Remove dots from track number.
metadata = re.sub('[^\w\s]', '', metadata) # Remove any non-words.
metadata = re.sub('\s+', ' ', metadata) # Reduce multiple spaces to a single space.
metadata = re.sub('^\s+', '', metadata) # Remove leading space.
metadata = re.sub('\s+$', '', metadata) # Remove trailing space.
metadata = re.sub('^the\s+', '', metadata, re.I) # Remove leading "the".
return metadata
def template_to_file_name(template, suggested_filename, metadata):
"""Create directory structure and file name based on metadata template."""
drive, path = os.path.splitdrive(template)
parts = []
while True:
newpath, tail = os.path.split(path)
if newpath == path:
break
parts.append(tail)
path = newpath
parts.reverse()
for i, part in enumerate(parts):
if "%suggested%" in part:
parts[i] = parts[i].replace("%suggested%", suggested_filename.replace('.mp3', ''))
for key in TEMPLATE_PATTERNS:
if key in part and TEMPLATE_PATTERNS[key] in metadata:
if key == '%track2%':
metadata['tracknumber'] = metadata['tracknumber'][0].zfill(2)
try:
metadata.save()
except:
pass
parts[i] = parts[i].replace(key, metadata[TEMPLATE_PATTERNS[key]][0])
for char in CHARACTER_REPLACEMENTS:
if char in parts[i]:
parts[i] = parts[i].replace(char, CHARACTER_REPLACEMENTS[char])
if drive:
filename = os.path.join(drive, os.sep, *parts) + '.mp3'
else:
if os.path.isabs(template):
filename = os.path.join(os.sep, *parts) + '.mp3'
else:
filename = os.path.join(*parts) + '.mp3'
dirname, __ = os.path.split(filename)
if dirname:
try:
os.makedirs(dirname)
except OSError:
if not os.path.isdir(dirname):
raise
return filename
class _Base(object):
@gm_utils.accept_singleton(basestring)
def get_local_songs(self, paths, formats=SUPPORTED_FORMATS, exclude_patterns=None, filters=None, filter_all=False):
"""Load songs from local file paths."""
self.print_("Loading local songs...")
include_songs = []
exclude_songs = []
for path in paths:
if not isinstance(path, unicode):
path = path.decode('utf8')
if os.path.isdir(path):
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
if filename.lower().endswith(formats):
filepath = os.path.join(dirpath, filename)
if exclude_path(filepath, exclude_patterns):
exclude_songs.append(filepath)
else:
include_songs.append(filepath)
elif os.path.isfile(path) and path.lower().endswith(formats):
if exclude_path(path, exclude_patterns):
exclude_songs.append(path)
else:
include_songs.append(path)
local_songs, filter_songs = match_filters_local(include_songs, filters, filter_all)
self.print_("Excluded {0} local songs.".format(len(exclude_songs)))
self.print_("Filtered {0} local songs.".format(len(filter_songs)))
self.print_("Loaded {0} local songs.\n".format(len(local_songs)))
return local_songs, exclude_songs
class MobileClientWrapper(_Base):
def __init__(self, log=False, quiet=False):
self.api = Mobileclient(debug_logging=log)
self.api.logger.addHandler(logging.NullHandler())
self.print_ = safe_print if not quiet else lambda *args, **kwargs: None
def login(self, *args):
"""Authenticate the gmusicapi Mobileclient instance."""
if len(args) == 2:
username, password = args[0], args[1]
elif len(args) == 1:
username, password = args[0], None
else:
username, password = None, None
if not self.api.login(username, password):
if not username:
username = raw_input("Enter your Google username or email address: ")
if not password:
password = getpass.getpass(b"Enter your Google Music password: ")
self.api.login(username, password)
if not self.api.is_authenticated():
self.print_("Sorry, login failed.")
return False
self.print_("Successfully logged in.\n")
return True
def logout(self):
"""Log out the gmusicapi Mobileclient instance."""
self.api.logout()
def get_google_songs(self, filters=None, filter_all=False):
"""Load song list from Google Music library."""
self.print_("Loading Google Music songs...")
google_songs = []
filter_songs = []
songs = self.api.get_all_songs()
google_songs, filter_songs = match_filters_google(songs, filters, filter_all)
self.print_("Filtered {0} Google Music songs".format(len(filter_songs)))
self.print_("Loaded {0} Google Music songs\n".format(len(google_songs)))
return google_songs
class MusicManagerWrapper(_Base):
def __init__(self, log=False, quiet=False):
self.api = Musicmanager(debug_logging=log)
self.api.logger.addHandler(logging.NullHandler())
self.print_ = safe_print if not quiet else lambda *args, **kwargs: None
def login(self, oauth_file="oauth", uploader_id=None):
"""Authenticate the gmusicapi Musicmanager instance."""
oauth_cred = os.path.join(os.path.dirname(OAUTH_FILEPATH), oauth_file + '.cred')
try:
if not self.api.login(oauth_credentials=oauth_cred, uploader_id=uploader_id):
try:
self.api.perform_oauth(storage_filepath=oauth_cred)
except:
self.print_("\nUnable to login with specified oauth code.")
self.api.login(oauth_credentials=oauth_cred, uploader_id=uploader_id)
except (OSError, ValueError) as e:
self.print_(e.args[0])
return False
if not self.api.is_authenticated():
self.print_("Sorry, login failed.")
return False
self.print_("Successfully logged in.\n")
return True
def logout(self, revoke_oauth=False):
"""Log out the gmusicapi Musicmanager instance."""
self.api.logout(revoke_oauth=revoke_oauth)
def get_google_songs(self, filters=None, filter_all=False):
"""Load song list from Google Music library."""
self.print_("Loading Google Music songs...")
google_songs = []
filter_songs = []
songs = self.api.get_uploaded_songs()
google_songs, filter_songs = match_filters_google(songs, filters, filter_all)
self.print_("Filtered {0} Google Music songs".format(len(filter_songs)))
self.print_("Loaded {0} Google Music songs\n".format(len(google_songs)))
return google_songs
@gm_utils.accept_singleton(basestring)
def download(self, songs, template):
"""Download songs from Google Music."""
songnum = 0
total = len(songs)
errors = {}
pad = len(str(total))
for song in songs:
songnum += 1
try:
self.print_("Downloading {0} by {1}".format(song['title'], song['artist']), end="\r")
sys.stdout.flush()
suggested_filename, audio = self.api.download_song(song['id'])
except CallFailure as e:
self.print_("({num:>{pad}}/{total}) Failed to download {file} | {error}".format(num=songnum, total=total, file=suggested_filename, error=e, pad=pad))
errors[suggested_filename] = e
else:
with tempfile.NamedTemporaryFile(delete=False) as temp:
temp.write(audio)
metadata = mutagen.File(temp.name, easy=True)
if template != os.getcwd():
filename = template_to_file_name(template, suggested_filename, metadata)
else:
filename = suggested_filename
shutil.move(temp.name, filename)
self.print_("({num:>{pad}}/{total}) Successfully downloaded {file}".format(num=songnum, total=total, file=filename, pad=pad))
if errors:
self.print_("\n\nThe following errors occurred:\n")
for filename, e in errors.iteritems():
self.print_("{file} | {error}".format(file=filename, error=e))
self.print_("\nThese files may need to be synced again.\n")
@gm_utils.accept_singleton(basestring)
def upload(self, files, enable_matching=False):
"""Upload files to Google Music."""
filenum = 0
total = len(files)
errors = {}
pad = len(str(total))
for file in files:
filenum += 1
try:
self.print_("Uploading {0}".format(file), end="\r")
sys.stdout.flush()
uploaded, matched, not_uploaded = self.api.upload(file, transcode_quality="320k", enable_matching=enable_matching)
except CallFailure as e:
self.print_("({num:>{pad}}/{total}) Failed to upload {file} | {error}".format(num=filenum, total=total, file=file, error=e, pad=pad))
errors[file] = e
else:
if uploaded:
self.print_("({num:>{pad}}/{total}) Successfully uploaded {file}".format(num=filenum, total=total, file=file, pad=pad))
elif matched:
self.print_("({num:>{pad}}/{total}) Successfully scanned and matched {file}".format(num=filenum, total=total, file=file, pad=pad))
else:
check_strings = ["ALREADY_EXISTS", "this song is already uploaded"]
if any(check_string in not_uploaded[file] for check_string in check_strings):
response = "ALREADY EXISTS"
else:
response = not_uploaded[file]
self.print_("({num:>{pad}}/{total}) Failed to upload {file} | {response}".format(num=filenum, total=total, file=file, response=response, pad=pad))
if errors:
self.print_("\n\nThe following errors occurred:\n")
for file, e in errors.iteritems():
self.print_("{file} | {error}".format(file=file, error=e))
self.print_("\nThese files may need to be synced again.\n")