/** * 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; }
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; }
public static function api($row_raw) { if (empty($row_raw)) { return array(); } if ($row_raw instanceof self) { $row_raw = $row_raw->toArray(); } $row = array('id' => (int) $row_raw['id'], 'station_id' => (int) $row_raw['station_id'], 'guid' => $row_raw['guid'], 'start_time' => (int) $row_raw['start_time'], 'end_time' => (int) $row_raw['end_time'], 'is_all_day' => (bool) $row_raw['is_all_day'], 'title' => $row_raw['title'], 'location' => $row_raw['location'], 'body' => $row_raw['body'], 'banner_url' => $row_raw['banner_url'], 'web_url' => $row_raw['web_url'], 'range' => $row_raw['range'] ?: self::getRangeText($row_raw['start_time'], $row_raw['end_time'], $row_raw['is_all_day']), 'image_url' => \PVL\Url::upload(self::getRowImageUrl($row_raw))); if (isset($row_raw['minutes_until'])) { $row['minutes_until'] = (int) $row_raw['minutes_until']; } // Add station shortcode. if (isset($row_raw['station'])) { $row['station'] = Station::api($row_raw['station']); $shortcode = Station::getStationShortName($row_raw['station']['name']); $row['station_shortcode'] = $shortcode; } return $row; }
/** * 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 listAction() { $category = $this->getParam('category', 'all'); if ($category == 'all') { $stations_raw = Station::fetchArray(); } else { $cats = Station::getStationsInCategories(); if (!isset($cats[$category])) { return $this->returnError('Category not found.'); } $stations_raw = $cats[$category]['stations']; } $stations = array(); foreach ($stations_raw as $row) { $stations[] = Station::api($row); } return $this->returnSuccess($stations); }