Exemple #1
0
 /**
  * Detect
  *
  * @param $field_name
  * @param null $default_value
  * @return null
  */
 protected function _getFileValueOrDefault($field_name, $default_value = null)
 {
     if ($this->{$field_name}) {
         $value = $this->{$field_name};
         $path = \PVL\Service\AmazonS3::path($value);
         if (file_exists($path)) {
             return $value;
         }
     }
     return $default_value;
 }
Exemple #2
0
 public static function generate()
 {
     set_time_limit(60);
     // Fix DF\URL // prefixing.
     \DF\Url::forceSchemePrefix(true);
     $nowplaying = self::loadNowPlaying();
     // Post statistics to official record (legacy for duplication, for now)
     // Analytics::post($nowplaying['api']);
     // Post statistics to InfluxDB.
     $influx = self::getInflux();
     $influx->setDatabase('pvlive_stations');
     $active_shortcodes = Station::getShortNameLookup();
     $total_overall = 0;
     foreach ($nowplaying['api'] as $short_code => $info) {
         $listeners = (int) $info['listeners']['current'];
         $station_id = $info['station']['id'];
         if (isset($active_shortcodes[$short_code])) {
             $total_overall += $listeners;
         }
         $influx->insert('station.' . $station_id . '.listeners', ['value' => $listeners]);
     }
     $influx->insert('all.listeners', ['value' => $total_overall]);
     // Clear any records that are not audio/video category.
     $api_categories = array('audio', 'video');
     foreach ($nowplaying['api'] as $station_shortcode => $station_info) {
         if (!in_array($station_info['station']['category'], $api_categories)) {
             unset($nowplaying['api'][$station_shortcode]);
             unset($nowplaying['legacy'][$station_shortcode]);
         }
     }
     // Generate PVL legacy nowplaying file.
     $nowplaying_feed = json_encode($nowplaying['legacy'], JSON_UNESCAPED_SLASHES);
     $pvl_file_path = \PVL\Service\AmazonS3::path('api/nowplaying.json');
     @file_put_contents($pvl_file_path, $nowplaying_feed);
     $legacy_file_path = DF_INCLUDE_STATIC . '/api/nowplaying.json';
     @file_put_contents($legacy_file_path, $nowplaying_feed);
     // Generate PVL API cache.
     $np_api = $nowplaying['api'];
     foreach ($np_api as $station => $np_info) {
         $np_api[$station]['cache'] = 'hit';
     }
     Cache::save($np_api, 'api_nowplaying_data', array('nowplaying'), 60);
     foreach ($np_api as $station => $np_info) {
         $np_api[$station]['cache'] = 'flatfile';
     }
     // Generate PVL API nowplaying file.
     $file_path_api = \PVL\Service\AmazonS3::path('api/nowplaying_api.json');
     $nowplaying_api = json_encode(array('status' => 'success', 'result' => $np_api), JSON_UNESCAPED_SLASHES);
     @file_put_contents($file_path_api, $nowplaying_api);
     // Push to live-update service.
     PvlNode::push('nowplaying', $nowplaying['api']);
     return $pvl_file_path;
 }
 public function indexAction()
 {
     $this->setCacheLifetime(15);
     // Pull from cache, or load from flatfile otherwise.
     $np = \DF\Cache::get('api_nowplaying_data', function () {
         $file_path_api = \PVL\Service\AmazonS3::path('api/nowplaying_api.json');
         $np_raw = file_get_contents($file_path_api);
         $np_arr = @json_decode($np_raw, TRUE);
         $np = $np_arr['result'];
         return $np;
     });
     // Sanity check for now playing data.
     if (empty($np)) {
         return $this->returnError('Now Playing data has not loaded into the cache. Wait for file reload.');
     }
     if ($this->hasParam('id') || $this->hasParam('station')) {
         if ($this->hasParam('id')) {
             $id = (int) $this->getParam('id');
             foreach ($np as $key => $np_row) {
                 if ($np_row['station']['id'] == $id) {
                     $sc = $key;
                     break;
                 }
             }
             if (empty($sc)) {
                 return $this->returnError('Station not found!');
             }
         } elseif ($this->hasParam('station')) {
             $sc = $this->getParam('station');
         }
         if (isset($np[$sc])) {
             return $this->returnSuccess($np[$sc]);
         } else {
             return $this->returnError('Station not found!');
         }
     } elseif ($this->hasParam('category')) {
         $type = $this->getParam('category');
         $np = array_filter($np, function ($station_row) use($type) {
             return $station_row['station']['category'] == $type;
         });
         return $this->returnSuccess($np);
     } else {
         return $this->returnSuccess($np);
     }
 }
