From 5a4265247b57a23ae34f916d6845d39819363c30 Mon Sep 17 00:00:00 2001 From: Brandon4466 Date: Wed, 14 Dec 2022 15:21:15 -0800 Subject: [PATCH] working --- .cache-thebrandon45 | 1 + credentials.json | 1 + json_file1.txt | 109 ++++++++++++ json_file2.txt | 184 ++++++++++++++++++++ json_file3.txt | 404 +++++++++++++++++++++++++++++++++++++++++++ json_file4.txt | 402 ++++++++++++++++++++++++++++++++++++++++++ json_file5.txt | 0 json_file6.txt | 0 librespot_example.py | 121 +++++++++++++ program.py | 253 +++++++++++++++++++++++++++ 10 files changed, 1475 insertions(+) create mode 100644 .cache-thebrandon45 create mode 100644 credentials.json create mode 100644 json_file1.txt create mode 100644 json_file2.txt create mode 100644 json_file3.txt create mode 100644 json_file4.txt create mode 100644 json_file5.txt create mode 100644 json_file6.txt create mode 100644 librespot_example.py create mode 100644 program.py diff --git a/.cache-thebrandon45 b/.cache-thebrandon45 new file mode 100644 index 0000000..5e62790 --- /dev/null +++ b/.cache-thebrandon45 @@ -0,0 +1 @@ +{"access_token": "BQDT_WherHrUXbbhIgBKI7Fo7emhUb-CW2hhY2eGzJjIS01ryBT2r71AxXKkVQcX1aa7-98pw_0yMOvIjWO8w6WwJuZVTRM4y0_-NvZLuTTCCF7fr7pme0EYQ8VdHdJtxESZO81ZGatYvpt7MQ59TsTQSE9aGhIKfnTprUtWJR43XfYJn6QWcT2bfNTIkQ", "token_type": "Bearer", "expires_in": 3600, "scope": "user-modify-playback-state user-read-playback-state", "expires_at": 1671062858, "refresh_token": "AQAQRkRkmOxwR34lsJu_mpG4wxEFiNuJ1FIdmHYN5j0dClEEqMRqPPFX8jM3cQTQkRpeXXyi3xPQ8YImc7Fc-PH1CKTvyARy9VkMKXIes4-l6ijO9fVMyCNwbI0IGvpfrqE"} \ No newline at end of file diff --git a/credentials.json b/credentials.json new file mode 100644 index 0000000..e16ee35 --- /dev/null +++ b/credentials.json @@ -0,0 +1 @@ +{"username": "thebrandon45", "credentials": "QVFESGtlN01Mb3VURGRWT1YyQy0xRHh4LXFfSmczdkdzbTNvbVRIbVNVSDVXMVg3ZjZWMmxOTWdxYnpTdG93RmpwR2tjYkhKYVBsc3h3RTVDWVVoVXh6Q2NuTlR0dHVRYXlhai1WbUQ0WnlDTEdtbDg0cVVCQUxXMEVfcno2bWpDOHdieUdneXM4N0tnQQ==", "type": "AUTHENTICATION_STORED_SPOTIFY_CREDENTIALS"} \ No newline at end of file diff --git a/json_file1.txt b/json_file1.txt new file mode 100644 index 0000000..6ce0988 --- /dev/null +++ b/json_file1.txt @@ -0,0 +1,109 @@ +{ + "actions": { + "disallows": { + "resuming": true, + "skipping_prev": true + } + }, + "context": null, + "currently_playing_type": "track", + "device": { + "id": "3e20a2964c1151e118255b819e12e57836e32c1b", + "is_active": true, + "is_private_session": false, + "is_restricted": false, + "name": "SILVERHAND", + "type": "Computer", + "volume_percent": 20 + }, + "is_playing": true, + "item": { + "album": { + "album_type": "album", + "artists": [ + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/2tIP7SsRs7vjIcLrU85W8J" + }, + "href": "https://api.spotify.com/v1/artists/2tIP7SsRs7vjIcLrU85W8J", + "id": "2tIP7SsRs7vjIcLrU85W8J", + "name": "The Kid LAROI", + "type": "artist", + "uri": "spotify:artist:2tIP7SsRs7vjIcLrU85W8J" + } + ], + "available_markets": [ + "AD", + "ZM", + "ZW" + ], + "external_urls": { + "spotify": "https://open.spotify.com/album/3YjfdLdpQcVI72uKhooZst" + }, + "href": "https://api.spotify.com/v1/albums/3YjfdLdpQcVI72uKhooZst", + "id": "3YjfdLdpQcVI72uKhooZst", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/ab67616d0000b273df16d539f508603bfb1efe02", + "width": 640 + }, + { + "height": 300, + "url": "https://i.scdn.co/image/ab67616d00001e02df16d539f508603bfb1efe02", + "width": 300 + }, + { + "height": 64, + "url": "https://i.scdn.co/image/ab67616d00004851df16d539f508603bfb1efe02", + "width": 64 + } + ], + "name": "F*CK LOVE (SAVAGE)", + "release_date": "2020-11-06", + "release_date_precision": "day", + "total_tracks": 22, + "type": "album", + "uri": "spotify:album:3YjfdLdpQcVI72uKhooZst" + }, + "artists": [ + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/2tIP7SsRs7vjIcLrU85W8J" + }, + "href": "https://api.spotify.com/v1/artists/2tIP7SsRs7vjIcLrU85W8J", + "id": "2tIP7SsRs7vjIcLrU85W8J", + "name": "The Kid LAROI", + "type": "artist", + "uri": "spotify:artist:2tIP7SsRs7vjIcLrU85W8J" + } + ], + "available_markets": [ + "AD", + "ZM", + "ZW" + ], + "disc_number": 1, + "duration_ms": 151704, + "explicit": true, + "external_ids": { + "isrc": "USSM12006583" + }, + "external_urls": { + "spotify": "https://open.spotify.com/track/3QCU8BT0zDS9LPVfEhRFEE" + }, + "href": "https://api.spotify.com/v1/tracks/3QCU8BT0zDS9LPVfEhRFEE", + "id": "3QCU8BT0zDS9LPVfEhRFEE", + "is_local": false, + "name": "ALWAYS DO", + "popularity": 71, + "preview_url": "https://p.scdn.co/mp3-preview/d5f01637c9840500d1434131f804a4cc184bd1e4?cid=69b82a34d0fb40be80b020eae8e80f25", + "track_number": 4, + "type": "track", + "uri": "spotify:track:3QCU8BT0zDS9LPVfEhRFEE" + }, + "progress_ms": 25688, + "repeat_state": "off", + "shuffle_state": true, + "timestamp": 1670714537726 +} \ No newline at end of file diff --git a/json_file2.txt b/json_file2.txt new file mode 100644 index 0000000..4e0e559 --- /dev/null +++ b/json_file2.txt @@ -0,0 +1,184 @@ +{ + "artists": { + "href": "https://api.spotify.com/v1/search?query=artist%3Amachine+gun+kelly&type=artist&offset=0&limit=10", + "items": [ + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/6TIYQ3jFPwQSRmorSezPxX" + }, + "followers": { + "href": null, + "total": 4894771 + }, + "genres": [ + "ohio hip hop", + "pop", + "pop rap" + ], + "href": "https://api.spotify.com/v1/artists/6TIYQ3jFPwQSRmorSezPxX", + "id": "6TIYQ3jFPwQSRmorSezPxX", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/ab6761610000e5eb1fd54eb6e30d0bc8f633621e", + "width": 640 + }, + { + "height": 320, + "url": "https://i.scdn.co/image/ab676161000051741fd54eb6e30d0bc8f633621e", + "width": 320 + }, + { + "height": 160, + "url": "https://i.scdn.co/image/ab6761610000f1781fd54eb6e30d0bc8f633621e", + "width": 160 + } + ], + "name": "Machine Gun Kelly", + "popularity": 80, + "type": "artist", + "uri": "spotify:artist:6TIYQ3jFPwQSRmorSezPxX" + }, + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/1lP7f2evgtQcghRmqkPb3j" + }, + "followers": { + "href": null, + "total": 686 + }, + "genres": [], + "href": "https://api.spotify.com/v1/artists/1lP7f2evgtQcghRmqkPb3j", + "id": "1lP7f2evgtQcghRmqkPb3j", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/ab67616d0000b2734f88aa4c73f3017a3e21d9d4", + "width": 640 + }, + { + "height": 300, + "url": "https://i.scdn.co/image/ab67616d00001e024f88aa4c73f3017a3e21d9d4", + "width": 300 + }, + { + "height": 64, + "url": "https://i.scdn.co/image/ab67616d000048514f88aa4c73f3017a3e21d9d4", + "width": 64 + } + ], + "name": "Machine Gun Kelly", + "popularity": 1, + "type": "artist", + "uri": "spotify:artist:1lP7f2evgtQcghRmqkPb3j" + }, + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/4VPvSWeZ4gpl80xJBJ4Dsh" + }, + "followers": { + "href": null, + "total": 2473 + }, + "genres": [ + "swedish hard rock" + ], + "href": "https://api.spotify.com/v1/artists/4VPvSWeZ4gpl80xJBJ4Dsh", + "id": "4VPvSWeZ4gpl80xJBJ4Dsh", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/ab67616d0000b273bed51eb447e0d2c2fcd5d80f", + "width": 640 + }, + { + "height": 300, + "url": "https://i.scdn.co/image/ab67616d00001e02bed51eb447e0d2c2fcd5d80f", + "width": 300 + }, + { + "height": 64, + "url": "https://i.scdn.co/image/ab67616d00004851bed51eb447e0d2c2fcd5d80f", + "width": 64 + } + ], + "name": "MachineGun Kelly", + "popularity": 19, + "type": "artist", + "uri": "spotify:artist:4VPvSWeZ4gpl80xJBJ4Dsh" + }, + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/05m0YksOWOcA4yJ32hqNui" + }, + "followers": { + "href": null, + "total": 340 + }, + "genres": [], + "href": "https://api.spotify.com/v1/artists/05m0YksOWOcA4yJ32hqNui", + "id": "05m0YksOWOcA4yJ32hqNui", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/ab67616d0000b2736fa54a23a30401c989fd5fa7", + "width": 640 + }, + { + "height": 300, + "url": "https://i.scdn.co/image/ab67616d00001e026fa54a23a30401c989fd5fa7", + "width": 300 + }, + { + "height": 64, + "url": "https://i.scdn.co/image/ab67616d000048516fa54a23a30401c989fd5fa7", + "width": 64 + } + ], + "name": "Machine Gun Kelly", + "popularity": 0, + "type": "artist", + "uri": "spotify:artist:05m0YksOWOcA4yJ32hqNui" + }, + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/3I5tJ05CH0HiK1PCw3vqOS" + }, + "followers": { + "href": null, + "total": 847 + }, + "genres": [], + "href": "https://api.spotify.com/v1/artists/3I5tJ05CH0HiK1PCw3vqOS", + "id": "3I5tJ05CH0HiK1PCw3vqOS", + "images": [], + "name": "Machine Gun Kelly & Ray Jr.", + "popularity": 4, + "type": "artist", + "uri": "spotify:artist:3I5tJ05CH0HiK1PCw3vqOS" + }, + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/1DKkR55mGDMlx2bASVW4iq" + }, + "followers": { + "href": null, + "total": 26 + }, + "genres": [], + "href": "https://api.spotify.com/v1/artists/1DKkR55mGDMlx2bASVW4iq", + "id": "1DKkR55mGDMlx2bASVW4iq", + "images": [], + "name": "Machine Gunn Kelly", + "popularity": 0, + "type": "artist", + "uri": "spotify:artist:1DKkR55mGDMlx2bASVW4iq" + } + ], + "limit": 10, + "next": null, + "offset": 0, + "previous": null, + "total": 6 + } +} \ No newline at end of file diff --git a/json_file3.txt b/json_file3.txt new file mode 100644 index 0000000..b7487d9 --- /dev/null +++ b/json_file3.txt @@ -0,0 +1,404 @@ +{ + "albums": { + "href": "https://api.spotify.com/v1/search?query=machine+gun+kelly&type=album&market=US&offset=0&limit=2", + "items": [ + { + "album_type": "album", + "artists": [ + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/6TIYQ3jFPwQSRmorSezPxX" + }, + "href": "https://api.spotify.com/v1/artists/6TIYQ3jFPwQSRmorSezPxX", + "id": "6TIYQ3jFPwQSRmorSezPxX", + "name": "Machine Gun Kelly", + "type": "artist", + "uri": "spotify:artist:6TIYQ3jFPwQSRmorSezPxX" + } + ], + "external_urls": { + "spotify": "https://open.spotify.com/album/3sKZHtQoq3tPtkXbT8PJAc" + }, + "href": "https://api.spotify.com/v1/albums/3sKZHtQoq3tPtkXbT8PJAc", + "id": "3sKZHtQoq3tPtkXbT8PJAc", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/ab67616d0000b273b4683d9ac3c5f14a71523c84", + "width": 640 + }, + { + "height": 300, + "url": "https://i.scdn.co/image/ab67616d00001e02b4683d9ac3c5f14a71523c84", + "width": 300 + }, + { + "height": 64, + "url": "https://i.scdn.co/image/ab67616d00004851b4683d9ac3c5f14a71523c84", + "width": 64 + } + ], + "name": "mainstream sellout", + "release_date": "2022-03-25", + "release_date_precision": "day", + "total_tracks": 16, + "type": "album", + "uri": "spotify:album:3sKZHtQoq3tPtkXbT8PJAc" + }, + { + "album_type": "album", + "artists": [ + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/6TIYQ3jFPwQSRmorSezPxX" + }, + "href": "https://api.spotify.com/v1/artists/6TIYQ3jFPwQSRmorSezPxX", + "id": "6TIYQ3jFPwQSRmorSezPxX", + "name": "Machine Gun Kelly", + "type": "artist", + "uri": "spotify:artist:6TIYQ3jFPwQSRmorSezPxX" + } + ], + "external_urls": { + "spotify": "https://open.spotify.com/album/57lgFncHBYu5E3igZnuCJK" + }, + "href": "https://api.spotify.com/v1/albums/57lgFncHBYu5E3igZnuCJK", + "id": "57lgFncHBYu5E3igZnuCJK", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/ab67616d0000b273d4d4929a3a86fe2f9fadbd42", + "width": 640 + }, + { + "height": 300, + "url": "https://i.scdn.co/image/ab67616d00001e02d4d4929a3a86fe2f9fadbd42", + "width": 300 + }, + { + "height": 64, + "url": "https://i.scdn.co/image/ab67616d00004851d4d4929a3a86fe2f9fadbd42", + "width": 64 + } + ], + "name": "Tickets To My Downfall", + "release_date": "2020-09-25", + "release_date_precision": "day", + "total_tracks": 15, + "type": "album", + "uri": "spotify:album:57lgFncHBYu5E3igZnuCJK" + } + ], + "limit": 2, + "next": "https://api.spotify.com/v1/search?query=machine+gun+kelly&type=album&market=US&offset=2&limit=2", + "offset": 0, + "previous": null, + "total": 532 + }, + "artists": { + "href": "https://api.spotify.com/v1/search?query=machine+gun+kelly&type=artist&market=US&offset=0&limit=2", + "items": [ + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/6TIYQ3jFPwQSRmorSezPxX" + }, + "followers": { + "href": null, + "total": 4894771 + }, + "genres": [ + "ohio hip hop", + "pop", + "pop rap" + ], + "href": "https://api.spotify.com/v1/artists/6TIYQ3jFPwQSRmorSezPxX", + "id": "6TIYQ3jFPwQSRmorSezPxX", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/ab6761610000e5eb1fd54eb6e30d0bc8f633621e", + "width": 640 + }, + { + "height": 320, + "url": "https://i.scdn.co/image/ab676161000051741fd54eb6e30d0bc8f633621e", + "width": 320 + }, + { + "height": 160, + "url": "https://i.scdn.co/image/ab6761610000f1781fd54eb6e30d0bc8f633621e", + "width": 160 + } + ], + "name": "Machine Gun Kelly", + "popularity": 80, + "type": "artist", + "uri": "spotify:artist:6TIYQ3jFPwQSRmorSezPxX" + }, + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/4VPvSWeZ4gpl80xJBJ4Dsh" + }, + "followers": { + "href": null, + "total": 2473 + }, + "genres": [ + "swedish hard rock" + ], + "href": "https://api.spotify.com/v1/artists/4VPvSWeZ4gpl80xJBJ4Dsh", + "id": "4VPvSWeZ4gpl80xJBJ4Dsh", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/ab67616d0000b273bed51eb447e0d2c2fcd5d80f", + "width": 640 + }, + { + "height": 300, + "url": "https://i.scdn.co/image/ab67616d00001e02bed51eb447e0d2c2fcd5d80f", + "width": 300 + }, + { + "height": 64, + "url": "https://i.scdn.co/image/ab67616d00004851bed51eb447e0d2c2fcd5d80f", + "width": 64 + } + ], + "name": "MachineGun Kelly", + "popularity": 19, + "type": "artist", + "uri": "spotify:artist:4VPvSWeZ4gpl80xJBJ4Dsh" + } + ], + "limit": 2, + "next": "https://api.spotify.com/v1/search?query=machine+gun+kelly&type=artist&market=US&offset=2&limit=2", + "offset": 0, + "previous": null, + "total": 25 + }, + "playlists": { + "href": "https://api.spotify.com/v1/search?query=machine+gun+kelly&type=playlist&market=US&offset=0&limit=2", + "items": [ + { + "collaborative": false, + "description": "", + "external_urls": { + "spotify": "https://open.spotify.com/playlist/51UkIAIWZwzn0pav4LTixC" + }, + "href": "https://api.spotify.com/v1/playlists/51UkIAIWZwzn0pav4LTixC", + "id": "51UkIAIWZwzn0pav4LTixC", + "images": [ + { + "height": null, + "url": "https://i.scdn.co/image/ab67706c0000bebb4cba5729c734756bc3171e8f", + "width": null + } + ], + "name": "Machine Gun Kelly: Best Of", + "owner": { + "display_name": "MGK", + "external_urls": { + "spotify": "https://open.spotify.com/user/machinegunkellyak" + }, + "href": "https://api.spotify.com/v1/users/machinegunkellyak", + "id": "machinegunkellyak", + "type": "user", + "uri": "spotify:user:machinegunkellyak" + }, + "primary_color": null, + "public": null, + "snapshot_id": "MzAzLDJiNjNhMjBlMjA2OTA1NDJhNTMwMTk5ZDFjMmVhMTc0ZDA5ZmJmZWM=", + "tracks": { + "href": "https://api.spotify.com/v1/playlists/51UkIAIWZwzn0pav4LTixC/tracks", + "total": 125 + }, + "type": "playlist", + "uri": "spotify:playlist:51UkIAIWZwzn0pav4LTixC" + }, + { + "collaborative": false, + "description": "This is Machine Gun Kelly. The essential tracks, all in one playlist.", + "external_urls": { + "spotify": "https://open.spotify.com/playlist/37i9dQZF1DZ06evO44TgnC" + }, + "href": "https://api.spotify.com/v1/playlists/37i9dQZF1DZ06evO44TgnC", + "id": "37i9dQZF1DZ06evO44TgnC", + "images": [ + { + "height": null, + "url": "https://thisis-images.scdn.co/37i9dQZF1DZ06evO44TgnC-large.jpg", + "width": null + } + ], + "name": "This Is Machine Gun Kelly", + "owner": { + "display_name": "Spotify", + "external_urls": { + "spotify": "https://open.spotify.com/user/spotify" + }, + "href": "https://api.spotify.com/v1/users/spotify", + "id": "spotify", + "type": "user", + "uri": "spotify:user:spotify" + }, + "primary_color": null, + "public": null, + "snapshot_id": "Mjc4NDQ5MDIsMDAwMDAwMDAxYWFhZTljYTZlM2U5NzYzZjVkMDBmMjg3M2JlYzQ4MQ==", + "tracks": { + "href": "https://api.spotify.com/v1/playlists/37i9dQZF1DZ06evO44TgnC/tracks", + "total": 48 + }, + "type": "playlist", + "uri": "spotify:playlist:37i9dQZF1DZ06evO44TgnC" + } + ], + "limit": 2, + "next": "https://api.spotify.com/v1/search?query=machine+gun+kelly&type=playlist&market=US&offset=2&limit=2", + "offset": 0, + "previous": null, + "total": 3793 + }, + "tracks": { + "href": "https://api.spotify.com/v1/search?query=machine+gun+kelly&type=track&market=US&offset=0&limit=2", + "items": [ + { + "album": { + "album_type": "album", + "artists": [ + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/6TIYQ3jFPwQSRmorSezPxX" + }, + "href": "https://api.spotify.com/v1/artists/6TIYQ3jFPwQSRmorSezPxX", + "id": "6TIYQ3jFPwQSRmorSezPxX", + "name": "Machine Gun Kelly", + "type": "artist", + "uri": "spotify:artist:6TIYQ3jFPwQSRmorSezPxX" + } + ], + "external_urls": { + "spotify": "https://open.spotify.com/album/57lgFncHBYu5E3igZnuCJK" + }, + "href": "https://api.spotify.com/v1/albums/57lgFncHBYu5E3igZnuCJK", + "id": "57lgFncHBYu5E3igZnuCJK", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/ab67616d0000b273d4d4929a3a86fe2f9fadbd42", + "width": 640 + }, + { + "height": 300, + "url": "https://i.scdn.co/image/ab67616d00001e02d4d4929a3a86fe2f9fadbd42", + "width": 300 + }, + { + "height": 64, + "url": "https://i.scdn.co/image/ab67616d00004851d4d4929a3a86fe2f9fadbd42", + "width": 64 + } + ], + "name": "Tickets To My Downfall", + "release_date": "2020-09-25", + "release_date_precision": "day", + "total_tracks": 15, + "type": "album", + "uri": "spotify:album:57lgFncHBYu5E3igZnuCJK" + }, + "artists": [ + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/6TIYQ3jFPwQSRmorSezPxX" + }, + "href": "https://api.spotify.com/v1/artists/6TIYQ3jFPwQSRmorSezPxX", + "id": "6TIYQ3jFPwQSRmorSezPxX", + "name": "Machine Gun Kelly", + "type": "artist", + "uri": "spotify:artist:6TIYQ3jFPwQSRmorSezPxX" + }, + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/2cFrymmkijnjDg9SS92EPM" + }, + "href": "https://api.spotify.com/v1/artists/2cFrymmkijnjDg9SS92EPM", + "id": "2cFrymmkijnjDg9SS92EPM", + "name": "blackbear", + "type": "artist", + "uri": "spotify:artist:2cFrymmkijnjDg9SS92EPM" + } + ], + "disc_number": 1, + "duration_ms": 139460, + "explicit": true, + "external_ids": { + "isrc": "USUM72014730" + }, + "external_urls": { + "spotify": "https://open.spotify.com/track/7kDUspsoYfLkWnZR7qwHZl" + }, + "href": "https://api.spotify.com/v1/tracks/7kDUspsoYfLkWnZR7qwHZl", + "id": "7kDUspsoYfLkWnZR7qwHZl", + "is_local": false, + "is_playable": true, + "name": "my ex's best friend (with blackbear)", + "popularity": 78, + "preview_url": null, + "track_number": 11, + "type": "track", + "uri": "spotify:track:7kDUspsoYfLkWnZR7qwHZl" + }, + { + "album": { + "album_type": "album", + "artists": [ + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/6TIYQ3jFPwQSRmorSezPxX" + }, + "href": "https://api.spotify.com/v1/artists/6TIYQ3jFPwQSRmorSezPxX", + "id": "6TIYQ3jFPwQSRmorSezPxX", + "name": "Machine Gun Kelly", + "type": "artist", + "uri": "spotify:artist:6TIYQ3jFPwQSRmorSezPxX" + } + ], + "external_urls": { + "spotify": "https://open.spotify.com/album/5NzdnPboXcqvuvyEH1wSEJ" + }, + "href": "https://api.spotify.com/v1/albums/5NzdnPboXcqvuvyEH1wSEJ", + "id": "5NzdnPboXcqvuvyEH1wSEJ", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/ab67616d0000b273a57d3a2736f815d3cf358e37", + "width": 640 + }, + { + "height": 300, + "url": "https://i.scdn.co/image/ab67616d00001e02a57d3a2736f815d3cf358e37", + "width": 300 + }, + { + "height": 64, + "url": "https://i.scdn.co/image/ab67616d00004851a57d3a2736f815d3cf358e37", + "width": 64 + } + ], + "name": "Lace Up (Deluxe)", + "release_date": "2012-01-01", + "release_date_precision": "day", + "total_tracks": 16, + "type": "album", + "uri": "spotify:album:5NzdnPboXcqvuvyEH1wSEJ" + }, + "artists": [ + } + ], + "limit": 2, + "next": "https://api.spotify.com/v1/search?query=machine+gun+kelly&type=track&market=US&offset=2&limit=2", + "offset": 0, + "previous": null, + "total": 2563 + } +} \ No newline at end of file diff --git a/json_file4.txt b/json_file4.txt new file mode 100644 index 0000000..db66f3f --- /dev/null +++ b/json_file4.txt @@ -0,0 +1,402 @@ +{ + "playlists": { + "href": "https://api.spotify.com/v1/search?query=playlist%3Arap&type=playlist&offset=0&limit=10", + "items": [ + { + "collaborative": false, + "description": "", + "external_urls": { + "spotify": "https://open.spotify.com/playlist/6jkyRflychhI50Lcv9TCfr" + }, + "href": "https://api.spotify.com/v1/playlists/6jkyRflychhI50Lcv9TCfr", + "id": "6jkyRflychhI50Lcv9TCfr", + "images": [ + { + "height": 640, + "url": "https://mosaic.scdn.co/640/ab67616d0000b273419cedff7b313b962a93932eab67616d0000b273bba7cfaf7c59ff0898acba1fab67616d0000b273ce159a3ba2096e13fa9d4b4cab67616d0000b273e31a279d267f3b3d8912e6f1", + "width": 640 + }, + { + "height": 300, + "url": "https://mosaic.scdn.co/300/ab67616d0000b273419cedff7b313b962a93932eab67616d0000b273bba7cfaf7c59ff0898acba1fab67616d0000b273ce159a3ba2096e13fa9d4b4cab67616d0000b273e31a279d267f3b3d8912e6f1", + "width": 300 + }, + { + "height": 60, + "url": "https://mosaic.scdn.co/60/ab67616d0000b273419cedff7b313b962a93932eab67616d0000b273bba7cfaf7c59ff0898acba1fab67616d0000b273ce159a3ba2096e13fa9d4b4cab67616d0000b273e31a279d267f3b3d8912e6f1", + "width": 60 + } + ], + "name": "Playlist Rap us Chill \ud83c\uddfa\ud83c\uddf8", + "owner": { + "display_name": "Pierre-Louis Drouelle", + "external_urls": { + "spotify": "https://open.spotify.com/user/21x6ai6t5q6udgg3hx45fxmay" + }, + "href": "https://api.spotify.com/v1/users/21x6ai6t5q6udgg3hx45fxmay", + "id": "21x6ai6t5q6udgg3hx45fxmay", + "type": "user", + "uri": "spotify:user:21x6ai6t5q6udgg3hx45fxmay" + }, + "primary_color": null, + "public": null, + "snapshot_id": "MzgsYWQwMzNmZTdmNjVkZjYxZDRmOWQ0ZTgwOGFiMWE3OTY4ZGFjYTdhOQ==", + "tracks": { + "href": "https://api.spotify.com/v1/playlists/6jkyRflychhI50Lcv9TCfr/tracks", + "total": 37 + }, + "type": "playlist", + "uri": "spotify:playlist:6jkyRflychhI50Lcv9TCfr" + }, + { + "collaborative": false, + "description": "Les classiques et les nouveaut\u00e9s du Rap FR ! Photo : Orelsan Contact, insta : @lerapenfrance", + "external_urls": { + "spotify": "https://open.spotify.com/playlist/2kZm59MItcYXMO0RMtTTjd" + }, + "href": "https://api.spotify.com/v1/playlists/2kZm59MItcYXMO0RMtTTjd", + "id": "2kZm59MItcYXMO0RMtTTjd", + "images": [ + { + "height": null, + "url": "https://i.scdn.co/image/ab67706c0000bebbca81a1f3a58ab3893bced31d", + "width": null + } + ], + "name": "Playlist Rap Francais 2022", + "owner": { + "display_name": "LeRapEnFrance", + "external_urls": { + "spotify": "https://open.spotify.com/user/math.vecchioni-fr" + }, + "href": "https://api.spotify.com/v1/users/math.vecchioni-fr", + "id": "math.vecchioni-fr", + "type": "user", + "uri": "spotify:user:math.vecchioni-fr" + }, + "primary_color": null, + "public": null, + "snapshot_id": "NjU5NSw0NGMwOGQwOTIyMDYyZWNjYmUwOGVkYzRlNjllZTdiOTkwMDQxM2Vl", + "tracks": { + "href": "https://api.spotify.com/v1/playlists/2kZm59MItcYXMO0RMtTTjd/tracks", + "total": 85 + }, + "type": "playlist", + "uri": "spotify:playlist:2kZm59MItcYXMO0RMtTTjd" + }, + { + "collaborative": false, + "description": "", + "external_urls": { + "spotify": "https://open.spotify.com/playlist/7HFibvUcJL4V1ixYYsGK8J" + }, + "href": "https://api.spotify.com/v1/playlists/7HFibvUcJL4V1ixYYsGK8J", + "id": "7HFibvUcJL4V1ixYYsGK8J", + "images": [ + { + "height": 640, + "url": "https://mosaic.scdn.co/640/ab67616d0000b27336f15d38804d5bdd29b9fc7aab67616d0000b27341b41e6f7924e530a9b00f9cab67616d0000b273ce159a3ba2096e13fa9d4b4cab67616d0000b273f54b99bf27cda88f4a7403ce", + "width": 640 + }, + { + "height": 300, + "url": "https://mosaic.scdn.co/300/ab67616d0000b27336f15d38804d5bdd29b9fc7aab67616d0000b27341b41e6f7924e530a9b00f9cab67616d0000b273ce159a3ba2096e13fa9d4b4cab67616d0000b273f54b99bf27cda88f4a7403ce", + "width": 300 + }, + { + "height": 60, + "url": "https://mosaic.scdn.co/60/ab67616d0000b27336f15d38804d5bdd29b9fc7aab67616d0000b27341b41e6f7924e530a9b00f9cab67616d0000b273ce159a3ba2096e13fa9d4b4cab67616d0000b273f54b99bf27cda88f4a7403ce", + "width": 60 + } + ], + "name": "", + "owner": { + "display_name": "Jakedorworth", + "external_urls": { + "spotify": "https://open.spotify.com/user/toxicsnouis" + }, + "href": "https://api.spotify.com/v1/users/toxicsnouis", + "id": "toxicsnouis", + "type": "user", + "uri": "spotify:user:toxicsnouis" + }, + "primary_color": null, + "public": null, + "snapshot_id": "NjExLDI2OTFjNjYxMmJiOWYzZGQ1NzRhMDJjNDE4MDliOWRiYmM2YjlmODY=", + "tracks": { + "href": "https://api.spotify.com/v1/playlists/7HFibvUcJL4V1ixYYsGK8J/tracks", + "total": 487 + }, + "type": "playlist", + "uri": "spotify:playlist:7HFibvUcJL4V1ixYYsGK8J" + }, + { + "collaborative": false, + "description": "", + "external_urls": { + "spotify": "https://open.spotify.com/playlist/3aUN9KuF9Ce87BXdL86MVI" + }, + "href": "https://api.spotify.com/v1/playlists/3aUN9KuF9Ce87BXdL86MVI", + "id": "3aUN9KuF9Ce87BXdL86MVI", + "images": [ + { + "height": null, + "url": "https://i.scdn.co/image/ab67706c0000bebb72951219c5e1ef0daad297d4", + "width": null + } + ], + "name": "Playlist rap triste ", + "owner": { + "display_name": "bastien lefel", + "external_urls": { + "spotify": "https://open.spotify.com/user/8bu7sbzpe1qn67adebtn65lgn" + }, + "href": "https://api.spotify.com/v1/users/8bu7sbzpe1qn67adebtn65lgn", + "id": "8bu7sbzpe1qn67adebtn65lgn", + "type": "user", + "uri": "spotify:user:8bu7sbzpe1qn67adebtn65lgn" + }, + "primary_color": null, + "public": null, + "snapshot_id": "Mjg4LGI3YmMyMWRlMzI2ZjJiZDA1MTYyMzVlNTE3NjdlNGE2YTQ3YzFhMWY=", + "tracks": { + "href": "https://api.spotify.com/v1/playlists/3aUN9KuF9Ce87BXdL86MVI/tracks", + "total": 243 + }, + "type": "playlist", + "uri": "spotify:playlist:3aUN9KuF9Ce87BXdL86MVI" + }, + { + "collaborative": false, + "description": "You can also find us with: top country rap songs - cowboy rap music - country rap songs - best country rap songs - country rap music - top country rap songs 2022 - cowboy rap song - rap country - country rap 2022 - best country rap songs 2022 - country rap songs 2022 - country rap - rap country 2022", + "external_urls": { + "spotify": "https://open.spotify.com/playlist/25GMAkhuPpZvT2UqNp1G04" + }, + "href": "https://api.spotify.com/v1/playlists/25GMAkhuPpZvT2UqNp1G04", + "id": "25GMAkhuPpZvT2UqNp1G04", + "images": [ + { + "height": null, + "url": "https://i.scdn.co/image/ab67706c0000bebb341e17482a0f26650e4645a9", + "width": null + } + ], + "name": "Country Rap 2022 - Best Country Rap Songs 2022 Playlist (Cowboy Rap Music Hits)", + "owner": { + "display_name": "Redlist Playlists", + "external_urls": { + "spotify": "https://open.spotify.com/user/7bntwjdswpqhrnqr0gssydbd0" + }, + "href": "https://api.spotify.com/v1/users/7bntwjdswpqhrnqr0gssydbd0", + "id": "7bntwjdswpqhrnqr0gssydbd0", + "type": "user", + "uri": "spotify:user:7bntwjdswpqhrnqr0gssydbd0" + }, + "primary_color": null, + "public": null, + "snapshot_id": "MjA5NSwyNmYyZjZmNjViNGIyZTJhYzdlMDA0OWMzZWNmMDUzZDM1ZTg5MTQ0", + "tracks": { + "href": "https://api.spotify.com/v1/playlists/25GMAkhuPpZvT2UqNp1G04/tracks", + "total": 40 + }, + "type": "playlist", + "uri": "spotify:playlist:25GMAkhuPpZvT2UqNp1G04" + }, + { + "collaborative": false, + "description": "HOTTEST HIP HOP BANGERS \ud83d\udd25 BEST INDIE RAP - BEST RAP PLAYLIST 2022 - HIP HOP 2022", + "external_urls": { + "spotify": "https://open.spotify.com/playlist/6cqAN7tZC8eUY3862EYnUK" + }, + "href": "https://api.spotify.com/v1/playlists/6cqAN7tZC8eUY3862EYnUK", + "id": "6cqAN7tZC8eUY3862EYnUK", + "images": [ + { + "height": null, + "url": "https://i.scdn.co/image/ab67706c0000bebb90dd7bab8ab72f66406f6574", + "width": null + } + ], + "name": "Best Rap Playlist 2022 \ud83d\udd25 Hip Hop 2022 \ud83d\udd25 Best Rap Hits 2022 / Hip Hop Hits 2022 / Rap Playlist", + "owner": { + "display_name": "LABYRINTH RECORDS", + "external_urls": { + "spotify": "https://open.spotify.com/user/7wbrcexsh97eqgo8wyk3be4zi" + }, + "href": "https://api.spotify.com/v1/users/7wbrcexsh97eqgo8wyk3be4zi", + "id": "7wbrcexsh97eqgo8wyk3be4zi", + "type": "user", + "uri": "spotify:user:7wbrcexsh97eqgo8wyk3be4zi" + }, + "primary_color": null, + "public": null, + "snapshot_id": "NzM2NCw5NTI1MWE0YTZhYmIxOTVjY2I1Y2U4MGEzODJiYjYxNDcxOTAzZWU5", + "tracks": { + "href": "https://api.spotify.com/v1/playlists/6cqAN7tZC8eUY3862EYnUK/tracks", + "total": 310 + }, + "type": "playlist", + "uri": "spotify:playlist:6cqAN7tZC8eUY3862EYnUK" + }, + { + "collaborative": false, + "description": "", + "external_urls": { + "spotify": "https://open.spotify.com/playlist/4ERLnbsHKTjKRJjLuPjr8g" + }, + "href": "https://api.spotify.com/v1/playlists/4ERLnbsHKTjKRJjLuPjr8g", + "id": "4ERLnbsHKTjKRJjLuPjr8g", + "images": [ + { + "height": 640, + "url": "https://mosaic.scdn.co/640/ab67616d0000b2734ad6e5838f15401ff7d62856ab67616d0000b2739b19c107109de740bad72df5ab67616d0000b273f7f74100d5cc850e01172cbfab67616d0000b273fe3cf32b1320e8ded39d8c74", + "width": 640 + }, + { + "height": 300, + "url": "https://mosaic.scdn.co/300/ab67616d0000b2734ad6e5838f15401ff7d62856ab67616d0000b2739b19c107109de740bad72df5ab67616d0000b273f7f74100d5cc850e01172cbfab67616d0000b273fe3cf32b1320e8ded39d8c74", + "width": 300 + }, + { + "height": 60, + "url": "https://mosaic.scdn.co/60/ab67616d0000b2734ad6e5838f15401ff7d62856ab67616d0000b2739b19c107109de740bad72df5ab67616d0000b273f7f74100d5cc850e01172cbfab67616d0000b273fe3cf32b1320e8ded39d8c74", + "width": 60 + } + ], + "name": "Playlist rap us 2000", + "owner": { + "display_name": "Ade.bltr", + "external_urls": { + "spotify": "https://open.spotify.com/user/8mntv4fukzl27b86nl85t66sf" + }, + "href": "https://api.spotify.com/v1/users/8mntv4fukzl27b86nl85t66sf", + "id": "8mntv4fukzl27b86nl85t66sf", + "type": "user", + "uri": "spotify:user:8mntv4fukzl27b86nl85t66sf" + }, + "primary_color": null, + "public": null, + "snapshot_id": "OTYsY2U5ZDkzYzY1ODM4NGFlMzE1MzdmZGM0ZDk1NzA1OGI1NzkwOGMyZA==", + "tracks": { + "href": "https://api.spotify.com/v1/playlists/4ERLnbsHKTjKRJjLuPjr8g/tracks", + "total": 95 + }, + "type": "playlist", + "uri": "spotify:playlist:4ERLnbsHKTjKRJjLuPjr8g" + }, + { + "collaborative": false, + "description": "Game time", + "external_urls": { + "spotify": "https://open.spotify.com/playlist/2ms4lv3Kl23tXdC0O8F0ZW" + }, + "href": "https://api.spotify.com/v1/playlists/2ms4lv3Kl23tXdC0O8F0ZW", + "id": "2ms4lv3Kl23tXdC0O8F0ZW", + "images": [ + { + "height": null, + "url": "https://i.scdn.co/image/ab67706c0000bebbe28d1988febe51f64b4493bc", + "width": null + } + ], + "name": "Gym Workout Rap Playlist ", + "owner": { + "display_name": "Jayden", + "external_urls": { + "spotify": "https://open.spotify.com/user/31xjko2hitivryhl3xm7afsntbdu" + }, + "href": "https://api.spotify.com/v1/users/31xjko2hitivryhl3xm7afsntbdu", + "id": "31xjko2hitivryhl3xm7afsntbdu", + "type": "user", + "uri": "spotify:user:31xjko2hitivryhl3xm7afsntbdu" + }, + "primary_color": null, + "public": null, + "snapshot_id": "MjUzLGJhZjMyODEwOWIxMDY2OGVjOTQ5Y2VhMTg3NThjNzkyZDc3MGNlNTU=", + "tracks": { + "href": "https://api.spotify.com/v1/playlists/2ms4lv3Kl23tXdC0O8F0ZW/tracks", + "total": 619 + }, + "type": "playlist", + "uri": "spotify:playlist:2ms4lv3Kl23tXdC0O8F0ZW" + }, + { + "collaborative": false, + "description": "Curating Christian Rap culture. Tap the \ud83d\udc9a to follow! The Rapzilla Playlist is the hottest new songs in Christian Hip Hop. Follow our New Songs Daily Playlist also. Cover Artists: DJ Mykael V & 1995", + "external_urls": { + "spotify": "https://open.spotify.com/playlist/4UoWnawInXuG6Z8iyi2YAs" + }, + "href": "https://api.spotify.com/v1/playlists/4UoWnawInXuG6Z8iyi2YAs", + "id": "4UoWnawInXuG6Z8iyi2YAs", + "images": [ + { + "height": null, + "url": "https://i.scdn.co/image/ab67706c0000bebb10306e9bf3961142807c6c4e", + "width": null + } + ], + "name": "Christian Rap - The Rapzilla Playlist", + "owner": { + "display_name": "Rapzilla.com", + "external_urls": { + "spotify": "https://open.spotify.com/user/rapzilla.com" + }, + "href": "https://api.spotify.com/v1/users/rapzilla.com", + "id": "rapzilla.com", + "type": "user", + "uri": "spotify:user:rapzilla.com" + }, + "primary_color": null, + "public": null, + "snapshot_id": "ODQ2OSwwNDQ1ZDQxZDZmNTdiM2I2ODZlMWI3OWVlY2Q5ZTJlMzM3NTJhMjJl", + "tracks": { + "href": "https://api.spotify.com/v1/playlists/4UoWnawInXuG6Z8iyi2YAs/tracks", + "total": 97 + }, + "type": "playlist", + "uri": "spotify:playlist:4UoWnawInXuG6Z8iyi2YAs" + }, + { + "collaborative": false, + "description": "Damn son, where'd ya find this? Cover: MOUNT WESTMORE", + "external_urls": { + "spotify": "https://open.spotify.com/playlist/37i9dQZF1DX3XgesiUXnsq" + }, + "href": "https://api.spotify.com/v1/playlists/37i9dQZF1DX3XgesiUXnsq", + "id": "37i9dQZF1DX3XgesiUXnsq", + "images": [ + { + "height": null, + "url": "https://i.scdn.co/image/ab67706f00000003186ba85b84fc0c0af0ceec24", + "width": null + } + ], + "name": "Raw & Uncut", + "owner": { + "display_name": "Spotify", + "external_urls": { + "spotify": "https://open.spotify.com/user/spotify" + }, + "href": "https://api.spotify.com/v1/users/spotify", + "id": "spotify", + "type": "user", + "uri": "spotify:user:spotify" + }, + "primary_color": null, + "public": null, + "snapshot_id": "MTY3MDU2MjAwMCwwMDAwMDAwMDI1OTFhZjdiZDVhNzIyOTE5ZjY2Mjg4OWYzMzZiMzRl", + "tracks": { + "href": "https://api.spotify.com/v1/playlists/37i9dQZF1DX3XgesiUXnsq/tracks", + "total": 100 + }, + "type": "playlist", + "uri": "spotify:playlist:37i9dQZF1DX3XgesiUXnsq" + } + ], + "limit": 10, + "next": "https://api.spotify.com/v1/search?query=playlist%3Arap&type=playlist&offset=10&limit=10", + "offset": 0, + "previous": null, + "total": 20017 + } +} \ No newline at end of file diff --git a/json_file5.txt b/json_file5.txt new file mode 100644 index 0000000..e69de29 diff --git a/json_file6.txt b/json_file6.txt new file mode 100644 index 0000000..e69de29 diff --git a/librespot_example.py b/librespot_example.py new file mode 100644 index 0000000..6489c0a --- /dev/null +++ b/librespot_example.py @@ -0,0 +1,121 @@ +import os +import re +import socket +import threading + +from librespot.audio.decoders import AudioQuality, VorbisOnlyAudioQuality +from librespot.core import Session +from librespot.metadata import TrackId + +session: Session +sock: socket + + +def handler(client: socket.socket, address: str): + req_raw = client.recv(1024 * 1024) + if len(req_raw) == 0: + return + req_arr = req_raw.split(b"\r\n") + req_http_raw = req_arr[0] + req_header_str = req_raw.split(b"\r\n\r\n")[0] + req_body_str = req_raw.split(b"\r\n\r\n")[1] + req_http_arr = req_http_raw.split(b" ") + req_method = req_http_arr[0] + req_uri = req_http_arr[1] + req_http_version = req_http_arr[2] + req_header = {} + for header in req_header_str.split(b"\r\n"): + try: + key, value = header.split(b": ") + except ValueError: + continue + else: + req_header[key.decode().lower()] = value.decode() + status, headers, content, manually = response(client, req_uri.decode(), + req_header, req_body_str) + if not manually: + client.send(req_http_version + b" " + status.encode() + b"\r\n") + client.send(b"Access-Control-Allow-Origin: *\r\n") + for header in headers: + client.send(header.encode() + "\r\n") + client.send(b"\r\n") + client.send(content) + client.close() + + +class HttpCode: + http_200 = "200 OK" + http_204 = "204 No Content" + http_400 = "400 Bad Request" + http_403 = "403 Forbidden" + http_404 = "404 Not Found" + http_500 = "500 Internal Server Error" + + +def main(): + global session, sock + session = None + if os.path.isfile("credentials.json"): + try: + session = Session.Builder().stored_file().create() + except RuntimeError: + pass + if session is None or not session.is_valid(): + username = input("Username: ") + password = input("Password: ") + session = Session.Builder().user_pass(username, password).create() + if not session.is_valid(): + return + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.bind(("127.0.0.1", 8080)) + sock.listen(5) + while True: + threading.Thread(target=handler, args=sock.accept()).start() + + +def response(client: socket.socket, uri: str, header: dict, + body: bytes) -> tuple[str, list, bytes, bool]: + if re.search(r"^/audio/track/([0-9a-zA-Z]{22})$", uri) is not None: + track_id_search = re.search( + r"^/audio/track/(?P[0-9a-zA-Z]{22})$", uri) + track_id_str = track_id_search.group("TrackID") + track_id = TrackId.from_base62(track_id_str) + stream = session.content_feeder().load( + track_id, VorbisOnlyAudioQuality(AudioQuality.VERY_HIGH), False, + None) + start = 0 + end = stream.input_stream.stream().size() + if header.get("range") is not None: + range_search = re.search( + "^bytes=(?P[0-9]+?)-(?P[0-9]+?)$", + header.get("range")) + if range_search is not None: + start = int(range_search.group("start")) + end = (int(range_search.group("end")) + if int(range_search.group("end")) <= + stream.input_stream.stream().size() else + stream.input_stream.stream().size()) + stream.input_stream.stream().skip(start) + client.send(b"HTTP/1.0 200 OK\r\n") + client.send(b"Access-Control-Allow-Origin: *\r\n") + client.send(b"Content-Length: " + + (str(stream.input_stream.stream().size()).encode() if + stream.input_stream.stream().size() == end else "{}-{}/{}" + .format(start, end, + stream.input_stream.stream().size()).encode()) + + b"\r\n") + client.send(b"Content-Type: audio/ogg\r\n") + client.send(b"\r\n") + while True: + if (stream.input_stream.stream().pos() >= + stream.input_stream.stream().size()): + break + byte = stream.input_stream.stream().read(1) + client.send(byte) + return "", [], b"", True + else: + return HttpCode.http_404, [], HttpCode.http_404.encode(), False + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/program.py b/program.py new file mode 100644 index 0000000..63fb6c6 --- /dev/null +++ b/program.py @@ -0,0 +1,253 @@ +import spotipy +import spotipy.util +import tkinter as ttk +from tkinter import ttk as tk +import random +import json +import requests +import io +from PIL import Image +import urllib.request +import os +from win10toast import ToastNotifier +import sv_ttk +from librespot.core import Session +from librespot.metadata import TrackId +from librespot.audio.decoders import AudioQuality, VorbisOnlyAudioQuality + +# Set the Spotify app's client ID and client secret +client_id = "69b82a34d0fb40be80b020eae8e80f25" +client_secret = "455575b0e3db44acbbfaa0c419bc3c10" +redirect_uri = "http://127.0.0.1:8888/callback" + +# Set the user's Spotify username +username = "thebrandon45" + +# Get the user's Spotify authorization token +scope = "user-read-playback-state,user-modify-playback-state" +token = spotipy.util.prompt_for_user_token(username, scope, client_id, client_secret, redirect_uri) + + +session = Session.Builder() \ + .user_pass("thebrandon45", "Mariposa2502$") \ + .create() + +# Create a Spotify object with the user's authorization token +spotify = spotipy.Spotify(auth=token) + +# Create the tkinter window +root = ttk.Tk() +root.title("Media Controller") +root.attributes("-topmost", True) +sv_ttk.use_dark_theme() + + +# tkinter theme + + +# Function to call the Spotify API to play the current track +def play(): + spotify.start_playback() + +# Function to call the Spotify API to pause the current track +def pause(): + spotify.pause_playback() + +def next(): + spotify.next_track() + +def previous(): + spotify.previous_track() + +def maxvolume(): + spotify.volume(100) + +def minvolume(): + spotify.volume(0) + +def randomvolume(): + spotify.volume(random.randint(0,100)) + +def volumeslider(self): + spotify.volume(int(volumeslider_button.get())) + +def search(event): + searched = track_search.get() + if searched.startswith(":t"): + track_searched_isolated = searched.replace(":t", "") + track_search_results = spotify.search(q='track:' + track_searched_isolated, type='track') + searched_track_id = track_search_results["tracks"]["items"][0]["id"] + spotify.start_playback(uris=["spotify:track:" + searched_track_id]) + if searched.startswith(":a"): + artist_searched_isolated = searched.replace(":a", "") + artist_search_results = spotify.search(q='artist:' + artist_searched_isolated, type='artist') + searched_artist_id = artist_search_results["artists"]["items"][0]["uri"] + spotify.start_playback(context_uri=searched_artist_id) + if searched.startswith(":l"): + album_searched_isolated = searched.replace(":l", "") + album_search_results = spotify.search(q='album:' + album_searched_isolated, type='album') + searched_album_id = album_search_results["albums"]["items"][0]["uri"] + spotify.start_playback(context_uri=searched_album_id) + if searched.startswith(":p"): + playlist_searched_isolated = searched.replace(":p", "") + playlist_search_results = spotify.search(q='playlist:' + playlist_searched_isolated, type='playlist') + searched_playlist_id = playlist_search_results["playlists"]["items"][0]["uri"] + spotify.start_playback(context_uri=searched_playlist_id) + # elif not searched.startswith(":"): + # search_results = spotify.search(q=searched, type='track,artist,album,playlist') + # searched_track_id = search_results[0]["items"]["uri"] + # print(searched_track_id) + # spotify.start_playback(uris=searched_track_id) + +def start_playback_on_device(): + device_selections = devices_list.curselection() + list_of_devices = spotify.devices() + device_id = list_of_devices["devices"][device_selections[0]]["id"] + spotify.transfer_playback(device_id=device_id) + toast = ToastNotifier() + toast.show_toast("Spotify Media Controller", "Playback started on " + list_of_devices["devices"][device_selections[0]]["name"], duration=5, threaded=True) + +def get_devices(): + devices_list.delete(0, ttk.END) + list_of_devices = spotify.devices() + for num_of_device, garbage in enumerate(list_of_devices["devices"]): + devices_list.insert(num_of_device, list_of_devices["devices"][num_of_device]["name"]) + # devices_list_one = list_of_devices["devices"][0]["name"] + # devices_list_two = list_of_devices["devices"][1]["name"] + # devices_list.insert(0, devices_list_one) + # devices_list.insert(1, devices_list_two) + get_devices_button.pack_forget() + hide_devices_button.pack() + devices_list.pack() + start_playback_on_device_button.pack() + +def hide_devices(): + get_devices_button.pack() + devices_list.pack_forget() + start_playback_on_device_button.pack_forget() + hide_devices_button.pack_forget() + +# def start_local_playback(): +# track_id = TrackId.from_uri("spotify:track:0CiZrDrw1traECxGLXolm7") +# stream = session.content_feeder().load(track_id, VorbisOnlyAudioQuality(AudioQuality.VERY_HIGH), False, None) +# stream.input_stream().read(1024) + +# def audioanalysis(): +# current_playback = spotify.current_playback() +# track_id = current_playback["item"]["id"] +# audio_analysis = spotify.audio_analysis(track_id) +# audio_analysis_label.pack() +# audio_analysis_label.config(text=audio_analysis) + + +# frame = tk.Frame(root) +# frame2 = tk.Frame(root) + +# Create the media control buttons and a text label +play_button = tk.Button(root, text="Play", command=play) +pause_button = tk.Button(root, text="Pause", command=pause) +next_button = tk.Button(root, text="Next", command=next) +previous_button = tk.Button(root, text="Previous", command=previous) +maxvolume_button = tk.Button(root, text="Max Volume", command=maxvolume) +minvolume_button = tk.Button(root, text="Min Volume", command=minvolume) +randomvolume_button = tk.Button(root, text="Random Volume", command=randomvolume) +volumeslider_button = tk.Scale(root, from_=0, to=100, orient=ttk.HORIZONTAL, command=volumeslider) +#doaudio_analysis = tk.Button(root, text="Audio Analysis", command=doaudioanalysis) +artist_label = tk.Label(root, text="") +song_label = tk.Label(root, text="") +track_progress_label = tk.Label(root, text="") +track_duration_label = tk.Label(root, text="") +track_combined_label = tk.Label(root, text="") +track_search = tk.Entry(root, text="") +track_search_button = tk.Button(root, text="Search", command=search) +get_devices_button = tk.Button(root, text="Get Devices", command=get_devices) +start_playback_on_device_button = tk.Button(root, text="Start Playback on Device", command=start_playback_on_device) +hide_devices_button = tk.Button(root, text="Hide Devices", command=hide_devices) +username_label = tk.Label(root, text="Username: " + spotify.me()["display_name"]) +# audio_analysis_button = tk.Button(root, text="Audio Analysis", command=audioanalysis) +# audio_analysis_label = tk.Label(root, text="") + +devices_list = ttk.Listbox(root, selectmode=ttk.SINGLE) + +progress_bar = tk.Progressbar(root, orient=ttk.HORIZONTAL, length=180) + +# Play music locally + +# start_local_playback_button = tk.Button(root, text="Start Local Playback", command=start_local_playback) + +# album_image_label = tk.Label(root, image="") + +#audio_analysis = tk.Label(root, text="") +#track_label = tk.Label(root, text="") +# frame.pack() +# frame2.pack() +# Place the media control buttons and text label on the window +username_label.pack() +maxvolume_button.pack() +minvolume_button.pack() +randomvolume_button.pack() +volumeslider_button.pack() +artist_label.pack() +song_label.pack() +#doaudio_analysis.pack() +#audio_analysis.pack() +play_button.pack() +pause_button.pack() +next_button.pack() +previous_button.pack() +track_combined_label.pack() +progress_bar.pack() +track_search.pack() +track_search_button.pack() +get_devices_button.pack() +# start_local_playback_button.pack() +# audio_analysis_button.pack() +# album_image_label.pack() + +root.bind("", search) + +# Function to update the song label with the current track's name +def update_song_label(): + # Get the current playback information + current_playback = spotify.current_playback() + # If there is no current playback, set the text of the song label to "No playback" + if current_playback is None: + nothing_playing_obj = '{"item": {"artists": [{"name": "Nothing Playing"}],"duration_ms": 0,"name": "Nothing Playing"},"progress_ms": 0}' + current_playback = json.loads(nothing_playing_obj) + # Get the current track's name + track_name = current_playback["item"]["name"] + artist_name = current_playback["item"]["artists"][0]["name"] + track_progress = current_playback["progress_ms"] + track_duration = current_playback["item"]["duration_ms"] + # album_image_url = current_playback["item"]["album"]["images"][0]["url"] +# track_id = current_playback["item"]["id"] + + # Set the text of the song label to the track's name + song_label.config(text=track_name) + artist_label.config(text=artist_name) + track_progress_label.config(text=((track_progress//(1000*60))%60, "m", (track_progress//1000)%60, "s")) + track_duration_label.config(text=((track_duration//(1000*60))%60, "m", (track_duration//1000)%60, "s")) + progress_bar.config(maximum=track_duration) + progress_bar.config(value=track_progress) + # urllib.request.urlretrieve(album_image_url, "album_image.jpg") + # image_data = tk.PhotoImage("album_image.jpg") + # album_image_label.config(image=image_data) + track_progress_min = track_progress//(1000*60)%60 + track_progress_sec = (track_progress//1000)%60 + track_duration_min = track_duration//(1000*60)%60 + track_duration_sec = (track_duration//1000)%60 + + track_combined_label.config(text=("{}:{:02d} / {}:{:02d}".format(track_progress_min, track_progress_sec, track_duration_min, track_duration_sec))) + # track_combined_label.config(text=((track_progress//(1000*60))%60, ":", (track_progress//1000)%60, "/", (track_duration//(1000*60))%60, ":", (track_duration//1000)%60)) +# track_label.config(text=track_id) + + # print(json.dumps(current_playback, indent=4, sort_keys=True)) + + # Update the song label every 1 second + root.after(1000, update_song_label) + +# Start updating the song label +update_song_label() + +# Run the GUI +root.mainloop() \ No newline at end of file