You are here

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

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

Parse RSS 2.0 feed

Parameters

SimpleXMLElement $sxml:

FeedsImportBatch $batch:

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 280
Feeds parser class for Youtube

Class

FeedsYoutubeParser
Class definition for Youtube Parser.

Code

private function parseRss20(SimpleXMLElement $sxml, FeedsImportBatch $batch, FeedsSource $source) {

  // XML was parsed successfully, so we can begin to process items
  $batch->title = (string) $sxml->channel->title;
  $batch->description = (string) $sxml->channel->description;
  $batch->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 = (int) $attrs['average'];
    }
    $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,
      '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 FeedsImportBatch object with the parsed results.
    $batch->items[] = $item;
  }
}