parameters: (sid, target_seconds=None, target_delta=None)
docstring: Fetch a random song abiding by all election block, request block, and
  	availability rules, but giving priority to the target song length
  	provided.  Falls back to get_random_song on failure.
docstring_summary: Fetch a random song abiding by all election block, request block, and
  	availability rules, but giving priority to the target song length
  	provided.  Falls back to get_random_song on failure.
docstring_tokens: Fetch a random song abiding by all election block request block and availability rules but giving priority to the target song length provided . Falls back to get_random_song on failure .
function: def get_random_song_timed(sid, target_seconds=None, target_delta=None):
      """
  	Fetch a random song abiding by all election block, request block, and
  	availability rules, but giving priority to the target song length
  	provided.  Falls back to get_random_song on failure.
  	"""
      if not target_seconds:
          return get_random_song(sid)
      if not target_delta:
          target_delta = config.get_station(sid, "song_lookup_length_delta")
  
      sql_query = (
          "FROM r4_song_sid "
          "JOIN r4_songs USING (song_id) "
          "JOIN r4_album_sid ON (r4_album_sid.album_id = r4_songs.album_id AND r4_album_sid.sid = r4_song_sid.sid) "
          "WHERE r4_song_sid.sid = %s "
          "AND song_exists = TRUE "
          "AND song_cool = FALSE "
          "AND song_elec_blocked = FALSE "
          "AND album_requests_pending IS NULL "
          "AND song_request_only = FALSE "
          "AND song_length >= %s AND song_length <= %s"
      )
      lower_target_bound = target_seconds - (target_delta / 2)
      upper_target_bound = target_seconds + (target_delta / 2)
      num_available = db.c.fetch_var(
          "SELECT COUNT(r4_song_sid.song_id) " + sql_query,
          (sid, lower_target_bound, upper_target_bound),
      )
      log.info(
          "song_select",
          "Song pool size (cooldown, blocks, requests, timed) [target %s delta %s]: %s"
          % (target_seconds, target_delta, num_available),
      )
      if num_available == 0:
          log.warn(
              "song_select",
              "No songs available with target_seconds %s and target_delta %s."
              % (target_seconds, target_delta),
          )
          log.debug(
              "song_select",
              "Song select query: SELECT COUNT(r4_song_sid.song_id) "
              + sql_query % (sid, lower_target_bound, upper_target_bound),
          )
          return get_random_song(sid)
      else:
          offset = random.randint(1, num_available) - 1
          song_id = db.c.fetch_var(
              "SELECT r4_song_sid.song_id " + sql_query + " LIMIT 1 OFFSET %s",
              (sid, lower_target_bound, upper_target_bound, offset),
          )
          return Song.load_from_id(song_id, sid)
function_tokens: def get_random_song_timed ( sid , target_seconds = None , target_delta = None ) : if not target_seconds : return get_random_song ( sid ) if not target_delta : target_delta = config . get_station ( sid , "song_lookup_length_delta" ) sql_query = ( "FROM r4_song_sid " "JOIN r4_songs USING (song_id) " "JOIN r4_album_sid ON (r4_album_sid.album_id = r4_songs.album_id AND r4_album_sid.sid = r4_song_sid.sid) " "WHERE r4_song_sid.sid = %s " "AND song_exists = TRUE " "AND song_cool = FALSE " "AND song_elec_blocked = FALSE " "AND album_requests_pending IS NULL " "AND song_request_only = FALSE " "AND song_length >= %s AND song_length <= %s" ) lower_target_bound = target_seconds - ( target_delta / 2 ) upper_target_bound = target_seconds + ( target_delta / 2 ) num_available = db . c . fetch_var ( "SELECT COUNT(r4_song_sid.song_id) " + sql_query , ( sid , lower_target_bound , upper_target_bound ) , ) log . info ( "song_select" , "Song pool size (cooldown, blocks, requests, timed) [target %s delta %s]: %s" % ( target_seconds , target_delta , num_available ) , ) if num_available == 0 : log . warn ( "song_select" , "No songs available with target_seconds %s and target_delta %s." % ( target_seconds , target_delta ) , ) log . debug ( "song_select" , "Song select query: SELECT COUNT(r4_song_sid.song_id) " + sql_query % ( sid , lower_target_bound , upper_target_bound ) , ) return get_random_song ( sid ) else : offset = random . randint ( 1 , num_available ) - 1 song_id = db . c . fetch_var ( "SELECT r4_song_sid.song_id " + sql_query + " LIMIT 1 OFFSET %s" , ( sid , lower_target_bound , upper_target_bound , offset ) , ) return Song . load_from_id ( song_id , sid )