def ajax_get_winner(request): """" Closes out current round, and gets next video to play (i.e. winner of votes) and returns winning song data as JSON """ if request.is_ajax(): # Get arguments channel_hash_code = request.GET.get('code') try: data = {} # Pause voting while we update things c = channel.retrieve(channel_hash_code) c.is_accepting_votes = False c.save() # Get winner winning_video = voting.get_winner(channel_hash_code) data['video_url'] = winning_video.video_url data['artist'] = winning_video.artist_name data['song'] = winning_video.song_name # Delete last round/Setup next round cvr = voting.create_round(channel_hash_code) # Update channel c.is_accepting_votes = True c.last_song_play_date = datetime.now() c.current_round = cvr c.save() data['success'] = True except: data['success'] = False # Render response return HttpResponse(json.dumps(data), mimetype='application/json') else: # Should only be accessed in an ajax way, otherwise this request is # whack and throwing that shit back return HttpResponseRedirect("/")
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