|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * Feeds parser class for Telvue |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Class definition for Telvue Parser. |
| 10 | + * |
| 11 | + * Parses RSS feeds returned from Telvue schedule. |
| 12 | + */ |
| 13 | +class FeedsTelvueParser extends FeedsParser { |
| 14 | + |
| 15 | + /** |
| 16 | + * Parse the extra mapping sources provided by this parser. |
| 17 | + * |
| 18 | + * @param $fetcher_result FeedsFetcherResult |
| 19 | + * @param $source FeedsSource |
| 20 | + * |
| 21 | + * @see FeedsParser::parse() |
| 22 | + */ |
| 23 | + public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) { |
| 24 | + |
| 25 | + $mediarss_feed = $fetcher_result->getRaw(); |
| 26 | + |
| 27 | + $result = new FeedsParserResult(); |
| 28 | + |
| 29 | + /** |
| 30 | + * @see common_syndication_parser_parse() |
| 31 | + */ |
| 32 | + if (!defined('LIBXML_VERSION') || (version_compare(phpversion(), '5.1.0', '<'))) { |
| 33 | + @$sxml = simplexml_load_string($mediarss_feed, NULL); |
| 34 | + } |
| 35 | + else { |
| 36 | + @$sxml = simplexml_load_string($mediarss_feed, NULL, LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NOCDATA); |
| 37 | + } |
| 38 | + |
| 39 | + // Got a malformed XML. |
| 40 | + if ($sxml === FALSE || is_null($sxml)) { |
| 41 | + throw new Exception(t('FeedsTelvueParser: Malformed XML source.')); |
| 42 | + } |
| 43 | + |
| 44 | + if ($this->isRssFeed($sxml)) { |
| 45 | + $result = $this->parseRss20($sxml, $source, $fetcher_result); |
| 46 | + } |
| 47 | + else { |
| 48 | + throw new Exception(t('FeedsTelvueParser: Unknown type of feed.')); |
| 49 | + } |
| 50 | + return $result; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Check if given feed object is a RSS feed. |
| 55 | + * |
| 56 | + * @param SimpleXMLElement $sxml |
| 57 | + * |
| 58 | + * @return boolen |
| 59 | + * TRUE if given SimpleXML object is RSS feed or FALSE |
| 60 | + */ |
| 61 | + protected function isRssFeed(SimpleXMLElement $sxml) { |
| 62 | + return $sxml->getName() == 'rss'; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Add the extra mapping sources provided by this parser. |
| 67 | + */ |
| 68 | + public function getMappingSources() { |
| 69 | + return parent::getMappingSources() + array( |
| 70 | + 'guid' => array( |
| 71 | + 'name' => t('GUID'), |
| 72 | + ), |
| 73 | + 'link' => array( |
| 74 | + 'name' => t('Link'), |
| 75 | + ), |
| 76 | + 'title' => array( |
| 77 | + 'name' => t('Title'), |
| 78 | + 'description' => t('Title.'), |
| 79 | + ), |
| 80 | + 'author' => array( |
| 81 | + 'name' => t('Author'), |
| 82 | + 'description' => t('Author or uploader of the video.'), |
| 83 | + ), |
| 84 | + 'published' => array( |
| 85 | + 'name' => t('Published'), |
| 86 | + ), |
| 87 | + 'description' => array( |
| 88 | + 'name' => t('Description'), |
| 89 | + ), |
| 90 | + 'tags' => array( |
| 91 | + 'name' => t('Tags'), |
| 92 | + 'description' => t('This can be imported directly with Taxonomy "tags" vocabularies.'), |
| 93 | + ), |
| 94 | + |
| 95 | + |
| 96 | + // http://www.telvue.com/support/product-documentation/princeton-server-api-docs/schedule-methods/get-channel-schedule-as-rss-rest/ |
| 97 | + 'channel' => array( |
| 98 | + 'name' => t('channel'), |
| 99 | + ), |
| 100 | + 'psg_eventid' => array( |
| 101 | + 'name' => t('eventId'), |
| 102 | + ), |
| 103 | + 'psg_duration' => array( |
| 104 | + 'name' => t('duration'), |
| 105 | + ), |
| 106 | + 'psg_end_datetime' => array( |
| 107 | + 'name' => t('end_datetime'), |
| 108 | + ), |
| 109 | + 'psg_programcode' => array( |
| 110 | + 'name' => t('programCode'), |
| 111 | + ), |
| 112 | + 'psg_episode' => array( |
| 113 | + 'name' => t('episode'), |
| 114 | + ), |
| 115 | + 'psg_episodecode' => array( |
| 116 | + 'name' => t('episodeCode'), |
| 117 | + ), |
| 118 | + 'psg_thumbnail' => array( |
| 119 | + 'name' => t('thumbnail'), |
| 120 | + ), |
| 121 | + |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + /** |
| 128 | + * Parse RSS 2.0 feed |
| 129 | + * |
| 130 | + * @param SimpleXMLElement $sxml |
| 131 | + * @param FeedsFetcherResult $fetcher_result |
| 132 | + * @param FeedsSource $source |
| 133 | + */ |
| 134 | + private function parseRss20(SimpleXMLElement $sxml, FeedsSource $source, FeedsFetcherResult $fetcher_result) { |
| 135 | + |
| 136 | + // XML was parsed successfully, so we can begin to process items |
| 137 | + $result = new FeedsParserResult(); |
| 138 | + $fetcher_result->title = (string) $sxml->channel->title; |
| 139 | + $fetcher_result->description = (string) $sxml->channel->description; |
| 140 | + $fetcher_result->link = (string) $sxml->channel->link; |
| 141 | + $feed_title = (string) $sxml->channel->title; |
| 142 | + |
| 143 | + $namespaces = $sxml->getNamespaces(true); |
| 144 | + |
| 145 | + foreach ($sxml->xpath('//item') as $entry) { |
| 146 | + |
| 147 | + // Get nodes in media: namespace for media information |
| 148 | + $psg = $entry->children($namespaces['psg']); |
| 149 | + |
| 150 | + $start_parts = explode(' -', $entry->pubDate); |
| 151 | + $end_parts = explode(' -', $psg->end_datetime[0]); |
| 152 | + |
| 153 | + //shift |
| 154 | + $hours = $start_parts[1]/100; |
| 155 | + $shift = 60*60*$hours; |
| 156 | + |
| 157 | + $start = strtotime($start_parts[0]) - $shift; |
| 158 | + $end = strtotime($end_parts[0]) - $shift; |
| 159 | + |
| 160 | + //$start = strtotime($start_parts[0]); |
| 161 | + //$end = strtotime($end_parts[0]); |
| 162 | + |
| 163 | + $item = array( |
| 164 | + 'guid' => (string) $entry->guid, |
| 165 | + 'link' => (string) $entry->link, |
| 166 | + 'title' => html_entity_decode((string) $entry->title), |
| 167 | + 'author' => (string) $entry->author, |
| 168 | + 'description' => html_entity_decode((string) $entry->description), |
| 169 | + // $media object doesn't exist. |
| 170 | + //'tags' => explode(',', (string) $media->group->keywords), |
| 171 | + 'published' => (string) $start, |
| 172 | + 'channel' => (string) $feed_title, |
| 173 | + 'psg_eventid' => (string) $psg->eventId[0], |
| 174 | + 'psg_duration' => (string) $psg->duration[0], |
| 175 | + 'psg_end_datetime' => $end, |
| 176 | + 'psg_programcode' => (string) $psg->programcode[0], |
| 177 | + 'psg_episode' => (string) $psg->episode[0], |
| 178 | + 'psg_episodecode' => (string) $psg->episodecode[0], |
| 179 | + 'psg_thumbnail' => (string) $psg->thumbnail[0], |
| 180 | + |
| 181 | + ); |
| 182 | + |
| 183 | + // Populate the FeedsFetcherResult object with the parsed results. |
| 184 | + $result->items[] = $item; |
| 185 | + } |
| 186 | + |
| 187 | + return $result; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Display seconds as HH:MM:SS, with leading 0's. |
| 192 | + * |
| 193 | + * @param $seconds |
| 194 | + * The number of seconds to display. |
| 195 | + */ |
| 196 | + public function secsToTime($seconds) { |
| 197 | + // Number of seconds in an hour. |
| 198 | + $unith = 3600; |
| 199 | + // Number of seconds in a minute. |
| 200 | + $unitm = 60; |
| 201 | + |
| 202 | + // '/' given value by num sec in hour... output = HOURS |
| 203 | + $hh = intval($seconds / $unith); |
| 204 | + |
| 205 | + // Multiply number of hours by seconds, then subtract from given value. |
| 206 | + // Output = REMAINING seconds. |
| 207 | + $ss_remaining = ($seconds - ($hh * 3600)); |
| 208 | + |
| 209 | + // Take remaining seconds and divide by seconds in a min... output = MINS. |
| 210 | + $mm = intval($ss_remaining / $unitm); |
| 211 | + // Multiply number of mins by seconds, then subtract from remaining seconds. |
| 212 | + // Output = REMAINING seconds. |
| 213 | + $ss = ($ss_remaining - ($mm * 60)); |
| 214 | + |
| 215 | + $output = ''; |
| 216 | + |
| 217 | + // If we have any hours, then prepend that to our output. |
| 218 | + if ($hh) { |
| 219 | + $output .= "$hh:"; |
| 220 | + } |
| 221 | + |
| 222 | + // Create a safe-for-output MM:SS. |
| 223 | + $output .= sprintf($hh ? "%02d:%02d" : "%d:%02d", $mm, $ss); |
| 224 | + |
| 225 | + return $output; |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | + |
0 commit comments