Пример #1
0
    def remove_song_from_playlist(self, uid, pid, songid):
        """Function to remove song from playlist

        :param uid: user ID
        :type uid: string
        :param pid: playlist ID to remove song from
        :type pid: string
        :param songid: ID of song to remove
        :type songid: string
        :raises: RuntimeError

        """

        playlist_path = uid + "/" + pid

        util.check_if_git_playlist(self.git_dir, playlist_path)

        with open(self.git_dir + playlist_path + "/index.txt", "r+") as f:
            songIds = []
            found_song = False
            for line in f.readlines():
                line = line.strip()

                if songid == line:
                    found_song = True
                else:
                    songIds.append(line)

            if found_song == False:
                raise RuntimeError("playlist does not have song.")

            # go to the start of the text file
            f.seek(0)

            for ID in songIds:
                print(ID, file=f)

            # ignore the rest of the text file (parts that were already there)
            f.truncate()

        repo = pygit2.Repository(self.git_dir + playlist_path)

        # create the file blob
        file_blob = repo.create_blob_fromdisk(
            self.git_dir + playlist_path + "/index.txt")

        new_tree = repo.TreeBuilder()

        # insert it into the tree
        new_tree.insert("index.txt", file_blob,
                        os.stat(self.git_dir + playlist_path + "/index.txt").st_mode)

        new_tree.write()
Пример #2
0
    def pull_spotify_playlist(self, uid, pid):
        """Function to pull playlist from Spotify

        :param uid: user ID
        :type uid: string
        :param pid: playlist ID
        :type pid: string
        :returns: string -- stating status of pull (either successfull or not)

        """

        playlist_path = uid + "/" + pid

        util.check_if_git_playlist(self.git_dir, playlist_path)

        # grab tracks from spotify from pid
        results = self.sp.user_playlist_tracks(self.username, pid)
        results = results["items"]

        # get just a list of the track ids from the response
        remote_tracks = []
        for track in results:
            if track["track"]["id"] != None:  # only take spotify tracks
                remote_tracks.append(track["track"]["id"])

        # get local track ids
        with open(self.git_dir + playlist_path + "/index.txt") as f:
            local_tracks = f.read().splitlines()

        # merge tracks by adding if not added already. local takes precendence
        # does not preserve position of new remote tracks
        diff = False
        for remoteTrack in remote_tracks:
            if remoteTrack not in local_tracks:
                local_tracks.append(remoteTrack)
                diff = True

        # write tracks back to file
        with open(self.git_dir + playlist_path + "/index.txt", "w") as f:
            for track in local_tracks:
                print(track, file=f)

        # commit playlist changes if needed
        if diff:
            self.commit_changes_to_playlist(uid, pid)
            return 'Added and committed changes from remote.'
        return 'No changes committed, up to date with remote.'
Пример #3
0
    def push_spotify_playlist(self, uid, pid):
        """Function to push playlist to Spotify

        :param uid: user ID
        :type uid: string
        :param pid: playlist ID
        :type pid: string
        :returns: string -- stating status of pull (either successfull or not)

        """

        playlist_path = uid + "/" + pid

        util.check_if_git_playlist(self.git_dir, playlist_path)

        # grab tracks from spotify from pid
        results = self.sp.user_playlist_tracks(self.username, pid)
        results = results["items"]

        # get just a list of the track ids from the response
        remote_tracks = []
        for track in results:
            if track["track"]["id"] != None:  # only take spotify tracks
                remote_tracks.append(track["track"]["id"])

               # get local track ids
        with open(self.git_dir + playlist_path + "/index.txt") as f:
            local_tracks = f.read().splitlines()

        # merge tracks by adding if not added already. local takes precendence
        # does not preserve position of new remote tracks
        diff = False
        for remote_track in remote_tracks:
            if remote_track not in local_tracks:
                diff = True
                self.sp.user_playlist_remove_all_occurrences_of_tracks(
                    self.username, pid, [remote_track])

        for local_track in local_tracks:
            if local_track not in remote_tracks:
                diff = True
                self.sp.user_playlist_add_tracks(self.username, pid,
                                                 [local_track])
        if diff:
            return "Added and updated changes to the remote."
        return "No changes updated to the remote, remote and local are the same"
Пример #4
0
    def add_song_to_playlist(self, uid, pid, songid):
        """Function to add song to playlist

        :param uid: user ID
        :type uid: string
        :param pid: playlist ID to add song to
        :type pid: string
        :param songid: ID of song to add
        :type songid: string
        :raises: RuntimeError

        """

        playlist_path = os.path.join(uid, pid)

        util.check_if_git_playlist(self.git_dir, playlist_path)

        with open(self.git_dir + playlist_path + "/index.txt", "r+") as f:
            songIds = []
            for line in f.readlines():
                line = line.strip()
                songIds.append(line)

                if songid == line:
                    raise RuntimeError("Song is already in playlist")
            print(songid, file=f)

        # get the repo
        repo = pygit2.Repository(self.git_dir + playlist_path)

        # create a new blob for our new index
        file_blob = repo.create_blob_fromdisk(
            self.git_dir + playlist_path + "/index.txt")

        # build the tree
        new_tree = repo.TreeBuilder()

        # add the index file
        new_tree.insert("index.txt", file_blob,
                        os.stat(self.git_dir + playlist_path + "/index.txt").st_mode)

        new_tree.write()
Пример #5
0
    def commit_changes_to_playlist(self, uid, pid):
        """Function to commit changes to playlist

        :param uid: user ID
        :type uid: string
        :param pid: playlist ID
        :type pid: string

        """

        playlist_path = uid + "/" + pid

        util.check_if_git_playlist(self.git_dir, playlist_path)

        # get the repo
        repo = pygit2.Repository(self.git_dir + playlist_path)

        # create the file blob
        file_blob = repo.create_blob_fromdisk(
            self.git_dir + playlist_path + "/index.txt")

        new_tree = repo.TreeBuilder()

        # insert it into the tree
        new_tree.insert("index.txt", file_blob,
                        os.stat(self.git_dir + playlist_path + "/index.txt").st_mode)

        tree = new_tree.write()

        # add to commit
        repo.index.read()
        repo.index.add("index.txt")
        repo.index.write()

        # commit changes to playlist
        repo.create_commit("HEAD", self.author, self.comitter,
                           "Changes committed to " + playlist_path, tree, [repo.head.target])