/** * 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; }
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); } }
/** * 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(); } }
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'); }
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; }