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
 public static function import($new_songs, $force = false)
 {
     $db_stats = array('skipped' => 0, 'updated' => 0, 'inserted' => 0, 'deleted' => 0);
     if (empty($new_songs)) {
         return false;
     }
     Debug::startTimer('Import data into database');
     $em = self::getEntityManager();
     $existing_hashes = self::getHashes();
     $existing_ids = self::getIds();
     $unused_hashes = $existing_hashes;
     $song_ids = Song::getIds();
     $i = 0;
     foreach ($new_songs as $song_hash => $processed) {
         if (!in_array($song_hash, $song_ids)) {
             Song::getOrCreate($processed);
         }
         if (isset($existing_hashes[$song_hash])) {
             if ($force && $existing_hashes[$song_hash] == $processed['id']) {
                 $db_stats['updated']++;
                 $record = self::find($processed['id']);
             } else {
                 $db_stats['skipped']++;
                 $record = null;
             }
         } else {
             if (isset($existing_ids[$processed['id']])) {
                 $db_stats['updated']++;
                 $record = self::find($processed['id']);
             } else {
                 $db_stats['inserted']++;
                 $record = new self();
             }
         }
         if ($record instanceof self) {
             $existing_ids[$processed['id']] = $processed['hash'];
             $existing_hashes[$processed['hash']] = $processed['id'];
             $record->fromArray($processed);
             $em->persist($record);
         }
         unset($unused_hashes[$song_hash]);
         $i++;
         if ($i % 200 == 0) {
             $em->flush();
             $em->clear();
         }
     }
     $em->flush();
     $em->clear();
     // Clear out any songs not found.
     $hashes_remaining = array_keys($unused_hashes);
     $db_stats['deleted'] = count($hashes_remaining);
     $em->createQuery('DELETE FROM ' . __CLASS__ . ' e WHERE e.hash IN (:hashes)')->setParameter('hashes', $hashes_remaining)->execute();
     Debug::endTimer('Import data into database');
     Debug::print_r($db_stats);
     return $db_stats;
 }
Example #3
0
 public static function run()
 {
     // Pull podcast news.
     $em = self::getEntityManager();
     $all_podcasts = $em->createQuery('SELECT p, ps, pe FROM Entity\\Podcast p
         JOIN p.sources ps
         JOIN ps.episodes pe
         WHERE p.is_approved = 1
         AND ps.is_active = 1')->execute();
     foreach ($all_podcasts as $record) {
         Debug::startTimer('Process podcast: ' . $record->name);
         self::processPodcast($record);
         Debug::endTimer('Process podcast: ' . $record->name);
     }
     return true;
 }
Example #4
0
 public static function loadNowPlaying()
 {
     Debug::startTimer('Nowplaying Overall');
     $em = self::getEntityManager();
     $stations = Station::fetchAll();
     $nowplaying = array('legacy' => array(), 'api' => array());
     foreach ($stations as $station) {
         Debug::startTimer($station->name);
         $name = $station->short_name;
         $nowplaying['api'][$name] = self::processStation($station);
         $nowplaying['legacy'][$name] = self::processLegacy($nowplaying['api'][$name]);
         Debug::endTimer($station->name);
         Debug::divider();
     }
     Debug::endTimer('Nowplaying Overall');
     return $nowplaying;
 }
Example #5
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 #6
0
 public static function load($force = false)
 {
     set_time_limit(300);
     Debug::startTimer('Load remote data');
     $new_songs = array();
     for ($i = 1; $i <= 200; $i++) {
         $page_tracks = self::loadPage($i);
         if (count($page_tracks) == 0) {
             break;
         }
         foreach ((array) $page_tracks 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 #7
0
 public function syncAction()
 {
     $this->acl->checkPermission('administer all');
     $this->doNotRender();
     \PVL\Debug::setEchoMode(TRUE);
     \PVL\Debug::startTimer('sync_task');
     $type = $this->getParam('type', 'nowplaying');
     switch ($type) {
         case "long":
             \PVL\SyncManager::syncLong();
             break;
         case "medium":
             \PVL\SyncManager::syncMedium();
             break;
         case "short":
             \PVL\SyncManager::syncShort();
             break;
         case "nowplaying":
         default:
             $segment = $this->getParam('segment', 1);
             define('NOWPLAYING_SEGMENT', $segment);
             \PVL\SyncManager::syncNowplaying(true);
             break;
     }
     \PVL\Debug::endTimer('sync_task');
     \PVL\Debug::log('Sync task complete. See log above.');
 }
Example #8
0
 /**
  * Submit a URL request with a specified cache lifetime.
  *
  * @param null $c_opts
  * @param int $cache_time
  * @return string
  */
 public static function request($c_opts = null)
 {
     // Compose cURL configuration array.
     if (is_null($c_opts)) {
         $c_opts = array();
     } elseif (!is_array($c_opts)) {
         $c_opts = array('url' => $c_opts);
     }
     $c_defaults = array('method' => 'GET', 'useragent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2', 'timeout' => 10);
     $c_opts = array_merge($c_defaults, $c_opts);
     Debug::log('cURL Outgoing Request: ' . $c_opts['url']);
     Debug::startTimer('Make cURL Request');
     $postfields = false;
     if (!empty($c_opts['params'])) {
         if (strtoupper($c_opts['method']) == 'POST') {
             $postfields = $c_opts['params'];
         } else {
             $c_opts['url'] = $c_opts['url'] . '?' . http_build_query($c_opts['params']);
         }
     }
     // Start cURL request.
     $curl = curl_init($c_opts['url']);
     // Handle POST support.
     if (strtoupper($c_opts['method']) == 'POST') {
         curl_setopt($curl, CURLOPT_POST, true);
     }
     if (!empty($c_opts['referer'])) {
         curl_setopt($curl, CURLOPT_REFERER, $c_opts['referer']);
     }
     if ($postfields) {
         curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
     }
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $c_opts['timeout']);
     curl_setopt($curl, CURLOPT_TIMEOUT, $c_opts['timeout']);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($curl, CURLOPT_USERAGENT, $c_opts['useragent']);
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
     curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
     // Custom DNS management.
     curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
     curl_setopt($curl, CURLOPT_DNS_CACHE_TIMEOUT, 600);
     // Set custom HTTP headers.
     if (!empty($c_opts['headers'])) {
         curl_setopt($curl, CURLOPT_HTTPHEADER, $c_opts['headers']);
     }
     $return_raw = Utilities::curl_exec_utf8($curl);
     // End cURL request.
     Debug::endTimer('Make cURL Request');
     // Log more detailed information to screen about resolution times.
     $conn_info = curl_getinfo($curl);
     $important_conn_info = array('url', 'http_code', 'total_time', 'namelookup_time', 'connect_time', 'pretransfer_time', 'starttransfer_time', 'redirect_time');
     $debug_conn_info = array();
     foreach ($important_conn_info as $conn_param) {
         $debug_conn_info[$conn_param] = $conn_info[$conn_param];
     }
     Debug::print_r($debug_conn_info);
     $error = curl_error($curl);
     if ($error) {
         Debug::log("Curl error: " . $error);
     }
     return trim($return_raw);
 }