def addItems(self, ids, playlist_id_new): query = "https://api.spotify.com/v1/audio-features?ids={}".format(ids) response = requests.get( query, headers={ "Authorization": "Bearer {}".format(self.token) } ) result = response.json() if response.status_code == 400: raise ResponseException(response.status_code) uris = [] for i in result["audio_features"]: if i["danceability"] >= self.danceability and i["acousticness"] >= self.accousticness and i["instrumentalness"] >= self.instrumentalness: uris.append(i["uri"]) request_data = json.dumps(uris) query = "https://api.spotify.com/v1/playlists/{}/tracks".format(playlist_id_new) response = requests.post( query, data=request_data, headers={ "Authorization": "Bearer {}".format(self.token) } ) # check for valid response status if response.status_code == 400: raise ResponseException(response.status_code)
def tracks(self, playlist_ID, playlist_id_new): query = "https://api.spotify.com/v1/playlists/{}/tracks?offset=0&limit=100".format(playlist_ID) response = requests.get( query, headers={ "Authorization": "Bearer {}".format(self.token) } ) result = response.json() ids = "" for i in result["items"]: ids += i["track"]["id"] + "," self.addItems(ids[:-1], playlist_id_new) offset = 100 while offset < result["total"]: response = requests.get( result["next"], headers={ "Authorization": "Bearer {}".format(self.token) } ) result = response.json() if response.status_code == 400: raise ResponseException(response.status_code, message=response.text) ids = "" for i in result["items"]: ids += i["track"]["id"] + "," offset += 100 self.addItems(ids[:-1], playlist_id_new)
def add_songs(self): while True: q= input("Enter Lyrics, or type \"quit()\" to stop searching: ") if(q=="quit()"): break self.find_song(q) uris= [song["uri"] for _,song in self.songs_info.items() if song["uri"]!=None] playlist_id = self.create_playlist() request_data = json.dumps(uris) query = "https://api.spotify.com/v1/playlists/{}/tracks".format(playlist_id) response = requests.post( query, data=request_data, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(SPOTIFY_OAUTH) } ) # check for valid response status if response.status_code != 201: raise ResponseException(response.status_code,response.reason) response_json = response.json() return response_json
def get_spotify_uri(self, song_name, artist): """Search For the Song""" query = "https://api.spotify.com/v1/search?query=track%3A{}+artist%3A{}&type=track&offset=0&limit=20".format( song_name, artist ) response = requests.get( query, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(SPOTIFY_OAUTH) } ) #New Spotify Token is needed for status code 401 if response.status_code != 200: raise ResponseException(response.status_code,response.reason) response_json = response.json() songs = response_json["tracks"]["items"] # only use the first song try: uri = songs[0]["uri"] return uri except: print(f"Couldn't find {song_name} by {artist}") return None
def add_song_to_playlist(self): # Add all liked songs into a new Spotify playlist # Populate dictionary with our liked songs self.get_liked_videos() # Collect all of uri uris = [ info['spotify_uri'] for song, info in self.all_song_info.items() ] # Create a new playlist playlist_id = self.create_playlist() # Add all songs into new playlist request_data = json.dumps(uris) query = 'https://api.spotify.com/v1/playlists/{}/tracks'.format( playlist_id) response = requests.post(query, data=request_data, headers={ 'Content-Type': 'application/json', 'Authorization': 'Bearer {}'.format(spotify_token) }) # Check for valid response status if response.status_code not in (200, 201): raise ResponseException(response.status_code) return response.json()
def parse_data(self): try: picID = self.get_picID() url = self.pic_api + picID res = requests.get(url,headers = self.headers) html = etree.HTML(res.text) picurl = html.xpath('//*[@id="wallpaper"]/@src')[0] size = html.xpath('//*[@id="showcase-sidebar"]/div/div[1]/div[2]/dl/dd[4]/text()')[0] views = html.xpath('//*[@id="showcase-sidebar"]/div/div[1]/div[2]/dl/dd[5]/text()')[0] name_list = html.xpath('//*[@class="showcase-uploader"]/a/img[1]/@alt') name = name_list[0] if len(name_list) != 0 else "Unknown" avatar_url = html.xpath('//*[@class="showcase-uploader"]/a/img[1]/@data-cfsrc')[0] avatar = avatar_url if "http" in avatar_url else "https:" + avatar_url time = html.xpath('//*[@id="showcase-sidebar"]/div/div[1]/div[2]/dl/dd[1]/time/@title')[0] result = { "code": 200, "data": { "image": { "pic_id": picID, "size": size, "views": views, "time": time, "picurl": picurl, }, "author": { "name": name, "avatar": avatar } }, "developer":self.developer } return result except: return ResponseException(status_code= 500,msg="解析失败,请检查链接重试!")
def add_song(self): """Add a song to Spotify playlist.""" # Populate dictionary with liked songs self.get_liked_vids() # Collect all URIs uris = [ info["spotify_uri"] for song, info in self.all_song_info.items() ] # Create new playlists playlist_id = self.create_spotify_playlist() # Add all songs into new playlists request_data = json.dumps(uris) query = query = "https://api.spotify.com/v1/playlists/{}/tracks".format( playlist_id) response = requlests.post(query, data=request_data, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(spotify_token) }) # Check for valid response status if response.status_code != 200: raise ResponseException(response.status_code) response_json = response.json() return response_json
def add_favorite_youtube_song_to_new_spotify_playlist(self): self.get_youtube_liked_videos() # collect al uris with out liked songs uris = [info["spotify_uri"] for song, info in self.all_song_info.items()] uris = [i for i in uris if i] # create a new playlist playlist_id = create_spotify_playlist() # add all songs into new playlist request_data = json.dumps(uris) query = "https://api.spotify.com/v1/playlists/{}/tracks".format( playlist_id) response = requests.post( query, data=request_data, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(spotify_token) } ) # check for valid response status if response.status_code != 200 and response.status_code != 201: raise ResponseException(response.status_code) response_json = response.json() return response_json
def add_song_to_playlist(self): """Adicionar todos os vídeos marcados como gostei para nova playlist""" # Criando um dicionário com todos os vídeo marcados como gostei self.get_liked_videos() # Coletando todas uri's uris = [ info["spotify_uri"] for song, info in self.all_song_info.items() ] # Criar nova playlist playlist_id = self.criar_playlist() # Adicionar músicas à playlist request_data = json.dumps(uris) query = "https://api.spotify.com/v1/playlists/{}/tracks".format( playlist_id) response = requests.post(query, data=request_data, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(spotify_token) }) # check for valid response status if response.status_code != 200: raise ResponseException(response.status_code) response_json = response.json() return response_json
def add_song_to_playlist(self): songs = self.get_most_listened("edupeixoto", limit=30) uris = [] for song in songs: url = self.get_spotify_url(song.get('song'), song.get('artist')) if url: print(song.get('song'), url) uris.append(url) playlist_id = self.create_playlist("Peixo Most Listened", "") request_data = json.dumps(uris) query = "https://api.spotify.com/v1/playlists/{}/tracks".format( playlist_id ) response = requests.post( query, data=request_data, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(spotify_token) } ) if response.status_code != 200: raise ResponseException(response.status_code) response_json = response.json() return response_json
def parse_data(self): try: res1 = requests.get(self.api.format("detail", self.music_id, ""), headers=self.headers) data1 = json.loads(res1.text) br = data1["songs"][0]["h"]["br"] res2 = requests.get(self.api.format("song", self.music_id, br), headers=self.headers) data2 = json.loads(res2.text) music = data2['data'][0]["url"] res3 = requests.get(self.api.format("lyric", self.music_id, ""), headers=self.headers) data3 = json.loads(res3.text) lyric = data3['lrc']["lyric"] result = { "code": 200, "msg": "success", "data": { "name": data1["songs"][0]["name"], "br": br, "author": data1["songs"][0]["ar"][0]["name"], "picurl": data1["songs"][0]["al"]["picUrl"], "music": music, "lyric": lyric, }, "developer": self.developer } return result except: return ResponseException(status_code=500, msg="解析失败,请检查链接重试!")
def get_spotify_uri(self, song_name, artist): query = "https://api.spotify.com/v1/search?query=track%3A{}+artist%3A{}&type=track&market=TW&offset=0&limit=5".format( song_name, artist) response = requests.get(query, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(spotify_token) }) if response.status_code == 401: print("Spotify token need update! \n\ Checkout: https://developer.spotify.com/console/post-playlist-tracks/?playlist_id=&position=&uris= \n\ check scopes: (1) playist-modify-public (2) playlist-read-private \ (3) playlist-read-private (4) playlist-read-collabrative" ) raise ResponseException(response.status_code) response_json = response.json() songs = response_json["tracks"]["items"] # Return spotify uri if track is found if songs: # only use the first song uri = songs[0]["uri"] return uri return None
def add_song_to_spotify_playlist(self): # Add all the liked videos from youtube into the spotify playlist self.get_liked_videos() #collect all the uri uris = [ info["sportiy_uri"] for song, info in self.all_song_info.items() ] playlist_id = self.create_playlist() request_data = json.dumps(uris) query = "https://api.spotify.com/v1/playlists/{}/tracks".format( playlist_id) response = requests.post(query, data=request_data, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(self.spotify_token) }) # check for valid response status if response.status_code != 200: raise ResponseException(response.status_code) response_json = response.json() return response_json
def add_song_to_playlist(self): """Add songs from liked videos to the new Spotify playlist""" self.get_liked_videos() uris = [ info["spotify_uri"] for song, info in self.all_song_info.items() ] playlist_id = self.create_playlist() request_data = json.dumps(uris) # update uri query = "https://api.spotify.com/v1/playlists/{}/tracks".format( playlist_id) response = requests.post(query, data=request_data, headers={ "Content-Type": "application/json", "Authorisation": "Bearer {}".format(spotify_token) }) # check response if response.status_code != 200: raise ResponseException(response.status_code) response_json = response.json() return response_json
def parse_data(self): try: data = self.get_data() if data['status_code'] == 0: code = 200 else: code = 0 data = data['item_list'][0] result = { "code": code, "msg": "解析成功", "data": { "video": { "comment_count": data['statistics']['comment_count'], "digg_count": data['statistics']['digg_count'], "share_count": data['statistics']['share_count'], "share_title": data['share_info']['share_title'], "cover": data['video']['cover']['url_list'][0], "music": data['music']['play_url']['url_list'][0], "play_addr": data['video']['play_addr']['url_list'][0] }, "author": { "uid": data['author']['uid'], "short_id": data['author']['short_id'], "nickname": data['author']['nickname'], "avatar": data['author']['avatar_larger']['url_list'][0] } }, "developer": self.developer } return result except: return ResponseException(status_code= 500,msg="解析失败,请检查链接重试!")
def searchShow(self): pod_name = input("What is the name of your podcast? ") print("Searching for " + pod_name + "\n") query = "https://api.spotify.com/v1/search?q=name:{}&type=show".format( pod_name.replace(' ', '%20')) response = requests.get(query, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(spotify_token) }) if response.status_code != 200: webbrowser.open( "https://developer.spotify.com/console/get-show-episodes/") raise ResponseException(response.status_code) response_json = response.json() shows = response_json["shows"]["items"] print(str(len(response_json["shows"]["items"])) + " shows found\n") for show in shows: print("Name: " + show["name"] + " / " + show["description"].strip() + "\n") if (input("Is this the show you were looking for (Y/N)? ").lower() == "y"): return show["id"] print(":(") quit()
def add_song_to_playlist(self): """Add all liked songs into a new Spotify playlist""" # populate our songs dictionary self.get_liked_videos() # collect all of uri uri = [] for song, info in self.all_song_info.items(): uri.append(info['spotify_uri']) # create a new playlist playlist_id = self.create_playlist() # add all songs into new playlist request_data = json.dumps(uris) query = 'https://api.spotify.com/v1/playlists/{}/tracks'.format(playlist_id) response = requests.post( query, data = request_data, header = { 'Content-Type':'application/json', 'Authorization':'Bearer {}'.format(spotify_token) } ) # check for valid response status if response.status.code != 200: raise ResponseException(response.status.code) response_json = response.json() return response_json
def add_to_playlist(self): # collect all of uri uris = [f"spotify:track:{track}" for track in self.recommended_tracks()] # create a new playlist playlist_id = self.create_playlist() # add all songs into new playlist request_body = json.dumps(uris) query = "https://api.spotify.com/v1/playlists/{}/tracks".format( playlist_id) response = requests.post( query, data=request_body, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(spotify_token) } ) # check for valid response status if response.status_code != 200: raise ResponseException(response.status_code) response_json = response.json() return response_json
def AddSong(self): # populate song dictionary self.GetLikedVideos() # collect all uris uris = [] for song, info in self.allSongsInfo.items(): uris.append(info["spotifyUri"]) # create a new playlist playlistID = self.CreatePlaylist() # add songs into new playlist requestData = json.dumps(uris) EndPoint = "https://api.spotify.com/v1/tracks/{id}".format(playlistID) response = requests.post( EndPoint, data=requestData, headers={ "Content-Type": "application/json", "Authorization": "Bearer{}".format(Spotifytoken), }, ) if response.status_code != 200: raise ResponseException(response.status_code) response_json = response_json() return response_json
def add_song_to_playlist(self): """Add all liked songs into a new Spotify playlist""" # populate dictionary with our liked songs self.get_liked_videos() # collect all of uri uris = [info["spotify_uri"] for song, info in self.all_song_info.items()] # create a new playlist playlist_id = self.create_playlist() # add all songs into new playlist request_data = json.dumps(uris) query = "https://api.spotify.com/v1/playlists/{}/tracks".format( playlist_id) response = requests.post( query, data=request_data, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(spotify_token) } ) # check for valid response status if (response.status_code != 201) : raise ResponseException(response.status_code) response_json = response.json() return response_json
def add_song_to_playlist(self): self.get_playlist_videos() uris = [ info["spotify_uri"] for song, info in self.all_song_info.items() ] # remove null values from list uris = [i for i in uris if i] playlist_id = self.create_playlist() request_data = json.dumps(uris) query = "https://api.spotify.com/v1/playlists/{}/tracks".format( playlist_id) response = requests.post(query, data=request_data, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(spotify_token) }) if response.status_code != 200 or response.status_code != 201: raise ResponseException(response.status_code) response_json = response.json() return response_json
def add_songs_to_playlists(self): """Adds songs to the created playlists""" self.get_my_songs() playlists = self.check_existing_playlists() for key, value in self.songs_info.items(): if 'Songs from {}'.format(key) in playlists.keys(): self.add_songs_to_existing_playlists(key, value, playlists) else: playlist_id = self.create_playlist(key) request_data = json.dumps(value) query = "https://api.spotify.com/v1/playlists/{}/tracks".format( playlist_id) response = requests.post(query, data=request_data, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(spotify_token) }) if response.status_code != 200 or response.status_code != 201: raise ResponseException(response.status_code)
def add_song_to_playlist(self, songs_lst): """Add all liked songs into a new Spotify playlist""" # # collect all of uri # uris = [info["spotify_uri"] # for song, info in self.all_song_info.items()] # Create a new playlist playlist_id = self.create_playlist() # Add all songs into new playlist request_data = json.dumps(songs_lst) query = "https://api.spotify.com/v1/playlists/{}/tracks".format( playlist_id) response = requests.post(query, data=request_data, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(self.oauth_token) }) # TODO Create HTTP response handler # check for valid response status if response.status_code != 200 | response.status_code != 201: raise ResponseException(response.status_code) response_json = response.json() return response_json
def add_song_to_playlist(self): # populate the dictionary self.get_liked_videos() # collect all uris uris = [] for song, infos in self.all_song_info.items(): uris.append(info['spotify_uri']) # create a new playlist playlist_id = self.create_playlist() # add all songs into playlist request_data = json.dumps(uris) query = 'https://api.spotify.com/v1/playlists/{}/tracks'.format( playlist_id) response = requests.post(query, data=request_data, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format(spotify_token) }) # check for valid response status if response.status_code != 200: raise ResponseException(response.status_code) response_json = response.json() return response_json
def parse_data(self): try: file = open('source/soul.txt', 'r', encoding='utf-8') ls = file.readlines() count = len(ls) txt = ls[random.randint(0, count)].replace("\n", "") file.close() result = {"code": 200, "data": txt, "developer": self.developer} return result except: return ResponseException(status_code=500, msg="请求失败,请检查链接重试!")
def parse_data(self): try: data = self.get_data() result = { "code": 200, "msg": "请求成功", "data": data, "developer": self.developer } return result except: return ResponseException(status_code=500, msg="解析失败,请检查链接重试!")
def parse_data(self): try: total = self.get_total() start = random.randint(0, int(total)) url = self.api.format(self.cid, start, self.count) res = requests.get(url, headers=self.headers) data = json.loads(res.text) result = { "code": 200, "data": data['data'], "developer": self.developer } return result except: return ResponseException(status_code=500, msg="解析失败,请检查链接重试!")
def add_song_to_playlist(self, target_playlist, playlist_name, playlist_desc): # populate dictionary self.get_mix(target_playlist) # check to see if spotify playlist already exists exists = self.check_for_playlist(playlist_name) if exists: # go through process of filtering out existing song, thus updating rather than dumping repeats self.update_playlist(exists) playlist_id = exists # create a new playlist if doesn't exist else: playlist_id = self.create_playlist(playlist_name, playlist_desc) # collect uris uris = [ info["spotify_uri"] for song, info in self.all_song_info.items() ] # add all songs to playlist request_data = json.dumps(uris) # print("Le Musica de Harry Frause:", request_data) endpoint = "https://api.spotify.com/v1/playlists/{}/tracks".format( playlist_id) response = requests.post(endpoint, data=request_data, headers={ "Content-Type": "application/json", "Authorization": "Bearer {}".format( self.spotify_client_token) }) # print("All Song Info: ", self.all_song_info) # check for valid response status if response.status_code != 201: raise ResponseException(response.status_code) response_json = response.json() return response_json
def add_song_to_playlist(self): # populate our songs dictionary # TODO: change this to ask which playlist they would like to gather videos from print("Select the playlist you would like to send to Spotify") self.playlists[1] = "Liked videos" print("1. Liked videos") self.get_playlists() print("Number: ", end = "") num = input() # If the choice is 1, then we get the liked videos; otherwise we get the videos from that playlist if int(num) == 1: self.get_liked_videos() else: self.get_playlist_videos(int(num)) # collect all uris uris = [] for song, info in self.all_song_info.items(): if info["spotify_uri"] is not None: uris.append(info["spotify_uri"]) # create a new playlist playlist_id = self.create_playlist() # add all new songs into playlist request_data = json.dumps(uris) print(request_data) query = "https://api.spotify.com/v1/playlists/{}/tracks".format(playlist_id) response = requests.post( query, data = request_data, headers = { "Content-Type": "application/json", "Authorization": "Bearer {}".format(self.spotify_token) } ) # check for valid response status if response.status_code != 200 || response.status_code != 201: raise ResponseException(response.status_code) response_json = response.json() return response_json
def parse_data(self): try: data = self.get_data() result = { "code": 200, "msg": "解析成功", "data": { "video": { "share_title": data['feed_desc_withat'], "cover": data['video_cover']['static_cover']['url'], "play_addr": data['video_url'] }, "developer": self.developer } } return result except: return ResponseException(status_code= 500,msg="解析失败,请检查链接重试!")