You are here

private function FeedsYoutubeParser::parseRss20 in Feeds: YouTube Parser 7.2

Same name and namespace in other branches
  1. 6 FeedsYoutubeParser.inc \FeedsYoutubeParser::parseRss20()

Parse RSS 2.0 feed.

Parameters

SimpleXMLElement $sxml:

FeedsFetcherResult $fetcher_result:

FeedsSource $source:

1 call to FeedsYoutubeParser::parseRss20()
FeedsYoutubeParser::parse in ./FeedsYoutubeParser.inc
Parse the extra mapping sources provided by this parser.

File

./FeedsYoutubeParser.inc, line 320
Feeds parser class for YouTube.

Class

FeedsYoutubeParser
Class definition for YouTube Parser.

Code

private function parseRss20(SimpleXMLElement $sxml, FeedsSource $source, FeedsFetcherResult $fetcher_result) {

  // XML was parsed successfully, so we can begin to process items.
  $result = new FeedsParserResult();
  $fetcher_result->title = (string) $sxml->channel->title;
  $fetcher_result->description = (string) $sxml->channel->description;
  $fetcher_result->link = (string) $sxml->channel->link;
  $feed_title = (string) $sxml->channel->title;

  // Iterate over entries in feed.
  // TODO: This is not DRY - extract things which is same in Atom and RSS20 to common method
  foreach ($sxml
    ->xpath('//item') as $entry) {

    // Get Atom nodes.
    $atom = $entry
      ->children('http://www.w3.org/2005/Atom');
    $updated = $atom->updated;

    // Get video ID.
    $id = end(explode('/', $entry->guid));

    // Get nodes in media: namespace for media information.
    $media = $entry
      ->children('http://search.yahoo.com/mrss/');

    // Get video player URL
    $attrs = $media->group->player
      ->attributes();
    $player = (string) $attrs['url'];

    // Get video thumbnail.
    $attrs = $media->group->thumbnail[0]
      ->attributes();
    $thumbnail = (string) $attrs['url'];

    // Get <yt:duration> node for video length.
    $yt = $media
      ->children('http://gdata.youtube.com/schemas/2007');
    $attrs = $yt->duration
      ->attributes();
    $length = (int) $attrs['seconds'];

    // Get <yt:stats> node for viewer statistics.
    $yt = $entry
      ->children('http://gdata.youtube.com/schemas/2007');
    $attrs = $yt->statistics
      ->attributes();
    $viewCount = (int) $attrs['viewCount'];
    $favCount = (int) $attrs['favoriteCount'];

    // Get <gd:rating> node for video ratings.
    $gd = $entry
      ->children('http://schemas.google.com/g/2005');
    $rating = 0;
    if ($gd->rating) {
      $attrs = $gd->rating
        ->attributes();
      $rating = (double) $attrs['average'];
      $raters_number = (int) $attrs['numRaters'];
    }
    $item = array(
      'feed_title' => $feed_title,
      'guid' => (string) $entry->guid,
      'video_id' => $id,
      'url' => 'http://www.youtube.com/watch?v=' . $id,
      'watch_page' => 'http://www.youtube.com/watch?v=' . $id,
      'title' => html_entity_decode((string) $media->group->title),
      'author' => (string) $entry->author,
      'description' => html_entity_decode((string) $media->group->description),
      'thumbnail' => $thumbnail,
      'category' => (string) $media->group->category,
      'tags' => explode(',', (string) $media->group->keywords),
      'embedded_player' => $player,
      'duration' => $this
        ->secsToTime($length),
      'duration_raw' => $length,
      'view_count' => $viewCount,
      'fav_count' => $favCount,
      'rating' => $rating,
      'raters_number' => $raters_number,
      'updated_datetime' => date('Y-m-d H:i:s', strtotime($updated)),
      'updated_timestamp' => strtotime($updated),
      'published_datetime' => date('Y-m-d H:i:s', strtotime($entry->pubDate)),
      'published_timestamp' => strtotime($entry->pubDate),
    );

    // Populate the FeedsFetcherResult object with the parsed results.
    $result->items[] = $item;
  }
  return $result;
}