Exemple #4
0
 public function uploadSong($local_path)
 {
     if (!$this->title) {
         $this->title = 'No Title';
     }
     if (!$this->artist) {
         $this->artist = 'Various Artists';
     }
     // Create a new path based on the song metadata.
     $song_path_base = $this->artist . ' - ' . $this->title;
     $song_path_base = preg_replace('/[^\\w\\s-]+/', '', $song_path_base);
     $song_path_base = preg_replace('/\\s\\s+/', ' ', $song_path_base);
     $new_path = 'song_uploads/' . $song_path_base . '.' . \DF\File::getFileExtension($local_path);
     // Upload to remote service.
     \PVL\Service\AmazonS3::upload($local_path, $new_path);
     $this->song_url = $new_path;
     return \PVL\Service\AmazonS3::url($new_path);
 }
 /**
  * Send notifications for new podcast episodes.
  *
  * @param \Phalcon\DiInterface $di
  * @throws \DF\Exception
  */
 public static function _runPodcastEpisodes(\Phalcon\DiInterface $di, $force = false)
 {
     $em = $di->get('em');
     $start_threshold = time() - 86400 * 7;
     $end_threshold = time();
     $podcast_episodes = $em->createQuery('SELECT pe, p
         FROM Entity\\PodcastEpisode pe JOIN pe.podcast p
         WHERE pe.timestamp BETWEEN :start AND :end
         AND pe.is_notified = 0
         AND pe.is_active = 1
         AND p.is_approved = 1
         ORDER BY pe.timestamp DESC')->setParameter('start', $start_threshold)->setParameter('end', $end_threshold)->setMaxResults(1)->execute();
     if ($podcast_episodes) {
         $episode = $podcast_episodes[0];
         $podcast = $episode->podcast;
         $podcast_name = $podcast->name;
         if ($podcast->is_adult) {
             $podcast_name = '[18+] ' . $podcast_name;
         }
         $title = \DF\Utilities::truncateText($episode->title, 110 - strlen($podcast_name) - 6);
         $tweet = $podcast_name . ': "' . $title . '"';
         PvlNode::push('podcast.new_episode', array('episode' => PodcastEpisode::api($episode), 'podcast' => Podcast::api($podcast, false)));
         $image_url = NULL;
         if ($podcast->banner_url) {
             $image_url = \PVL\Service\AmazonS3::path($podcast->banner_url);
         }
         // Special handling of podcast YT videos.
         if (stristr($episode->web_url, 'youtube.com') !== false) {
             $image_url = NULL;
         }
         self::notify($tweet, $episode->getLocalUrl('twitter'), $image_url);
         // Set all episodes of the same podcast to be notified, to prevent spam.
         $em->createQuery('UPDATE Entity\\PodcastEpisode pe SET pe.is_notified=1 WHERE pe.podcast_id = :podcast_id')->setParameter('podcast_id', $podcast->id)->execute();
     }
 }
Exemple #6
0
 public function cleanupAction()
 {
     $s3_path = 'db_dumps/pvlive_import.sql';
     \PVL\Service\AmazonS3::delete($s3_path);
     return $this->returnSuccess('DB Import dump file deleted.');
 }
Exemple #7
0
 /**
  * Return URL for user-uploaded content.
  *
  * @param null $path
  * @return string
  */
 public static function upload($path = NULL)
 {
     return \PVL\Service\AmazonS3::url($path);
 }
Exemple #8
0
 public static function getEpisodeRotatorUrl($episode, $podcast = NULL, $source = NULL)
 {
     if ($episode instanceof self) {
         if ($podcast === null) {
             $podcast = $episode->podcast;
         }
         if ($source === null) {
             $source = $episode->source;
         }
     }
     if ($episode['banner_url'] && !$podcast['is_adult'] && !$podcast['always_use_banner_url']) {
         $image_path_base = 'podcast_episodes/' . $episode['guid'] . '.jpg';
         $image_path = AmazonS3::path($image_path_base);
         // Crop remote banner URL if the local version doesn't already exist.
         if (file_exists($image_path)) {
             return AmazonS3::url($image_path_base);
         } else {
             $temp_path_ext = \DF\File::getFileExtension($episode['banner_url']);
             $temp_path = DF_INCLUDE_TEMP . DIRECTORY_SEPARATOR . '/podcast_episodes/podcast_episode_' . $episode['id'] . '_temp.' . $temp_path_ext;
             @mkdir(dirname($temp_path));
             @copy($episode['banner_url'], $temp_path);
             if (file_exists($temp_path)) {
                 try {
                     Image::resizeImage($temp_path, $temp_path, 600, 300, TRUE);
                     AmazonS3::upload($temp_path, $image_path_base);
                     return AmazonS3::url($image_path_base);
                 } catch (\Exception $e) {
                 }
             }
         }
     }
     if ($podcast !== null && !empty($podcast['banner_url'])) {
         // Reference the podcast's existing banner URL.
         return AmazonS3::url($podcast['banner_url']);
     }
     if ($source !== null) {
         return Url::content('images/podcast_' . $source['type'] . '_banner.png');
     }
     return Url::content('images/podcast_default.png');
 }
Exemple #9
0
 public static function _runTumblrNews(\Phalcon\DiInterface $di)
 {
     $news_items = array();
     $config = $di->get('config');
     // Pull featured images.
     $timestamp_threshold = strtotime('-6 weeks');
     $api_params = array('api_key' => $config->apis->tumblr->key, 'limit' => 10);
     $api_url = 'http://api.tumblr.com/v2/blog/news.ponyvillelive.com/posts/photo?' . http_build_query($api_params);
     $results_raw = @file_get_contents($api_url);
     if ($results_raw) {
         $results = json_decode($results_raw, true);
         $posts = $results['response']['posts'];
         foreach ((array) $posts as $post) {
             if ($post['timestamp'] < $timestamp_threshold) {
                 continue;
             }
             $image = null;
             $post_style = 'vertical';
             $image_is_valid = false;
             foreach ((array) $post['photos'] as $photo) {
                 $image = $photo['original_size'];
                 if ($image['width'] == 600 && $image['height'] == 300) {
                     $image_is_valid = true;
                     $post_style = 'vertical';
                     break;
                 } elseif ($image['width'] == 1150 && $image['height'] == 200) {
                     $image_is_valid = true;
                     $post_style = 'horizontal';
                     break;
                 }
             }
             if (!$image_is_valid) {
                 continue;
             }
             // Copy the image to the local static directory (for SSL and other caching support).
             $image_url = $image['url'];
             $image_url_basename = basename($image_url);
             $local_path_base = 'rotators/' . $image_url_basename;
             $local_url = $local_path_base;
             $local_path = DF_INCLUDE_TEMP . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $local_path_base);
             $s3_path = Service\AmazonS3::path($local_url);
             if (!file_exists($s3_path)) {
                 @mkdir(dirname($local_path));
                 @copy($image_url, $local_path);
                 // Optimize image for fast display.
                 Image::resizeImage($local_path, $local_path, $image['width'], $image['height']);
                 Service\AmazonS3::upload($local_path, $local_path_base);
             }
             $tags = array_map('strtolower', (array) $post['tags']);
             if (in_array('archive', $tags)) {
                 continue;
             }
             $description = strip_tags($post['caption']);
             if (strpos($description, ':') === FALSE) {
                 break;
             }
             list($title, $description) = explode(':', $description, 2);
             $description = Utilities::truncateText($description, self::DESCRIPTION_LENGTH);
             $news_items[] = array('id' => 'tumblr_' . $post['id'], 'title' => trim($title), 'source' => 'tumblr', 'body' => trim($description), 'image_url' => \PVL\Url::upload($local_url), 'web_url' => $post['post_url'], 'layout' => $post_style, 'tags' => (array) $post['tags'], 'sort_timestamp' => $post['timestamp'], 'display_timestamp' => $post['timestamp']);
         }
     }
     return $news_items;
 }
