/** * Generate Structured NowPlaying Data * * @param Station $station * @return array Structured NowPlaying Data */ public static function processStation(Station $station) { $em = self::getEntityManager(); $np_old = (array) $station->nowplaying_data; $np = array(); $np['status'] = 'offline'; $np['station'] = Station::api($station); // Remove API-supplied 'streams' item in the wrong place. unset($np['station']['streams']); $listener_totals = array('current' => 0, 'unique' => 0, 'total' => 0); $np['streams'] = array(); foreach ($station->streams as $stream) { if (!$stream->is_active) { continue; } if ($station->category == 'video') { $np_stream = self::processVideoStream($stream, $station); foreach ($listener_totals as $type => $total) { $listener_totals[$type] += $np_stream['meta']['listeners']; } } else { $np_stream = self::processAudioStream($stream, $station); foreach ($np_stream['listeners'] as $type => $count) { $listener_totals[$type] += $count; } } $np['streams'][] = $np_stream; $em->persist($stream); // Merge default info into main array for legacy purposes. if ($np_stream['is_default'] == true) { $np['status'] = $np_stream['status']; $np['station']['stream_url'] = $np_stream['url']; $np['station']['default_stream_id'] = $np_stream['id']; if ($station->category != 'video') { $np['current_song'] = $np_stream['current_song']; $np['song_history'] = $np_stream['song_history']; } } } $np['listeners'] = $listener_totals; // Get currently active event (cached query) $event_current = Schedule::getCurrentEvent($station->id); $event_upcoming = Schedule::getUpcomingEvent($station->id); $np['event'] = Schedule::api($event_current); $np['event_upcoming'] = Schedule::api($event_upcoming); if ($station->category != 'video') { $station->nowplaying_data = array('current_song' => $np['current_song'], 'song_history' => $np['song_history']); $em->persist($station); } $em->flush(); return $np; }
/** * Send notifications for new station events. * * @param \Phalcon\DiInterface $di * @param bool $force * @throws \DF\Exception */ public static function _runStationEvents(\Phalcon\DiInterface $di, $force = false) { $notify_minutes = 15; $em = $di->get('em'); $start_threshold = time(); $end_threshold = time() + 60 * $notify_minutes; $schedule_items = $em->createQuery('SELECT s, st FROM Entity\\Schedule s JOIN s.station st WHERE s.start_time >= :start AND s.start_time <= :end AND s.is_notified = 0')->setParameter('start', $start_threshold)->setParameter('end', $end_threshold)->setMaxResults(1)->execute(); if ($schedule_items) { $schedule_item = $schedule_items[0]; $station = $schedule_item->station; if ($station->twitter_url) { $twitter_handle = '@' . array_pop(explode('/', $station->twitter_url)); } else { $twitter_handle = $station->name; } $tweet = 'Tune in to ' . $schedule_item->title . ' in ' . $notify_minutes . ' minutes on ' . $twitter_handle . '!'; $tweet_url = $station->getShortUrl(); PvlNode::push('schedule.event_upcoming', array('event' => Schedule::api($schedule_item), 'station' => Station::api($station))); $image_url = NULL; if ($schedule_item->banner_url) { $image_url = $schedule_item->banner_url; } else { if ($station->banner_url) { $image_url = \PVL\Service\AmazonS3::path($station->banner_url); } } self::notify($tweet, $tweet_url, $image_url, $force); $schedule_item->is_notified = true; $schedule_item->save(); } }
public function indexAction() { // Get calendar name. $short_names = Station::getShortNameLookup(); $station_shortcode = $this->getParam('station', 'all'); if ($station_shortcode != "all") { $station = $short_names[$station_shortcode]; $calendar_name = $station['name']; } else { $calendar_name = 'Ponyville Live!'; } // Get timestamp boundaries. if ($this->hasParam('month')) { $show = $this->getParam('month'); $calendar = new \DF\Calendar($show); $timestamps = $calendar->getTimestamps(); $start_timestamp = $timestamps['start']; $end_timestamp = $timestamps['end']; $use_cache = true; $cache_name = 'month_' . $show; $calendar_name .= ' - ' . date('F Y', $timestamps['mid']); } elseif ($this->hasParam('start')) { $start_timestamp = (int) $this->getParam('start'); $end_timestamp = (int) $this->getParam('end'); $use_cache = false; $cache_name = null; // $cache_name = 'range_'.$start_timestamp.'_'.$end_timestamp; $calendar_name .= ' - ' . date('F j, Y', $start_timestamp) . ' to ' . date('F j, Y', $end_timestamp); } else { $start_timestamp = time(); $end_timestamp = time() + 86400 * 30; $use_cache = true; $cache_name = 'upcoming'; $calendar_name .= ' - Upcoming'; } // Load from cache or regenerate. if ($use_cache) { $cache_name = 'api_sched_' . $station_shortcode . '_' . $cache_name; $events = \DF\Cache::get($cache_name); } else { $events = null; } if (!$events) { if ($station_shortcode != "all") { $station = $short_names[$station_shortcode]; $events_raw = $this->em->createQuery('SELECT s FROM Entity\\Schedule s WHERE (s.station_id = :sid) AND (s.start_time <= :end AND s.end_time >= :start) ORDER BY s.start_time ASC')->setParameter('sid', $station['id'])->setParameter('start', $start_timestamp)->setParameter('end', $end_timestamp)->getArrayResult(); } else { $events_raw = $this->em->createQuery('SELECT s, st FROM Entity\\Schedule s LEFT JOIN s.station st WHERE (s.start_time <= :end AND s.end_time >= :start) ORDER BY s.start_time ASC')->setParameter('start', $start_timestamp)->setParameter('end', $end_timestamp)->getArrayResult(); } $events = array(); foreach ((array) $events_raw as $event) { $events[] = Schedule::api($event); } if ($use_cache) { \DF\Cache::save($events, $cache_name, array(), 300); } } $format = strtolower($this->getParam('format', 'json')); switch ($format) { case "ics": case "ical": return $this->_printCalendar($events, $calendar_name, $cache_name); break; case "json": default: return $this->returnSuccess($events); break; } }