[READ-ONLY] Mirror of https://github.com/probablykasper/gpm-to-itunes. Imports Google Play Music library to iTunes
cli google-play-music itunes
0

Configure Feed

Select the types of activity you want to include in your feed.

user_files folder, removed options object

+40 -27
+39 -26
main.py
··· 1 1 import gmusicapi 2 2 import json, datetime, pprint, sys, os, subprocess 3 3 from pathlib import Path 4 - import appscript # pip3 install distribute; sudo distribute 4 + import appscript 5 5 from tqdm import tqdm 6 6 from mp3_tagger import MP3File, VERSION_1, VERSION_2, VERSION_BOTH 7 7 pprint = pprint.PrettyPrinter(indent=4).pprint 8 8 9 - from options import options 10 9 if len(sys.argv) > 1: 11 - options['action'] = sys.argv[1] 12 - if options['action'] == 'match_files': 10 + action = sys.argv[1] 11 + else: 12 + sys.exit('Error: Must have an "action" argument.') 13 + 14 + if action == 'match_files': 13 15 if len(sys.argv) > 2: 14 - options['songs_path'] = sys.argv[2] 16 + songs_path = sys.argv[2] 17 + else: 18 + sys.exit('Error: Must have a "songs_path" argument.') 19 + elif action == 'add_to_itunes': 20 + if len(sys.argv) > 3: 21 + sudo_password = sys.argv[2] 22 + itunes_media_folder = sys.argv[3] 23 + else: 24 + sys.exit('Error: Must have "sudo_password" and "itunes_media_folder" argument.') 15 25 16 - def load_library(path = "library.json"): 26 + def load_library(path = "user_files/library.json"): 17 27 print('Loading GPM library...') 18 28 gpm_library = json.loads(open(path).read()) 19 29 print(" Track count:", len(gpm_library["tracks"])) 20 30 print(" Playlist count:", len(gpm_library["playlists"])) 21 31 return gpm_library 22 - def save_library(content, path = "library.json"): 32 + def save_library(content, path = "user_files/library.json"): 23 33 file = open(path, "w+") 24 34 file.write(json.dumps(content, indent=2)) 25 35 file.close() ··· 91 101 def match_files(gpm_library): 92 102 # scan how many files there are 93 103 fileCount = 0 94 - for subdir, dirs, files in os.walk(options['songs_path']): 104 + for subdir, dirs, files in os.walk(songs_path): 95 105 fileCount += len(files) 96 106 with tqdm(total=fileCount) as progressbar: 97 - for subdir, dirs, files in os.walk(options['songs_path']): 107 + for subdir, dirs, files in os.walk(songs_path): 98 108 for file in files: 99 109 file_path = os.path.join(subdir, file) 100 110 filename, extension = os.path.splitext(file_path) ··· 136 146 137 147 iTunes = appscript.app('iTunes') 138 148 itunes_library = iTunes.library_playlists['Library'] 139 - md_map = json.loads(open('md_map.json').read()) 149 + if Path('user_files/md_map.json').is_file(): 150 + md_map = json.loads(open('user_files/md_map.json').read()) 151 + else: 152 + md_map = {} 140 153 141 154 # find unmatched itunes tracks 142 155 itunes_tracks = itunes_library.tracks.get() ··· 215 228 original_usingnetworktime = 'off' if 'Network Time: Off' in usingnetworktime_output else 'on' 216 229 subprocess.call( 217 230 ('echo %s | sudo -S systemsetup -setusingnetworktime %s' 218 - % (options['sudo_password'], 'off')), shell=True, stdout=subprocess.PIPE 231 + % (sudo_password, 'off')), shell=True, stdout=subprocess.PIPE 219 232 ) 220 233 print() 221 234 ··· 258 271 time = date_added.strftime('%H:%M:%S') # hh:mm:ss 259 272 subprocess.call( 260 273 ('echo "%s" | sudo -S systemsetup -setdate %s -settime %s' 261 - % (options['sudo_password'], date, time)), shell=True, stdout=FNULL, stderr=subprocess.STDOUT 274 + % (sudo_password, date, time)), shell=True, stdout=FNULL, stderr=subprocess.STDOUT 262 275 ) 263 276 264 277 # add to itunes and update metadata ··· 267 280 itunes_track = gpm_track['itunes_track'] 268 281 269 282 itunes_track_location = getattr(itunes_track, 'location').get() 270 - if itunes_track_location == appscript.k.missing_value: 283 + if itunes_track_location == appscript.k.missing_value and itunes_media_folder: 271 284 import shutil, ntpath 272 285 filename = ntpath.basename(track_md['file_path']) 273 286 new_itunes_track_location = os.path.join(options['itunes_media_folder'], filename) ··· 321 334 # set network time to what it was before 322 335 subprocess.call( 323 336 ('echo %s | sudo -S systemsetup -setusingnetworktime %s' 324 - % (options['sudo_password'], original_usingnetworktime)).split() 337 + % (sudo_password, original_usingnetworktime)).split() 325 338 ) 326 339 327 340 # add playlists to itunes ··· 346 359 if only_scan == False: 347 360 print('All done. Now make sure your clock is correct.') 348 361 349 - if options['action'] == 'login': 362 + if action == 'login': 350 363 351 364 init_gpm_api(new_login=True) 352 365 353 - elif options['action'] == 'fetch': 366 + elif action == 'fetch': 354 367 355 368 gpm_library = download_library() 356 - save_library(gpm_library, 'library_downloaded.json') 369 + save_library(gpm_library, 'user_files/library_downloaded.json') 357 370 print(" Track count:", len(gpm_library["tracks"])) 358 371 print(" Playlist count:", len(gpm_library["playlists"])) 359 372 360 373 gpm_library = restructure_library(gpm_library) 361 - save_library(gpm_library, 'library_restructured.json') 374 + save_library(gpm_library, 'user_files/library_restructured.json') 362 375 363 - elif options['action'] == 'match_files': 376 + elif action == 'match_files': 364 377 365 - gpm_library = load_library('library_restructured.json') 378 + gpm_library = load_library('user_files/library_restructured.json') 366 379 print('Matching GPM library with song files...') 367 380 gpm_library = match_files(gpm_library) 368 - save_library(gpm_library, 'library_matched_files.json') 381 + save_library(gpm_library, 'user_files/library_matched_files.json') 369 382 370 - elif options['action'] == 'scan_itunes': 383 + elif action == 'scan_itunes': 371 384 372 - gpm_library = load_library('library_matched_files.json') 385 + gpm_library = load_library('user_files/library_matched_files.json') 373 386 add_to_itunes(gpm_library, only_scan=True) 374 387 375 - elif options['action'] == 'add_to_itunes': 388 + elif action == 'add_to_itunes': 376 389 377 - gpm_library = load_library('library_matched_files.json') 390 + gpm_library = load_library('user_files/library_matched_files.json') 378 391 add_to_itunes(gpm_library, only_scan=False) 379 392 380 393 else: 381 394 382 - sys.exit('Unknown action "'+options['action']+'"') 395 + sys.exit('Unknown action "'+action+'"') 383 396 384 397 # loop through audio files 385 398 # -get the audio file's artist/title
+1 -1
md_map.json user_files/md_map.json
··· 1 - {} 1 + {}