def load_playlist(fname): try: with open(fname + ".playlist") as f: playlist = Playlist() data = json.load(f) playlist.name = data['name'] playlist.thumbnail = data['thumbnail'] db.session.add(playlist) db.session.commit() for s in data['songs']: song_obj = Song.query.filter_by( itunes_resource_id=s['itunes_resource_id']).first() if song_obj is None: song_obj = Song() song_obj.name = s['name'] song_obj.artist = s['artist'] song_obj.album = s['album'] song_obj.thumbnail_url = s['thumbnail_url'] song_obj.preview_url = s['preview_url'] song_obj.external_url = s['external_url'] song_obj.itunes_resource_id = s['itunes_resource_id'] db.session.add(song_obj) db.session.commit() playlist.songs.append(song_obj) db.session.commit() return True except IOError as e: return False return False
def __createSongModel(self, url, metadata): song = Song() song.url = url id = metadata.get('id') song.media_id = f'{id}.mp3' song.title = metadata.get('title') song.alt_title = metadata.get('alt_title') song.artist = metadata.get('artist') song.album = metadata.get('album') return song
def run(self): global threads, thread_running thread_running = self print("{} started!".format(self.getName())) d = spotifyapi._getPlaylist(self.spotifyURI) data = [] for t in d['tracks']['items']: data.append((t['track']['artists'][0]['name'], t['track']['name'], t['track']['album']['name'])) for i, song in enumerate(data): self._progress = i / float(len(data)) cleanArtist = song[0].lower().replace(' ', '+') cleanSong = song[1].split(' - ', 1)[0].strip().lower().replace(' ', '+') r = requests.get(self._its % cleanSong) print(self._its % cleanSong, cleanArtist) itdata = json.loads(r.text)["results"] playlist = Playlist.query.get(self.playlistID) success = False for result in itdata: if result['artistName'] == song[0] and result[ 'collectionName'] == song[2]: song_obj = Song.query.filter_by( itunes_resource_id=result['trackId']).first() if song_obj is None: song_obj = Song() song_obj.name = result['trackCensoredName'] song_obj.artist = result['artistName'] song_obj.album = result['collectionCensoredName'] song_obj.thumbnail_url = result[ 'artworkUrl100'].replace('100x100', '400x400') song_obj.preview_url = result['previewUrl'] song_obj.external_url = result['trackViewUrl'] song_obj.itunes_resource_id = result['trackId'] db.session.add(song_obj) db.session.commit() print('adding song...') playlist.songs.append(song_obj) db.session.commit() success = True break if success: continue for result in itdata: if result['artistName'] == song[0]: song_obj = Song.query.filter_by( itunes_resource_id=result['trackId']).first() if song_obj is None: song_obj = Song() song_obj.name = result['trackCensoredName'] song_obj.artist = result['artistName'] song_obj.album = result['collectionCensoredName'] song_obj.thumbnail_url = result[ 'artworkUrl100'].replace('100x100', '400x400') song_obj.preview_url = result['previewUrl'] song_obj.external_url = result['trackViewUrl'] song_obj.itunes_resource_id = result['trackId'] db.session.add(song_obj) db.session.commit() print('adding song (no album match)...') playlist.songs.append(song_obj) db.session.commit() break time.sleep(3) print("{} finished!".format(self.getName())) # if more threads waiting, start the next if len(threads) > 0: thread_running = threads.pop(0) thread_running.start() else: thread_running = None