def create_new_drain(user, dListNameame, drainlist, sources): """ Creates a new drainlist :param user: User owning the Drainlist :param drainlist: Name for new Drainlist (should be a URI) :param sources: list of sources to associate with the new drain :return: the contents of the file as a string """ if "spotify:playlist:" not in sources[0]: sources = ["spotify:playlist:" + source for source in sources] sources = [{ "URI": source, "Name": spio.get_name(spio.get_access_token(user), source) } for source in sources] with playlist.open_drainlist(user, drainlist, "w+") as dfile: json.dump( { "Name": dListNameame, "Playlist_URI": drainlist, "Sources": sources }, dfile) with playlist.open_drainlist(user, drainlist) as dfile: dlist = playlist.Drainlist(user, dfile) dlist.populate(spio.get_access_token(user)) dlist.cleanup(user) return json.dumps({ "Name": dListNameame, "Playlist_URI": drainlist, "Sources": sources })
def setUp(self): spotify.create_new_drain(test_user, self.drainSinkName ,self.drain_name, self.sources) self.access_token = spio.get_access_token(test_user) with playlist.open_drainlist(test_user, self.drain_name) as infile: self.Dlist = playlist.Drainlist(test_user, infile) if spio.get_tracks(self.access_token, self.Dlist.uri): self.Dlist.depopulate(spio.get_access_token(test_user))
def refresh_request(): user = request.args["user"] token = spio.get_access_token(user) refresh(user, token) resp = app.make_response("success") resp.headers['Access-Control-Allow-Origin'] = '*' return resp
def test_integ_populate_and_sync(self): # drop a track dropped_track = self.Dlist.sources[0].reference.tracks.pop() self.Dlist.sources[0].tracks.remove(dropped_track) # populate self.Dlist.populate(self.access_token) tracks = self.tracks[:] tracks.remove(dropped_track) self.assertEqual(set(tracks), set(spio.get_tracks(self.access_token, self.Dlist.uri))) # re-add the track and sync self.Dlist.sources[0].tracks += [dropped_track] diff = self.Dlist.sync() spio.add_tracks_to_drain(self.access_token, self.Dlist, diff) # check if the track was added tracks += [dropped_track] self.assertEqual(set(tracks), set(spio.get_tracks(self.access_token, self.Dlist.uri))) # depopulate self.Dlist.depopulate(spio.get_access_token(test_user)) self.assertEqual(set(), set(spio.get_tracks(self.access_token, self.Dlist.uri))) self.Dlist.cleanup(test_user) self.assertEqual(set(os.listdir(test_user + "/Playlists/")), {'spotify:playlist:4L3PeQ9LzinSq0Q3KnzLvb_ref', 'spotify:playlist:069rrIb9s1MRw2BBwXmeJE_drain', 'spotify:playlist:6E2XjEeEOEhUKVoftRHusb_ref'})
def test_create_and_delete_playlist(self): token = spio.get_access_token(test_user) uri = spio.create_playlists(token, "test") plists = spio.get_playlists(token) self.assertTrue(uri in [pl["uri"] for pl in plists]) spio.remove_playlist(token, uri) plists = spio.get_playlists(token) self.assertFalse(uri in [pl["uri"] for pl in plists])
def list_playlists_request(): user = request.args["user"] lists = spio.get_playlists(spio.get_access_token(user)) resp = app.make_response( json.dumps([{ "name": l["name"], "uri": l["uri"] } for l in lists])) resp.headers['Access-Control-Allow-Origin'] = '*' return resp
def test_populate_depopulate(self): with playlist.open_drainlist(test_user, dname) as infile: d = playlist.Drainlist(test_user, infile) token = spio.get_access_token(test_user) tracks = spio.get_tracks(token, listname) + spio.get_tracks( token, list2name) d.populate(token) self.assertEqual(set(tracks), set(spio.get_tracks(token, d.uri))) d.depopulate(token) self.assertEqual(set(), set(spio.get_tracks(token, d.uri)))
def create_new_drain_from_name(user, dlistName, sources): """ Given a name (non-URI) this creates a new sink playlist and sets up the URI work as expected :param user: User owning the Drainlist :param dlistName: name for the sink playlist (Spotify Name) :param sources: sources to be associated with the new Drainlist :return: todo prevent name collisions """ # create new playlist with given name, get the URI and proceed uri = spio.create_playlists(spio.get_access_token(user), dlistName) return create_new_drain(user, dlistName, uri, sources)
def add_source_api(self, uri): """ Adds a new Playlist source to a drainlist via the API using the Playlist URI :param uri: URI of the playlist to be added :return: None """ # here if source file does not exist, but still try: refFile = open_playlist(self.user, uri + "_ref") ref = Playlist.from_file(self.user, refFile, None) except Exception as e: ref = Playlist.from_web_api(self.user, spio.get_access_token(self.user), uri, None) templist = Playlist.from_web_api(self.user, spio.get_access_token(self.user), uri, ref) templist.write_out() ref.write_out() self.sources += [templist]
def initialize(): try: spio.get_access_token(user) return redirect(auth_completed_url) except Exception as e: return redirect(spio.get_new_tokens().url)
def test_get_name(self): token = spio.get_access_token(test_user) n = spio.get_name(token, "spotify:playlist:069rrIb9s1MRw2BBwXmeJE") self.assertEqual(n, "Default")
def tearDown(self): for f in os.listdir(test_user + "/Playlists/"): os.remove(test_user + "/Playlists/" + f) if spio.get_tracks(self.access_token, self.Dlist.uri): self.Dlist.depopulate(spio.get_access_token(test_user))