From 57b3fa6cbeabb1acb92d80899d238dff730e4e72 Mon Sep 17 00:00:00 2001 From: CatInAPoke Date: Tue, 7 Jul 2020 17:26:47 +0300 Subject: [PATCH] Added example: daily playlist updater --- examples/DailyPlaylistUpdater.py | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 examples/DailyPlaylistUpdater.py diff --git a/examples/DailyPlaylistUpdater.py b/examples/DailyPlaylistUpdater.py new file mode 100644 index 0000000..02e95a8 --- /dev/null +++ b/examples/DailyPlaylistUpdater.py @@ -0,0 +1,40 @@ +import sys +import datetime +from yandex_music.client import Client + +# Help text +if(len(sys.argv) == 1 or len(sys.argv) > 3): + print('Usage: DailyPlaylistUpdater.py token') + print('token - Authentication token'); + print('\nUsage: DailyPlaylistUpdater.py username password') + print('username - username in format \'example@yandex.ru\''); + print('password - your password'); + quit() +# Authorization +elif(len(sys.argv) == 2): + client = Client.fromToken(sys.argv[1]); +elif(len(sys.argv) == 3): + client = Client .fromCredentials(sys.argv[1], sys.argv[2]) + +# Current daily playlist +PersonalPlaylistBlocks = client.landing(blocks=['personalplaylists']).blocks[0]; +DailyPlaylist = next(x.data.data for x in PersonalPlaylistBlocks.entities if x.data.data.generated_playlist_type == 'playlistOfTheDay') + +# Check if we don't need to update it +if(DailyPlaylist.play_counter.updated): + modifiedDate = datetime.datetime.strptime(DailyPlaylist.modified, "%Y-%m-%dT%H:%M:%S%z").date() + if (datetime.datetime.now().date() == modifiedDate): + print('\x1b[6;30;43m' + 'Looks like it has been already updated today' + '\x1b[0m') + quit() + +# Updated playlist +updatedPlaylist = client.users_playlists(user_id=DailyPlaylist.uid, kind=DailyPlaylist.kind)[0] + +if(updatedPlaylist.play_counter.updated and not DailyPlaylist.play_counter.updated): + print('\x1b[6;30;42m' + 'Success!' + '\x1b[0m') +else: + print('\x1b[6;30;41m' + 'Something has gone wrong and nothing updated' + '\x1b[0m') + + # Debug information + print('Before:\n modified: %s\n PlayCounter: %s' % (DailyPlaylist.modified, DailyPlaylist.play_counter)) + print('After:\n modified: %s\n PlayCounter: %s' % (updatedPlaylist.modified, updatedPlaylist.play_counter)) \ No newline at end of file