Guest User

Untitled

a guest
Oct 2nd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from gmusicapi.api import Api
  4. from getpass import getpass
  5. import sys
  6.  
  7. def init():
  8. api = Api()
  9. logged_in = False
  10. attempts = 0
  11. while not logged_in and attempts < 3:
  12. #password = getpass()
  13. logged_in = api.login("gmailaddress", "passwordhere")
  14. attempts += 1
  15.  
  16. return api
  17.  
  18.  
  19. def add_to_playlist(api, playlist_name, song_id):
  20. res = api.get_all_playlist_ids(auto=False, instant=False, user=True, always_id_lists=False)
  21.  
  22. for k,v in res['user'].items():
  23. if k.encode("cp932") == playlist_name:
  24. playlist_id = v
  25. api.add_songs_to_playlist(playlist_id, [song_id])
  26.  
  27.  
  28. def upload_song(api, filename):
  29. res = api.upload([filename])
  30. for k, v in res.items():
  31. song_id = v
  32. return v
  33.  
  34. def main():
  35. if len(sys.argv) < 3:
  36. return
  37.  
  38. api = init()
  39. if not api.is_authenticated():
  40. return
  41.  
  42. filename = sys.argv[1]
  43. playlist = sys.argv[2]
  44.  
  45. song_id = upload_song(api,filename)
  46. add_to_playlist(api, playlist, song_id)
  47. api.logout()
  48.  
  49.  
  50. if __name__ == '__main__':
  51. main()
Add Comment
Please, Sign In to add comment