Exemplo n.º 1
0
def create_channel_and_get_first_video(artist_name):
    """
    Constructs a new channel from a given seed artist, and returns first video
    that should be displayed
    """
    # Take care of the channel stuff
    channel = create_channel(artist_name)
    playlist_info = echonest.start_dynamic_playlist(artist_name)
    
    channel.echonest_session_id = playlist_info['session_id']
    channel.save()
    
    rval = {}
    rval['channel'] = channel        
    
    # Get the video!
    videos = youtube.find_music_videos(playlist_info['artist_name'], playlist_info['song_title'], 1)
    if videos is None:
        for _ in xrange(MAX_FIRST_VIDEO_RETRY_SEARCH_ATTEMPT):
            song_info = echonest.get_next_song_dynamic_playlist(channel.echonest_session_id, channel.seed_artist)
            videos = youtube.find_music_videos(song_info['artist_name'], song_info['song_title'], 1)
            if videos is not None:
                # We're done here, move along...
                break
            time.sleep(RETRY_WAIT_TIME)
    
    if videos != None:
        rval['video_info'] = videos[0]
    rval['artist'] = playlist_info['artist_name']
    rval['song'] = playlist_info['song_title']
    
    return rval
Exemplo n.º 2
0
def search_and_create_video_next_on_playlist(channel_hash):
    """
    Searches for a video corresponding to next song on channel's playlist
    from echonest, and then save to persistence layer. Video object is then returned
    """
    video = None
    
    # Get channel
    c = channel.retrieve(channel_hash)        
        
    for _ in xrange(MAX_PLAYLIST_VIDEO_ATTEMPT):
        # Get next song (potential next video) from playlist
        song_info = None                        
        for _ in xrange(MAX_SONG_INFO_RETRY):
            song_info = echonest.get_next_song_dynamic_playlist(c.echonest_session_id, c.seed_artist)
            
            if song_info is None:
                # Something messed up with the echonest playlist, try restart
                time.sleep(SONG_RETRY_TIME)                            
                song_info = echonest.restart_dynamic_playlist(c.seed_artist)
                
                if song_info is not None:
                    c.echonest_session_id = song_info['session_id']
                    c.save()
                    break                
            else:
                # Found a song
                break 
                           
        # Find a video        
        video = search_and_create_video(song_info['artist_name'], song_info['song_title'], channel_hash)
        
        if video is not None:
            return video
        else:
            time.sleep(PLAYLIST_RETRY_TIME)
            
    return None