public function searchAction() { if (!$this->hasParam('q')) { return $this->returnError('No query provided.'); } $q = trim($this->getParam('q')); $results_raw = $this->em->createQuery('SELECT s FROM Entity\\Song s WHERE (s.text LIKE :q OR s.id = :q_exact) ORDER BY s.text ASC')->setParameter('q', '%' . addcslashes($q, "%_") . '%')->setParameter('q_exact', $q)->setMaxResults(50)->getArrayResult(); $results = array(); foreach ($results_raw as $row) { $results[$row['id']] = Song::api($row); } return $this->returnSuccess($results); }
/** * Process a single audio stream's NowPlaying info. * * @param StationStream $stream * @param Station $station * @return array Structured NowPlaying Data */ public static function processAudioStream(StationStream $stream, Station $station, $force = false) { $current_np_data = (array) $stream->nowplaying_data; // Only process non-default streams on odd-numbered "segments" to improve performance. if (!$stream->is_default && !$force && NOWPLAYING_SEGMENT % 2 == 0 && !empty($current_np_data)) { return $current_np_data; } $np = StationStream::api($stream); $custom_class = Station::getStationClassName($station->name); $custom_adapter = '\\PVL\\RadioAdapter\\' . $custom_class; if (class_exists($custom_adapter)) { $np_adapter = new $custom_adapter($stream, $station); } elseif ($stream->type == "icecast") { $np_adapter = new \PVL\RadioAdapter\IceCast($stream, $station); } elseif ($stream->type == "icebreath") { $np_adapter = new \PVL\RadioAdapter\IceBreath($stream, $station); } elseif ($stream->type == "shoutcast2") { $np_adapter = new \PVL\RadioAdapter\ShoutCast2($stream, $station); } elseif ($stream->type == "shoutcast1") { $np_adapter = new \PVL\RadioAdapter\ShoutCast1($stream, $station); } else { return array(); } Debug::log('Adapter Class: ' . get_class($np_adapter)); $stream_np = $np_adapter->process(); $np = array_merge($np, $stream_np['meta']); $np['listeners'] = $stream_np['listeners']; // Pull from current NP data if song details haven't changed. $current_song_hash = Song::getSongHash($stream_np['current_song']); if (strcmp($current_song_hash, $current_np_data['current_song']['id']) == 0) { $np['current_song'] = $current_np_data['current_song']; $np['song_history'] = $current_np_data['song_history']; } else { if (empty($stream_np['current_song']['text'])) { $np['current_song'] = array(); $np['song_history'] = $station->getRecentHistory($stream); } else { // Register a new item in song history. $np['current_song'] = array(); $np['song_history'] = $station->getRecentHistory($stream); // Determine whether to log this song play for analytics. $log_radio_play = $stream->is_default && $station->category == 'audio'; $song_obj = Song::getOrCreate($stream_np['current_song'], $log_radio_play); $sh_obj = SongHistory::register($song_obj, $station, $stream, $np); // Compose "current_song" object for API. $current_song = Song::api($song_obj); $current_song['sh_id'] = $sh_obj->id; $current_song['score'] = SongVote::getScoreForStation($song_obj, $station); $vote_urls = array(); $vote_functions = array('like', 'dislike', 'clearvote'); foreach ($vote_functions as $vote_function) { $vote_urls[$vote_function] = \PVL\Url::api(array('module' => 'api', 'controller' => 'song', 'action' => $vote_function, 'sh_id' => $sh_obj->id)); } $current_song['vote_urls'] = $vote_urls; $external = $song_obj->getExternal(); if ($external) { $current_song['external'] = $song_obj->getExternal(); } $np['current_song'] = $current_song; } } $stream->nowplaying_data = $np; return $np; }
public function getRecentHistory(StationStream $stream, $num_entries = 5) { $em = self::getEntityManager(); $history = $em->createQuery('SELECT sh, s FROM Entity\\SongHistory sh JOIN sh.song s WHERE sh.station_id = :station_id AND sh.stream_id = :stream_id ORDER BY sh.id DESC')->setParameter('station_id', $this->id)->setParameter('stream_id', $stream->id)->setMaxResults($num_entries)->getArrayResult(); $return = array(); foreach ($history as $sh) { $history = array('played_at' => $sh['timestamp'], 'song' => Song::api($sh['song'])); $return[] = $history; } return $return; }