Example #1
0
 public static function load($force = false)
 {
     set_time_limit(300);
     Debug::startTimer('Load remote data');
     $tracks = array();
     $first_page = self::loadPage(1);
     if (!$first_page) {
         return FALSE;
     }
     $total_pages = (int) $first_page['total_pages'];
     $tracks[1] = $first_page['tracks'];
     for ($i = 2; $i <= $total_pages; $i++) {
         $next_page = self::loadPage($i);
         if ($next_page) {
             $tracks[$i] = $next_page['tracks'];
         }
     }
     // Loop through tracks.
     $new_songs = array();
     foreach ($tracks as $page_num => $result) {
         foreach ((array) $result as $row) {
             $processed = External::processRemote($row);
             $processed['hash'] = Song::getSongHash($processed);
             $new_songs[$processed['hash']] = $processed;
         }
     }
     Debug::endTimer('Load remote data');
     return External::import($new_songs, $force);
 }
Example #2
0
 protected static function _querySearch($song)
 {
     $base_url = 'https://eqbeats.org/tracks/search/json';
     $url = $base_url . '?' . http_build_query(array('q' => $song->artist . ' ' . $song->title, 'client' => 'ponyvillelive'));
     Debug::log('Query Search: ' . $url);
     $result = file_get_contents($url);
     if ($result) {
         $rows = json_decode($result, TRUE);
         foreach ($rows as $row) {
             $song_hash = Song::getSongHash(array('artist' => $row['user']['name'], 'title' => $row['title']));
             if (strcmp($song_hash, $song->id) == 0) {
                 return $row;
             }
         }
     }
     return NULL;
 }
Example #3
0
 public static function load($force = false)
 {
     set_time_limit(300);
     Debug::startTimer('Load remote data');
     $remote_url = 'https://bronytunes.com/retrieve_songs.php?client_type=ponyvillelive';
     $result_raw = @file_get_contents($remote_url);
     Debug::endTimer('Load remote data');
     if ($result_raw) {
         $result = json_decode($result_raw, TRUE);
         $new_songs = array();
         foreach ((array) $result as $row) {
             $processed = External::processRemote($row);
             $processed['hash'] = Song::getSongHash($processed);
             $new_songs[$processed['hash']] = $processed;
         }
         return External::import($new_songs, $force);
     }
     return false;
 }
Example #4
0
 /**
  * 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;
 }
Example #5
0
 public static function sync()
 {
     if (DF_APPLICATION_ENV !== 'production') {
         return false;
     }
     $db = self::getDatabase();
     $em = self::getEntityManager();
     $settings = self::getSettings();
     // Force correct account settings (enable global unified request system).
     $account_values = array('allowrequests' => '1', 'autoqueuerequests' => '1', 'requestprobability' => '50', 'requestdelay' => '0', 'emailunknownrequests' => '0');
     $db->update('accounts', $account_values, array('expectedstate' => 'up'));
     // Clear out old logs.
     $threshold = strtotime('-1 month');
     $threshold_date = date('Y-m-d', $threshold) . ' 00:00:00';
     $db->executeQuery('DELETE FROM playbackstats_tracks WHERE endtime <= ?', array($threshold_date));
     $db->executeQuery('DELETE FROM visitorstats_sessions WHERE endtime <= ?', array($threshold_date));
     // Delete old requests still listed as pending.
     $requesttime = new \DateTime('NOW');
     $requesttime->modify('-3 hours');
     $requesttime->setTimezone(new \DateTimeZone($settings['timezone']));
     $threshold_requests = $requesttime->format('Y-m-d h:i:s');
     $db->executeQuery('DELETE FROM playlist_tracks_requests WHERE requesttime <= ?', array($threshold_requests));
     // Force playlist enabling for existing pending requests.
     $request_playlists_raw = $db->fetchAll('SELECT DISTINCT ptr.playlistid AS pid FROM playlist_tracks_requests AS ptr');
     foreach ($request_playlists_raw as $pl) {
         $pl_id = $pl['pid'];
         $db->update('playlists', array('status' => 'enabled'), array('id' => $pl_id));
     }
     // Preload all station media locally.
     $stations = $em->createQuery('SELECT s FROM Entity\\Station s WHERE s.requests_enabled = 1')->execute();
     foreach ($stations as $station) {
         $account_id = self::getStationID($station);
         if (!$account_id) {
             continue;
         }
         // Clear existing items.
         $existing_ids_raw = $em->createQuery('SELECT sm FROM Entity\\StationMedia sm WHERE sm.station_id = :station_id')->setParameter('station_id', $station->id)->execute();
         $existing_records = array();
         foreach ($existing_ids_raw as $row) {
             $existing_records[$row['id']] = $row;
         }
         // Find all tracks in active playlists.
         $new_records_raw = self::fetchTracks($station);
         $records_by_hash = array();
         foreach ($new_records_raw as $track_info) {
             if ($track_info['length'] < 60) {
                 continue;
             }
             if ($track_info['playlist_status'] !== 'enabled') {
                 continue;
             }
             $row = array('id' => $track_info['id'], 'title' => $track_info['title'], 'artist' => $track_info['artist'], 'album' => $track_info['album'], 'length' => $track_info['length']);
             $song_hash = Song::getSongHash(array('text' => $row['artist'] . ' - ' . $row['title'], 'artist' => $row['artist'], 'title' => $row['title']));
             $records_by_hash[$song_hash] = $row;
         }
         $new_records = array();
         foreach ($records_by_hash as $row) {
             $new_records[$row['id']] = $row;
         }
         // Reconcile differences.
         $existing_guids = array_keys($existing_records);
         $new_guids = array_keys($new_records);
         $guids_to_delete = array_diff($existing_guids, $new_guids);
         if ($guids_to_delete) {
             foreach ($guids_to_delete as $guid) {
                 $record = $existing_records[$guid];
                 $em->remove($record);
             }
         }
         $guids_to_add = array_diff($new_guids, $existing_guids);
         if ($guids_to_add) {
             foreach ($guids_to_add as $guid) {
                 $record = new StationMedia();
                 $record->station = $station;
                 $record->fromArray($new_records[$guid]);
                 $em->persist($record);
             }
         }
         $em->flush();
     }
     return true;
 }