Esempio n. 1
1
 public static function api($row_obj, $include_episodes = TRUE)
 {
     if ($row_obj instanceof self) {
         $row = $row_obj->toArray();
         $row['stations'] = array();
         if ($row_obj->stations) {
             foreach ($row_obj->stations as $station) {
                 $row['stations'][] = Station::api($station);
             }
         }
         $row['episodes'] = array();
         if ($include_episodes && $row_obj->episodes) {
             foreach ($row_obj->episodes as $episode) {
                 $row['episodes'][] = $episode;
             }
         }
     } else {
         $row = $row_obj;
         if (isset($row['stations'])) {
             $stations_raw = array();
             foreach ($row['stations'] as $station) {
                 $stations_raw[] = Station::api($station);
             }
             $row['stations'] = $stations_raw;
         } else {
             $row['stations'] = array();
         }
         if (!isset($row['episodes'])) {
             $row['episodes'] = array();
         }
     }
     $api_row = array('id' => (int) $row['id'], 'name' => $row['name'], 'country' => $row['country'], 'description' => $row['description'], 'image_url' => \PVL\Url::upload(self::getArtistImage($row['image_url'])), 'banner_url' => \PVL\Url::upload($row['banner_url']), 'stations' => (array) $row['stations'], 'is_adult' => (bool) $row['is_adult']);
     if ($include_episodes) {
         $api_row['episodes'] = array();
         $i = 1;
         foreach ((array) $row['episodes'] as $ep) {
             if (is_int($include_episodes) && $i > $include_episodes) {
                 break;
             }
             $api_row['episodes'][] = PodcastEpisode::api($ep);
             $i++;
         }
     }
     $social_types = array_keys(self::getSocialTypes());
     foreach ($social_types as $type_key) {
         $api_row[$type_key] = $row[$type_key];
     }
     return $api_row;
 }
Esempio n. 2
0
 public static function processPodcast(Podcast $record)
 {
     $em = self::getEntityManager();
     $db_stats = array('record' => $record->name, 'updated' => 0, 'inserted' => 0, 'deleted' => 0);
     foreach ($record->sources as $source) {
         if ($source->is_active) {
             $new_episodes = $source->process();
             if (empty($new_episodes)) {
                 continue;
             }
             // Reconcile differences.
             $existing_episodes = array();
             foreach ($source->episodes as $episode) {
                 // Remove duplicate episode.
                 if (isset($existing_episodes[$episode->guid])) {
                     $db_stats['deleted']++;
                     $em->remove($episode);
                 } else {
                     $existing_episodes[$episode->guid] = $episode;
                 }
             }
             foreach ($new_episodes as $ep_guid => $ep_info) {
                 if (isset($existing_episodes[$ep_guid])) {
                     $db_stats['updated']++;
                     $episode = $existing_episodes[$ep_guid];
                 } else {
                     $db_stats['inserted']++;
                     $episode = new PodcastEpisode();
                     $episode->source = $source;
                     $episode->podcast = $record;
                     // Preload banner URL if specified, and if episode is new enough.
                     if ($ep_info['banner_url'] && $ep_info['timestamp'] > time() - 86400 * 14) {
                         PodcastEpisode::getEpisodeRotatorUrl($ep_info, $record, $source);
                     }
                 }
                 $episode->fromArray($ep_info);
                 $em->persist($episode);
                 unset($existing_episodes[$ep_guid]);
             }
             foreach ($existing_episodes as $ep_guid => $ep_to_remove) {
                 $db_stats['deleted']++;
                 $em->remove($ep_to_remove);
             }
         } else {
             foreach ($source->episodes as $episode) {
                 $em->remove($episode);
             }
         }
         $em->flush();
     }
     Debug::print_r($db_stats);
     return true;
 }
