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