def fill(self, target_song_length=None, skip_requests=False): # ONLY RUN _ADD_REQUESTS ONCE PER FILL if not skip_requests: self._add_requests() for i in range(len(self.songs), self._num_songs): #pylint: disable=W0612 try: if not target_song_length and len( self.songs) > 0 and 'length' in self.songs[0].data: target_song_length = self.songs[0].data['length'] log.debug( "elec_fill", "Second song in election, aligning to length %s" % target_song_length) song = self._fill_get_song(target_song_length) song.data['entry_votes'] = 0 song.data['entry_type'] = ElecSongTypes.normal song.data['elec_request_user_id'] = 0 song.data['elec_request_username'] = None self._check_song_for_conflict(song) self.add_song(song) except Exception as e: log.exception("elec_fill", "Song failed to fill in an election.", e) if len(self.songs) == 0: raise ElectionEmptyException for song in self.songs: if 'elec_request_user_id' in song.data and song.data[ 'elec_request_user_id']: log.debug( "elec_fill", "Putting user %s back in line after request fulfillment." % song.data['elec_request_username']) u = User(song.data['elec_request_user_id']) u.put_in_request_line(u.get_tuned_in_sid()) request.update_line(self.sid)
def add_song(self, song): if not song: return False entry_id = db.c.get_next_id("r4_election_entries", "entry_id") song.data["entry_id"] = entry_id song.data["entry_position"] = len(self.songs) if not "entry_type" in song.data: song.data["entry_type"] = ElecSongTypes.normal if not "entry_votes" in song.data: song.data["entry_votes"] = 0 db.c.update( "INSERT INTO r4_election_entries (entry_id, song_id, elec_id, entry_position, entry_type, entry_votes) VALUES (%s, %s, %s, %s, %s, %s)", ( entry_id, song.id, self.id, len(self.songs), song.data["entry_type"], song.data["entry_votes"], ), ) song.start_election_block( self.sid, config.get_station(self.sid, "num_planned_elections") + 1) self.songs.append(song) if song.data["entry_type"] == ElecSongTypes.request: request.update_line(self.sid) return True
def fill(self, target_song_length = None, skip_requests = False): # ONLY RUN _ADD_REQUESTS ONCE PER FILL if not skip_requests: self._add_requests() for i in range(len(self.songs), self._num_songs): #pylint: disable=W0612 try: if not target_song_length and len(self.songs) > 0 and 'length' in self.songs[0].data: target_song_length = self.songs[0].data['length'] log.debug("elec_fill", "Second song in election, aligning to length %s" % target_song_length) song = self._fill_get_song(target_song_length) song.data['entry_votes'] = 0 song.data['entry_type'] = ElecSongTypes.normal song.data['elec_request_user_id'] = 0 song.data['elec_request_username'] = None self._check_song_for_conflict(song) self.add_song(song) except Exception as e: log.exception("elec_fill", "Song failed to fill in an election.", e) if len(self.songs) == 0: raise ElectionEmptyException for song in self.songs: if 'elec_request_user_id' in song.data and song.data['elec_request_user_id']: log.debug("elec_fill", "Putting user %s back in line after request fulfillment." % song.data['elec_request_username']) u = User(song.data['elec_request_user_id']) u.put_in_request_line(u.get_tuned_in_sid()) request.update_line(self.sid)
def add_song(self, song): if not song: return False entry_id = db.c.get_next_id("r4_election_entries", "entry_id") song.data['entry_id'] = entry_id song.data['entry_position'] = len(self.songs) if not 'entry_type' in song.data: song.data['entry_type'] = ElecSongTypes.normal db.c.update("INSERT INTO r4_election_entries (entry_id, song_id, elec_id, entry_position, entry_type) VALUES (%s, %s, %s, %s, %s)", (entry_id, song.id, self.id, len(self.songs), song.data['entry_type'])) song.start_election_block(self.sid, config.get_station(self.sid, "num_planned_elections") + 1) self.songs.append(song) if song.data['entry_type'] == ElecSongTypes.request: request.update_line(self.sid) return True
def _update_memcache(sid): cache.set_station(sid, "sched_current", current[sid], True) cache.set_station(sid, "sched_next", next[sid], True) cache.set_station(sid, "sched_history", history[sid], True) cache.set_station(sid, "sched_current_dict", current[sid].to_dict(), True) next_dict_list = [] for event in next[sid]: next_dict_list.append(event.to_dict()) cache.set_station(sid, "sched_next_dict", next_dict_list, True) history_dict_list = [] for event in history[sid]: history_dict_list.append(event.to_dict()) cache.set_station(sid, "sched_history_dict", history_dict_list, True) cache.prime_rating_cache_for_events([ current[sid] ] + next[sid], history[sid]) cache.set_station(sid, "listeners_current", listeners.get_listeners_dict(sid), True) cache.set_station(sid, "album_diff", playlist.get_updated_albums_dict(sid), True) request.update_line(sid)
def post_process(sid): start_time = time.time() set_next_start_times(sid) request.update_line(sid) playlist.reduce_song_blocks(sid) _create_elections(sid) _add_listener_count_record(sid) cache.update_user_rating_acl(sid, history[sid][0].get_song().id) _trim(sid) user.trim_listeners(sid) user.unlock_listeners(sid) playlist.warm_cooled_songs(sid) playlist.warm_cooled_albums(sid) _update_memcache(sid) sync_to_front.sync_frontend_all(sid) log.debug("post", "Post-processing prepare time: %.6f" % (time.time() - start_time,))
def post_process(sid): start_time = time.time() set_next_start_times(sid) request.update_line(sid) playlist.reduce_song_blocks(sid) _create_elections(sid) _add_listener_count_record(sid) cache.update_user_rating_acl(sid, history[sid][0].get_song().id) _trim(sid) user.trim_listeners(sid) user.unlock_listeners(sid) playlist.warm_cooled_songs(sid) playlist.warm_cooled_albums(sid) _update_memcache(sid) sync_to_front.sync_frontend_all(sid) log.debug( "post", "Post-processing prepare time: %.6f" % (time.time() - start_time, ))
def post_process(sid): try: db.c.start_transaction() start_time = time.time() playlist.prepare_cooldown_algorithm(sid) rainwave.playlist_objects.album.clear_updated_albums(sid) log.debug("post", "Playlist prepare time: %.6f" % (time.time() - start_time,)) start_time = time.time() current[sid].finish() log.debug("post", "Current finish time: %.6f" % (time.time() - start_time,)) start_time = time.time() last_song = current[sid].get_song() if last_song: db.c.update("INSERT INTO r4_song_history (sid, song_id) VALUES (%s, %s)", (sid, last_song.id)) log.debug("post", "Last song insertion time: %s" % (time.time() - start_time,)) start_time = time.time() history[sid].insert(0, current[sid]) while len(history[sid]) > 5: history[sid].pop() log.debug("post", "History management: %.6f" % (time.time() - start_time,)) start_time = time.time() current[sid] = upnext[sid].pop(0) current[sid].start_event() log.debug("advance", "Current management: %.6f" % (time.time() - start_time,)) start_time = time.time() playlist.warm_cooled_songs(sid) playlist.warm_cooled_albums(sid) log.debug("advance", "Cooldown warming: %.6f" % (time.time() - start_time,)) start_time = time.time() _add_listener_count_record(sid) _trim(sid) user.trim_listeners(sid) cache.update_user_rating_acl(sid, history[sid][0].get_song().id) user.unlock_listeners(sid) log.debug("advance", "User management and trimming: %.6f" % (time.time() - start_time,)) start_time = time.time() # reduce song blocks has to come first, otherwise it wll reduce blocks generated by _create_elections playlist.reduce_song_blocks(sid) # update_cache updates both the line and expiry times # this is expensive and not necessary to do more than once # DO THIS AFTER EVERYTHING ELSE, RIGHT BEFORE NEXT MANAGEMENT, OR PEOPLE'S REQUESTS SLIP THROUGH THE CRACKS request.update_line(sid) # add to the event list / update start times for events manage_next(sid) # update expire times AFTER manage_next, so people who aren't in line anymore don't see expiry times request.update_expire_times() log.debug("advance", "Request and upnext management: %.6f" % (time.time() - start_time,)) update_memcache(sid) sync_to_front.sync_frontend_all(sid) db.c.commit() except: db.c.rollback() raise
def post_process(sid): try: db.c.start_transaction() start_time = timestamp() playlist.prepare_cooldown_algorithm(sid) rainwave.playlist_objects.album.clear_updated_albums(sid) log.debug("post", "Playlist prepare time: %.6f" % (timestamp() - start_time,)) start_time = timestamp() current[sid].finish() for sched_id in db.c.fetch_list( "SELECT sched_id FROM r4_schedule WHERE sched_end < %s AND sched_used = FALSE", (timestamp(),), ): t_evt = events.event.BaseProducer.load_producer_by_id(sched_id) t_evt.finish() log.debug("post", "Current finish time: %.6f" % (timestamp() - start_time,)) start_time = timestamp() last_song = current[sid].get_song() if last_song: db.c.update( "INSERT INTO r4_song_history (sid, song_id) VALUES (%s, %s)", (sid, last_song.id), ) log.debug("post", "Last song insertion time: %s" % (timestamp() - start_time,)) start_time = timestamp() history[sid].insert(0, current[sid]) while len(history[sid]) > 5: history[sid].pop() log.debug("post", "History management: %.6f" % (timestamp() - start_time,)) start_time = timestamp() current[sid] = upnext[sid].pop(0) current[sid].start_event() log.debug("advance", "Current management: %.6f" % (timestamp() - start_time,)) start_time = timestamp() playlist.warm_cooled_songs(sid) playlist.warm_cooled_albums(sid) log.debug("advance", "Cooldown warming: %.6f" % (timestamp() - start_time,)) start_time = timestamp() _trim(sid) user.trim_listeners(sid) cache.update_user_rating_acl(sid, history[sid][0].get_song().id) user.unlock_listeners(sid) db.c.update( "UPDATE r4_listeners SET listener_voted_entry = NULL WHERE sid = %s", (sid,) ) log.debug( "advance", "User management and trimming: %.6f" % (timestamp() - start_time,), ) start_time = timestamp() # reduce song blocks has to come first, otherwise it wll reduce blocks generated by _create_elections playlist.reduce_song_blocks(sid) # update_cache updates both the line and expiry times # this is expensive and must be done before and after every request is filled # DO THIS AFTER EVERYTHING ELSE, RIGHT BEFORE NEXT MANAGEMENT, OR PEOPLE'S REQUESTS SLIP THROUGH THE CRACKS request.update_line(sid) # add to the event list / update start times for events manage_next(sid) # update expire times AFTER manage_next, so people who aren't in line anymore don't see expiry times request.update_expire_times() log.debug( "advance", "Request and upnext management: %.6f" % (timestamp() - start_time,), ) update_memcache(sid) sync_to_front.sync_frontend_all(sid) db.c.commit() except: db.c.rollback() raise if ( current[sid] and config.has_station(sid, "tunein_partner_key") and config.get_station(sid, "tunein_partner_key") ): ti_song = current[sid].get_song() if ti_song: ti_title = ti_song.data["title"] ti_album = ti_song.albums[0].data["name"] ti_artist = ", ".join([a.data["name"] for a in ti_song.artists]) params = { "id": config.get_station(sid, "tunein_id"), "title": ti_title, "artist": ti_artist, "album": ti_album, } try: req = requests.Request( "GET", "http://air.radiotime.com/Playing.ashx", params=params ) p = req.prepare() # Must be done here rather than in params because of odd strings TuneIn creates p.url += "&partnerId=%s" % config.get_station(sid, "tunein_partner_id") p.url += "&partnerKey=%s" % config.get_station( sid, "tunein_partner_key" ) s = requests.Session() resp = s.send(p, timeout=3) log.debug( "advance", "TuneIn updated (%s): %s" % (resp.status_code, resp.text) ) except Exception as e: log.exception("advance", "Could not update TuneIn.", e)
def post_process(sid): try: db.c.start_transaction() start_time = timestamp() playlist.prepare_cooldown_algorithm(sid) rainwave.playlist_objects.album.clear_updated_albums(sid) log.debug("post", "Playlist prepare time: %.6f" % (timestamp() - start_time,)) start_time = timestamp() current[sid].finish() for sched_id in db.c.fetch_list( "SELECT sched_id FROM r4_schedule WHERE sched_end < %s AND sched_used = FALSE", (timestamp(),) ): t_evt = events.event.BaseProducer.load_producer_by_id(sched_id) t_evt.finish() log.debug("post", "Current finish time: %.6f" % (timestamp() - start_time,)) start_time = timestamp() last_song = current[sid].get_song() if last_song: db.c.update("INSERT INTO r4_song_history (sid, song_id) VALUES (%s, %s)", (sid, last_song.id)) log.debug("post", "Last song insertion time: %s" % (timestamp() - start_time,)) start_time = timestamp() history[sid].insert(0, current[sid]) while len(history[sid]) > 5: history[sid].pop() log.debug("post", "History management: %.6f" % (timestamp() - start_time,)) start_time = timestamp() current[sid] = upnext[sid].pop(0) current[sid].start_event() log.debug("advance", "Current management: %.6f" % (timestamp() - start_time,)) start_time = timestamp() playlist.warm_cooled_songs(sid) playlist.warm_cooled_albums(sid) log.debug("advance", "Cooldown warming: %.6f" % (timestamp() - start_time,)) start_time = timestamp() _add_listener_count_record(sid) _trim(sid) user.trim_listeners(sid) cache.update_user_rating_acl(sid, history[sid][0].get_song().id) user.unlock_listeners(sid) db.c.update("UPDATE r4_listeners SET listener_voted_entry = NULL WHERE sid = %s", (sid,)) log.debug("advance", "User management and trimming: %.6f" % (timestamp() - start_time,)) start_time = timestamp() # reduce song blocks has to come first, otherwise it wll reduce blocks generated by _create_elections playlist.reduce_song_blocks(sid) # update_cache updates both the line and expiry times # this is expensive and must be done before and after every request is filled # DO THIS AFTER EVERYTHING ELSE, RIGHT BEFORE NEXT MANAGEMENT, OR PEOPLE'S REQUESTS SLIP THROUGH THE CRACKS request.update_line(sid) # add to the event list / update start times for events manage_next(sid) # update expire times AFTER manage_next, so people who aren't in line anymore don't see expiry times request.update_expire_times() log.debug("advance", "Request and upnext management: %.6f" % (timestamp() - start_time,)) update_memcache(sid) sync_to_front.sync_frontend_all(sid) db.c.commit() except: db.c.rollback() raise if current[sid] and config.has_station(sid, "tunein_partner_key") and config.get_station(sid, "tunein_partner_key"): ti_song = current[sid].get_song() if ti_song: ti_title = ti_song.data["title"] ti_album = ti_song.albums[0].data["name"] ti_artist = ", ".join([a.data["name"] for a in ti_song.artists]) params = { "id": config.get_station(sid, "tunein_id"), "title": ti_title, "artist": ti_artist, "album": ti_album, } try: req = requests.Request("GET", "http://air.radiotime.com/Playing.ashx", params=params) p = req.prepare() # Must be done here rather than in params because of odd strings TuneIn creates p.url += "&partnerId=%s" % config.get_station(sid, "tunein_partner_id") p.url += "&partnerKey=%s" % config.get_station(sid, "tunein_partner_key") s = requests.Session() resp = s.send(p, timeout=3) log.debug("advance", "TuneIn updated (%s): %s" % (resp.status_code, resp.text)) except Exception as e: log.exception("advance", "Could not update TuneIn.", e)