예제 #1
0
def main():
    options = parse_args()


    info('Fecthing threads from Reddit')
    raw = open('rap_pl.json')
    json_object = json.load(raw)
    entities = []
    for song in json_object:
        ss = song['artist']+" "+song['song'] + '['
        entities.append(Entity(ss))
                
    raw.close()
    info('Found {} threads'.format(len(entities)))
    
    for entity in entities:
        try:
            entity.search_term = search_term_from_title(entity.reddit_title)
        except Exception as e:
            error(e)
            error('Failed to convert Reddit title "{}" to a search term'.format(entity))

    refresh_token = read_refresh_token(options.refresh_token_file)

    try:
        s = Spotify(options.spotify_client_id, options.spotify_client_secret, refresh_token)
    except Exception as e:
        error('Failed to create Spotify agent')
        error(e)
        return 1

    info('Searching Spotify for tracks')
    for entity in entities:
        try:
            entity.spotify_track = s.search_track(entity.search_term)
        except Exception as e:
            error(e)
            error('Skipping...')

    # list to Set to list - done to dedupe
    tracks_found = list(Set([entity.spotify_track for entity in entities if entity.spotify_track is not None]))
    info('Found {} Spotify tracks'.format(len(tracks_found)))

    if not (float(len(tracks_found)) / len(entities)) > options.search_threshold:
        error('Search of Spotify tracks under threshold of {}'.format(options.search_threshold))
        return 1

    if options.dry_run == False:
        try:
            info('Removing existing tracks from playlist')
            s.clear_playlist(options.playlist_id)
            info('Adding {} new tracks to playlist'.format(len(tracks_found)))
            s.add_tracks_to_playlist(options.playlist_id, tracks_found)
        except Exception as e:
            error(e)
            return 1

    info('Run completed successfully')
    return 0
def main():
    options = parse_args()

    r = Reddit(options.subreddit)

    info('Fecthing threads from Reddit')
    top_titles = r.top(options.period, options.limit)
    entities = [Entity(title) for title in top_titles]
    info('Found {} threads'.format(len(entities)))
    
    for entity in entities:
        try:
            entity.search_term = search_term_from_title(entity.reddit_title)
        except:
            error('Failed to convert Reddit title "{}" to a search term'.format(title))

    refresh_token = read_refresh_token(options.refresh_token_file)

    try:
        s = Spotify(options.spotify_client_id, options.spotify_client_secret, refresh_token)
    except Exception as e:
        error('Failed to create Spotify agent')
        error(e)
        return 1

    info('Searching Spotify for tracks')
    for entity in entities:
        try:
            entity.spotify_track = s.search_track(entity.search_term)
        except Exception as e:
            error(e)
            error('Skipping...')

    # list to Set to list - done to dedupe
    tracks_found = list(Set([entity.spotify_track for entity in entities if entity.spotify_track is not None]))
    info('Found {} Spotify tracks'.format(len(tracks_found)))

    if not (float(len(tracks_found)) / len(entities)) > options.search_threshold:
        error('Search of Spotify tracks under threshold of {}'.format(options.search_threshold))
        return 1

    if options.dry_run == False:
        try:
            info('Removing existing tracks from playlist')
            s.clear_playlist(options.playlist_id)
            info('Adding {} new tracks to playlist'.format(len(tracks_found)))
            s.add_tracks_to_playlist(options.playlist_id, tracks_found)
        except Exception as e:
            error(e)
            return 1

    info('Run completed successfully')
    return 0
예제 #3
0
class SimpleTrackConverter(object):
    def __init__(self, sp=Spotify):
        self.sp = Spotify()

    def line_to_song(self, line):
        """
        Finds the tracks on Spotify that match the most words using the least
        amount of tracks.

        Parameters
        ----------
        line: str
            A line or segment of words

        Returns
        -------
        hit : Hit
            Best hit for the segment
        """
        par = partition(line.split())

        best_hit = Hit(0, [None], [line])
        for p in par:
            matched_words = 0
            sp_tracks = []

            # group in partition
            for n in p:
                sp_track = self.sp.search_track(" ".join(n))
                if sp_track:
                    matched_words += len(n)
                sp_tracks.append(sp_track)

            # store the hit only if it is better
            if matched_words > best_hit.match_count:
                best_hit = Hit(matched_words, sp_tracks,
                               [" ".join(n) for n in p])
            # take the one using the least amount of tracks if the number of
            # matched words is equal
            if matched_words == best_hit.match_count:
                if len(sp_tracks) < best_hit.sp_tracks:
                    best_hit = Hit(matched_words, sp_tracks,
                                   [" ".join(n) for n in p])

        return best_hit

    def poem_to_song(self, text):
        """
        Splits up the text at newlines, ',' '(' ')' and '-'.

        Parameters
        ----------
        text: str
            A poem of possibly multiple lines

        Returns
        -------
        hit : Hit
            Best hits for each segment of the text
        """
        sentences = split_multiple(text, ["\n", ",", "(", ")", "-"])
        logging.debug(sentences)
        hits = []
        for s in sentences:
            hits.append(self.line_to_song(s))
        return hits
예제 #4
0
class SimpleTrackConverter(object):
    def __init__(self, sp=Spotify):
        self.sp = Spotify()

    def line_to_song(self, line):
        """
        Finds the tracks on Spotify that match the most words using the least
        amount of tracks.

        Parameters
        ----------
        line: str
            A line or segment of words

        Returns
        -------
        hit : Hit
            Best hit for the segment
        """
        par = partition(line.split())

        best_hit = Hit(0, [None], [line])
        for p in par:
            matched_words = 0
            sp_tracks = []

            # group in partition
            for n in p:
                sp_track = self.sp.search_track(" ".join(n))
                if sp_track:
                    matched_words += len(n)
                sp_tracks.append(sp_track)

            # store the hit only if it is better
            if matched_words > best_hit.match_count:
                best_hit = Hit(matched_words, sp_tracks, [" ".join(n) for n in p])
            # take the one using the least amount of tracks if the number of
            # matched words is equal
            if matched_words == best_hit.match_count:
                if len(sp_tracks) < best_hit.sp_tracks:
                    best_hit = Hit(matched_words, sp_tracks, [" ".join(n) for n in p])

        return best_hit

    def poem_to_song(self, text):
        """
        Splits up the text at newlines, ',' '(' ')' and '-'.

        Parameters
        ----------
        text: str
            A poem of possibly multiple lines

        Returns
        -------
        hit : Hit
            Best hits for each segment of the text
        """
        sentences = split_multiple(text, ["\n", ",", "(", ")", "-"])
        logging.debug(sentences)
        hits = []
        for s in sentences:
            hits.append(self.line_to_song(s))
        return hits