{"id":1035,"date":"2022-12-02T11:24:44","date_gmt":"2022-12-02T11:24:44","guid":{"rendered":"https:\/\/thomas.w-p.me.uk\/blog\/?p=1035"},"modified":"2022-12-02T11:42:41","modified_gmt":"2022-12-02T11:42:41","slug":"spotify-api","status":"publish","type":"post","link":"https:\/\/thomas.w-p.me.uk\/blog\/2022\/12\/spotify-api\/","title":{"rendered":"Spotify API and Squeezebox transfer"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Had fun and games getting the API to work with <a href=\"https:\/\/spotipy.readthedocs.io\/en\/2.21.0\/\">spotipy<\/a>. All due t orustiness and inexpeerience. I just couldn&#8217;t get it to authenticate until I realised how to put my credentials in the script.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This was all part of a project to move our entire library of ripped CDs from the Squeezebox to Spotify. I was able to export the database to excel, clean it up, use the excel to make lines of code, then went through in batches of 100. I figured this would be quicker than hacking away maging it work with the spotify paging system as it was a one off task.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I did put the API authentication details in to a json file though:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"client_id\": \"xxxxxxxxxxxxxxxxxxxxxxxxxx\",\n\"client_secret\": \"xxxxxxxxxxxxxxxxxxxxxxxxxx\",\n\"redirect_uri\": \"https:\/\/localhost:8080\"\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The code (no promises it is not changed a bit), but it might help someone.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Shows a user's playlists\n\nimport spotipy\nfrom spotipy.oauth2 import SpotifyOAuth\nimport json\n\nimport sys\nimport pprint\n\n# Opening JSON file\nf = open('authorisation.json')\n# returns JSON object as \n# a dictionary\ndata = json.load(f)\n\nf = open('playlist_track_ids.json')\na_track_ids = json.load(f)\n\n# scope = 'playlist-read-private'\n# sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=data&#91;'client_id'],\n#                                     client_secret=data&#91;'client_secret'],\n#                                     redirect_uri=data&#91;'redirect_uri'],\n#                                     scope=scope))\n\n# results = sp.current_user_playlists(limit=50)\n# for i, item in enumerate(results&#91;'items']):\n#     print(\"%d %s %s\" % (i, item&#91;'name'],item&#91;'id']))\n\nscope = 'playlist-modify-private'\nsp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=data&#91;'client_id'],\n                                    client_secret=data&#91;'client_secret'],\n                                    redirect_uri=data&#91;'redirect_uri'],\n                                    scope=scope))\n\n\nsongsAndArtists = {\n#'Afro Angel': 'Will Smith',\n'Sandstorm': 'Cast',\n'Mis-Shapes': 'Pulp',\n'Mansize Rooster': 'Supergrass',\n'Sleeping In': 'Menswear',\n'Getting Better': 'Shed Seven',\n'From a Window': 'Northern Uproar',\n'The Riverboat Song': 'Ocean Colour Scene',\n'Weak': 'Skunk Anansie',\n'Divebomb': 'Number One Cup',\n'Time': 'Marion',\n'Far': 'Longpigs',\n'The Universal': 'Blur',\n'Dark Therapy': 'Echobelly',\n'For the Dead': 'Gene',\n'Ten Storey Love Song': 'The Stone Roses',\n'Just': 'Radiohead',\n'Lucky You': 'The Lightning Seeds',\n'Carnival': 'The Cardigans',\n'Anywhere': 'Dubstar',\n'Come Together': 'The Smokin\\' Mojo Filters',\n'Fade Away': 'Oasis and Friends',\n'Flying': 'Cast',\n'Beautiful Ones': 'Suede',\n'Everything Must Go': 'Manic Street Preachers',\n'Fighting Fit': 'Gene',\n'Twisted (Everyday Hurts)': 'Skunk Anansie',\n'Lost Myself': 'Longpigs',\n'Disco 2000': 'Pulp',\n'Ready or Not': 'The Lightning Seeds',\n'You\\'ve Got It Bad': 'Ocean Colour Scene',\n'C\\'mon Kids': 'The Boo Radleys',\n'Lava': 'Silver Sun',\n'Kandy Pop': 'Bis',\n'Teenage Angst': 'Placebo',\n'Burden in My Hand': 'Soundgarden',\n'Celebrity Hit List': 'Terrorvision',\n'Boy Wonder': 'Speedy',\n'No One Speaks': 'Geneva',\n'Great Things': 'Echobelly',\n'Nice Guy Eddie': 'Sleeper',\n'Sparkle': 'My Life Story',\n'Morning Glory': 'Oasis',\n'Milk': 'Garbage featuring Tricky',\n'6 Underground': 'Sneaker Pimps',\n'If You\\'re Thinking of Me': 'Dodgy',\n'Chasing Rainbows': 'Shed Seven',\n'For You': 'Electronic',\n'You\\'re Gorgeous': 'Babybird',\n'Patio Song': 'Gorky\\'s Zygotic Mynci',\n'Neighbourhood': 'Space',\n'Girls &amp; Boys': 'Blur',\n'The Frog Princess': 'The Divine Comedy',\n'Whatever': 'Oasis',\n'Lazy Lover': 'The Supernaturals',\n'Lenny': 'Supergrass',\n'If You Don\\'t Want Me to Destroy You': 'Super Furry Animals',\n'Broken Stones': 'Paul Weller',\n'If I Could Talk I\\'d Tell You': 'The Lemonheads',\n'Street Spirit (Fade Out)': 'Radiohead',\n'Not So Manic Now': 'Dubstar',\n'You\\'ve Got a Lot to Answer For': 'Catatonia',\n'One Night Stand': 'The Aloof',\n\n\n}\n\nsongsAndAlbums = {\n#'Deep Blue Day': 'Trainspotting',\n\n}\n\ni=0\n# Iterating over values\nfor name, artist in songsAndArtists.items():\n    \n    results = sp.search(q='track:' + name + ' artist:' + artist, type='track')\n    items = results&#91;'tracks']&#91;'items']\n    if len(items) &gt; 0:\n\n        track = items&#91;0]\n        try:\n            a_track_ids.index(track&#91;'id'])\n            #print('Found: '+track&#91;'name'])\n        except:\n            # #add to the list\n            # sp.playlist_add_items(playlist_id='2CE0LjqBpPFqdv2CnToUIh', items=&#91;'spotify:track:'+track&#91;'id']+''], position=None)\n            # a_track_ids.append(track&#91;'id'])\n            print('***Added: '+track&#91;'name'])\n\n\ni=0\n# Iterating over values\nfor name, album in songsAndAlbums.items():\n    \n    results = sp.search(q='track:' + name + ' album:' + album, type='track')\n    items = results&#91;'tracks']&#91;'items']\n    if len(items) &gt; 0:\n\n        track = items&#91;0]\n        \n        try:\n            a_track_ids.index(track&#91;'id'])\n            #print('Found: '+track&#91;'name'])\n        except:\n            # #add to the list\n            # sp.playlist_add_items(playlist_id='2CE0LjqBpPFqdv2CnToUIh', items=&#91;'spotify:track:'+track&#91;'id']+''], position=None)\n            # a_track_ids.append(track&#91;'id'])\n            print('***Added: '+track&#91;'name'])\n\n\nwith open(\"playlist_track_ids.json\", \"w\") as outfile: \n    json.dump(a_track_ids, outfile)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Had fun and games getting the API to work with spotipy. All due t orustiness and inexpeerience. I just couldn&#8217;t get it to authenticate until I realised how to put my credentials in the script. This was all part of a project to move our entire library of ripped CDs from the Squeezebox to Spotify. &hellip; <a href=\"https:\/\/thomas.w-p.me.uk\/blog\/2022\/12\/spotify-api\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Spotify API and Squeezebox transfer<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,105],"tags":[],"class_list":["post-1035","post","type-post","status-publish","format-standard","hentry","category-geekiness","category-python"],"_links":{"self":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/1035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/comments?post=1035"}],"version-history":[{"count":4,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/1035\/revisions"}],"predecessor-version":[{"id":1045,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/1035\/revisions\/1045"}],"wp:attachment":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/media?parent=1035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/categories?post=1035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/tags?post=1035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}