Esempio n. 3
0
 public function episodeAction()
 {
     $podcast_id = (int) $this->getParam('id');
     $episode_id = (int) $this->getParam('episode');
     $record = PodcastEpisode::getRepository()->findOneBy(array('id' => $episode_id, 'podcast_id' => $podcast_id));
     if (!$record instanceof PodcastEpisode) {
         throw new \DF\Exception\DisplayOnly('Podcast episode not found!');
     }
     $record->play_count = $record->play_count + 1;
     $record->save();
     // Insert into Influx
     if (isset($_SERVER['CF-Connecting-IP'])) {
         $remote_ip = $_SERVER['CF-Connecting-IP'];
     } else {
         $remote_ip = $_SERVER['REMOTE_ADDR'];
     }
     $origin = $this->getParam('origin', 'organic');
     $influx = $this->di->get('influx');
     $influx->setDatabase('pvlive_analytics');
     $influx->insert('podcast.' . $podcast_id . '.' . $episode_id, ['value' => 1, 'ip' => $remote_ip, 'client' => $origin, 'useragent' => $_SERVER['HTTP_USER_AGENT'], 'referrer' => $_SERVER['HTTP_REFERER']]);
     // If request made by AJAX, just return confirmation.
     if ($this->isAjax()) {
         return $this->renderJson(array('result' => 'OK'));
     } else {
         $redirect_url = \PVL\AnalyticsManager::addTracking($record->web_url, array('source' => $origin));
         return $this->redirect($redirect_url);
     }
 }
Esempio n. 4
0
 public function toggleAction()
 {
     $episode_id = (int) $this->getParam('id');
     $ep = PodcastEpisode::find($episode_id);
     if ($ep->podcast === $this->podcast) {
         $ep->is_active = !$ep->is_active;
         $ep->save();
     }
     return $this->redirectFromHere(array('action' => 'index', 'id' => NULL));
 }
Esempio n. 5
0
 public function latestAction()
 {
     try {
         $latest_shows = Podcast::fetchLatest();
         $return = array();
         foreach ((array) $latest_shows as $show_info) {
             $return_row = Podcast::api($show_info['record'], FALSE);
             foreach ((array) $show_info['episodes'] as $ep_row) {
                 $return_row['episodes'][] = PodcastEpisode::api($ep_row);
             }
             $return[] = $return_row;
         }
         return $this->returnSuccess($return);
     } catch (\Exception $e) {
         return $this->returnError($e->getMessage());
     }
 }
Esempio n. 6
0
 /**
  * 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();
     }
 }
Esempio n. 7
0
 public static function _runPodcastEpisodes(\Phalcon\DiInterface $di)
 {
     $news_items = array();
     $em = $di->get('em');
     // Pull podcast episodes.
     $podcasts_raw = $em->createQuery('SELECT p, pe, ps FROM Entity\\Podcast p LEFT JOIN p.episodes pe JOIN pe.source ps
         WHERE (p.banner_url IS NOT NULL AND p.banner_url != \'\')
         AND p.is_approved = 1
         AND pe.timestamp >= :threshold
         AND pe.is_active = 1
         ORDER BY p.id ASC, pe.timestamp DESC')->setParameter('threshold', strtotime('-1 month'))->getArrayResult();
     foreach ((array) $podcasts_raw as $podcast) {
         foreach ($podcast['episodes'] as $ep) {
             if (empty($ep['body'])) {
                 continue;
             }
             $title = trim($ep['title']);
             if ($podcast['is_adult']) {
                 $title = '[18+] ' . $title;
             }
             $news_items[] = array('id' => 'podcast_' . $ep['guid'], 'title' => $title, 'source' => 'podcast', 'body' => $ep['summary'], 'image_url' => \Entity\PodcastEpisode::getEpisodeRotatorUrl($ep, $podcast, $ep['source']), 'web_url' => \Entity\PodcastEpisode::getEpisodeLocalUrl($ep, 'pvlnews'), 'layout' => 'vertical', 'tags' => array($podcast['name'], 'Podcast Episodes'), 'sort_timestamp' => $ep['timestamp'], 'display_timestamp' => $ep['timestamp']);
             break;
         }
     }
     return $news_items;
 }