daily_playlist_updater.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import datetime
  2. import sys
  3. from yandex_music.client import Client
  4. # Help text
  5. if len(sys.argv) == 1 or len(sys.argv) > 3:
  6. print('Usage: DailyPlaylistUpdater.py token')
  7. print('token - Authentication token')
  8. quit()
  9. # Authorization
  10. elif len(sys.argv) == 2:
  11. client = Client(sys.argv[1]).init()
  12. # Current daily playlist
  13. PersonalPlaylistBlocks = client.landing(blocks=['personalplaylists']).blocks[0]
  14. DailyPlaylist = next(
  15. x.data.data for x in PersonalPlaylistBlocks.entities if x.data.data.generated_playlist_type == 'playlistOfTheDay'
  16. )
  17. # Check if we don't need to update it
  18. if DailyPlaylist.play_counter.updated:
  19. modifiedDate = datetime.datetime.strptime(DailyPlaylist.modified, '%Y-%m-%dT%H:%M:%S%z').date()
  20. if datetime.datetime.now().date() == modifiedDate:
  21. print('\x1b[6;30;43m' + 'Looks like it has been already updated today' + '\x1b[0m')
  22. quit()
  23. # Updated playlist
  24. updatedPlaylist = client.users_playlists(user_id=DailyPlaylist.uid, kind=DailyPlaylist.kind)[0]
  25. if updatedPlaylist.play_counter.updated and not DailyPlaylist.play_counter.updated:
  26. print('\x1b[6;30;42m' + 'Success!' + '\x1b[0m')
  27. else:
  28. print('\x1b[6;30;41m' + 'Something has gone wrong and nothing updated' + '\x1b[0m')
  29. # Debug information
  30. print('Before:\n modified: %s\n PlayCounter: %s' % (DailyPlaylist.modified, DailyPlaylist.play_counter))
  31. print('After:\n modified: %s\n PlayCounter: %s' % (updatedPlaylist.modified, updatedPlaylist.play_counter))