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 get(self): global threads global threadid parser = reqparse.RequestParser() parser.add_argument('spotifyid', type=str, required=True, help='spotify playlist URI') parser.add_argument('name', type=str, required=True, help='name for playlist') parser.add_argument('auth', type=str, required=True, help='auth token defined in ..env') args = parser.parse_args() args['spotifyid'] = args['spotifyid'].split(':')[-1] if args['auth'] != Config.PLAYLIST_API_SECRET: return {"message": "unauthorized"}, 401 # create and populate the metadata of the new playlist playlist_object = Playlist() playlist_object.name = args['name'] playlist_object.thumbnail = "/resource/placeholder.png" playlist_object.spotifyid = args['spotifyid'] db.session.add(playlist_object) db.session.commit() # create the thread to populate it threadid += 1 thread = GetSpotifyPlaylistWithITunesThread(playlist_object.id, args['spotifyid'], threadid, name="Playlist-thread-%i" % threadid) # add new thread to queue and start it if no threads running threads.append(thread) if not thread_running: threads.pop().start() return { 'threadid': threadid, 'queue_length': len(threads), 'started': True } return { 'threadid': threadid, 'queue_length': len(threads), 'started': False }