예제 #1
0
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
    })
예제 #2
0
    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))
예제 #3
0
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
예제 #4
0
    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'})
예제 #5
0
 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])
예제 #6
0
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
예제 #7
0
 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)))
예제 #8
0
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)
예제 #9
0
    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]
예제 #10
0
def initialize():
    try:
        spio.get_access_token(user)
        return redirect(auth_completed_url)
    except Exception as e:
        return redirect(spio.get_new_tokens().url)
예제 #11
0
 def test_get_name(self):
     token = spio.get_access_token(test_user)
     n = spio.get_name(token, "spotify:playlist:069rrIb9s1MRw2BBwXmeJE")
     self.assertEqual(n, "Default")
예제 #12
0
    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))