Exemple #10
0
}
/**
 * Database Import
 */
echo 'Importing database entries from production server...' . PHP_EOL;
$remote_url = $remote_base . '/dev/import?key=' . $api_key;
$remote_response_raw = file_get_contents($remote_url);
$remote_response = @json_decode($remote_response_raw, true);
if ($remote_response['status'] != 'success') {
    die('The remote server could not return a valid MySQL import response. Halting remote import.');
}
$db_path = $remote_response['result']['path'];
// Force S3 enabled in development mode.
define('DF_UPLOAD_URL', 'dev.pvlive.me');
$s3_client = \PVL\Service\AmazonS3::initClient();
$s3_bucket = \PVL\Service\AmazonS3::getBucket();
if (!$s3_client) {
    die('Amazon S3 could not be initialized! Halting remote import.');
}
// Trigger download of the entire bucket to the local static folder.
$s3_client->downloadBucket(DF_INCLUDE_STATIC, $s3_bucket);
// Clean up S3 bucket.
$remote_url = $remote_base . '/dev/cleanup?key=' . $api_key;
// Prepare and execute mysqlimport command.
$db_path_full = DF_INCLUDE_STATIC . DIRECTORY_SEPARATOR . $db_path;
$db_config = $config->db->toArray();
$command_flags = array('-h ' . $db_config['host'], '-u ' . $db_config['user'], '-p' . $db_config['password'], $db_config['dbname']);
$command = 'mysql ' . implode(' ', $command_flags) . ' < ' . $db_path_full;
system($command);
@unlink($db_path_full);
@rmdir(dirname($db_path_full));