spotify.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # PIP Spotipy | https://spotipy.readthedocs.io/en/2.13.0/
  2. #
  3. #
  4. # 1). Login
  5. # 2). Select playlist
  6. # 3). Iterate/Download items
  7. # 4). Use spotify metadata to ID3 tag song
  8. # 5). Songs that could not be found are listed an possible exported for later use
  9. #
  10. # This will be awesome!
  11. #
  12. from command import search, settings
  13. from helper import console, tagging
  14. import colorama as color
  15. import spotipy
  16. from spotipy.oauth2 import SpotifyOAuth
  17. from helper.settings import Settings
  18. from helper.tags.tagitem import TagItem
  19. def spotify():
  20. if Settings.SpotifyUsername.get() == '' or Settings.SpotifyClientID.get() == '' or Settings.SpotifySecret.get() == '':
  21. console.output('You need to request a developers account at spotify. You only have to do this once.')
  22. console.output('This is very easy, quick, and free to do and this doesn\'t further affect your account')
  23. console.output('Please go to:')
  24. console.output('\thttps://developer.spotify.com/dashboard/applications')
  25. console.output('And create a new application.')
  26. console.output('Fill in a name, description and enter in the Redirect URI\'s this:')
  27. console.output('\thttp://spotify-finder')
  28. console.output('Save it and make sure you find Client ID and Client Secret on your screen')
  29. console.ask_input('Are you ready to fill in your username, client ID, and secret? [y]')
  30. settings()
  31. return True
  32. console.output('Logging in to Spotify')
  33. spotify = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=Settings.SpotifyScope.get(),
  34. client_id=Settings.SpotifyClientID.get(),
  35. client_secret=Settings.SpotifySecret.get(),
  36. redirect_uri='http://spotify-finder',
  37. username=Settings.SpotifyUsername.get()))
  38. playlists = spotify.current_user_playlists()
  39. console.output('Found {0} spotify playlists'.format(len(playlists['items'])), console.DBG_INFO)
  40. playlist = console.option_picker("Pick a playlist that you want to download from",
  41. playlists['items'],
  42. quit=True,
  43. objects=[
  44. '__id__',
  45. 'x[name]',
  46. 'x[description]'
  47. ],
  48. table=[
  49. ('ID', 2),
  50. ('Name', 50),
  51. ('Description', 100)
  52. ])
  53. if playlist is None:
  54. return True
  55. else:
  56. playlist = playlists['items'][playlist]
  57. tracks = spotify.playlist_tracks(playlist['id'], fields='items(track(album(name,images(url)),artists(name),name))')
  58. console.output('Found {0} tracks in playlist {1}'.format(len(tracks['items']), playlist['name']), console.DBG_INFO)
  59. console.output('Start finding music from playlist \'{0}\''.format(playlist['name']))
  60. success = []
  61. fail = []
  62. for track in tracks['items']:
  63. track = track['track']
  64. tag = TagItem()
  65. tag.set_title(track['name'])
  66. tag.set_artist(', '.join(list(i['name'] for i in track['artists'])))
  67. tag.set_cover_url(track['album']['images'][0]['url']) # 640x640
  68. tag.set_album(track['album']['name'])
  69. tag.set_genre('') # unknown
  70. keywords = '{0} - {1}'.format(track['artists'][0]['name'], track['name'])
  71. console.output('Looking up {0} - {1}'.format(track['artists'][0]['name'], track['name']))
  72. result = search(keywords=keywords, automode=True, tags=tag)
  73. if result:
  74. success.append(tag)
  75. console.output(color.Fore.GREEN + '[ OK ] {tag.artist} - {tag.title}'.format(tag=tag))
  76. else:
  77. fail.append(tag)
  78. console.output(color.Fore.RED + '[FAIL] {tag.artist} - {tag.title}'.format(tag=tag))
  79. console.output('Sucessfully downloaded {0} songs. {1} songs failed to download'.format(len(success), len(fail)))
  80. if len(fail) > 0:
  81. seefails = console.ask_input('Do you want to see a list of the failed songs and manually search for it? [y/n]')
  82. if seefails == 'y':
  83. while True:
  84. song = console.option_picker('Pick a song to manually download it',
  85. fail,
  86. quit=True,
  87. objects=[
  88. '__id__',
  89. 'x.artist',
  90. 'x.title'
  91. ],
  92. table=[
  93. ('ID', 2),
  94. ('Artist', 50),
  95. ('Title', 100)
  96. ])
  97. if song is None:
  98. break
  99. else:
  100. keywords = '{tag.artist} - {tag.title}'.format(tag=tag)
  101. search(keywords=keywords)
  102. return True