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 test_add_source_init(self): with playlist.open_drainlist(test_user, dname) as infile: d = playlist.Drainlist(test_user, infile) d.remove_source(list2name) d.add_source_file(list2name) self.assertEqual(set([s.uri for s in d.sources]), {listname, list2name})
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 test_write_out(self): with playlist.open_drainlist(test_user, dname) as infile: d = playlist.Drainlist(test_user, infile) d.write_out() with playlist.open_drainlist(test_user, dname) as infile: data = infile.read() self.assertTrue(listname in data) self.assertTrue(list2name in data) self.assertTrue(dname in data)
def test_init(self): with playlist.open_drainlist(test_user, dname) as infile: d = playlist.Drainlist(test_user, infile) self.assertEqual(d.user, test_user) self.assertEqual(d.uri, dname) self.assertEqual(set(d.source_uris), {listname, list2name}) self.assertEqual(set(d.source_names), {"P1", "P2"}) self.assertEqual({s.uri for s in d.sources}, {listname, list2name})
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 test_add_remove_tracks_to_from_drain(self): with playlist.open_drainlist(test_user, dname) as infile: d = playlist.Drainlist(test_user, infile) dump_tracks = ["spotify:track:7EE7jbv7Dv8ZkyWBlKhPXX"] spio.add_tracks_to_drain(self.token, d, dump_tracks) tracks = spio.get_tracks(self.token, d.uri) self.assertEqual(tracks, dump_tracks) spio.remove_tracks_from_drain(self.token, d, dump_tracks) spio.get_tracks(self.token, d.uri) tracks = spio.get_tracks(self.token, d.uri) self.assertEqual(tracks, [])
def add_source_request(): user = request.args["user"] uri = request.args["listURI"] sinkName = request.args["sinkURI"] with playlist.open_drainlist(user, sinkName) as infile: dlist = playlist.Drainlist(user, infile) dlist.add_source_api(uri) dlist.write_out() dlist.cleanup(user) resp = app.make_response("success") resp.headers['Access-Control-Allow-Origin'] = '*' return resp
def refresh(user, token): """ Refreshes / updates all of a users drainlists The grandaddy of them all :param user: The user who we are updating :param token: The users token :return: None """ uris = [ f.replace("_drain", "") for f in os.listdir(user + "/Playlists/") if "_drain" in f ] for uri in uris: with playlist.open_drainlist(user, uri) as infile: d = playlist.Drainlist(user, infile) diff = d.sync() d.write_out() spio.add_tracks_to_drain(token, d, diff) d.cleanup(user)
def list_drains_request(): user = request.args["user"] drain_names = [ f for f in os.listdir(user + "/Playlists/") if f.endswith("_drain") ] drains = [] for drain in [d.replace("_drain", "") for d in drain_names]: with playlist.open_drainlist(user, drain) as infile: dlist = playlist.Drainlist(user, infile) drains.append(dlist) dlist.cleanup(user) resp = app.make_response( json.dumps([{ "Name": d.name, "URI": d.uri, "Sources": spio.print_sources(d.sources) } for d in drains])) resp.headers['Access-Control-Allow-Origin'] = '*' return resp
def test_cleanup(self): with playlist.open_drainlist(test_user, dname) as infile: d = playlist.Drainlist(test_user, infile) expected_files = [ 'spotify:playlist:4L3PeQ9LzinSq0Q3KnzLvb', 'spotify:playlist:6E2XjEeEOEhUKVoftRHusb', 'spotify:playlist:4L3PeQ9LzinSq0Q3KnzLvb_ref', 'spotify:playlist:069rrIb9s1MRw2BBwXmeJE_drain', 'spotify:playlist:6E2XjEeEOEhUKVoftRHusb_ref' ] self.assertEqual(set(os.listdir(test_user + "/Playlists/")), set(expected_files)) d.cleanup(test_user) expected_files = [ 'spotify:playlist:4L3PeQ9LzinSq0Q3KnzLvb_ref', 'spotify:playlist:069rrIb9s1MRw2BBwXmeJE_drain', 'spotify:playlist:6E2XjEeEOEhUKVoftRHusb_ref' ] self.assertEqual(set(os.listdir(test_user + "/Playlists/")), set(expected_files))
def test_sync(self): with playlist.open_drainlist(test_user, dname) as infile: d = playlist.Drainlist(test_user, infile) self.assertEqual(set(), d.sync())