You are here

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

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

Parse Atom feed

Parameters

SimpleXMLElement $sxml:

FeedsImportBatch $batch:

FeedsSource $source:

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

File

./FeedsYoutubeParser.inc, line 200
Feeds parser class for Youtube

Class

FeedsYoutubeParser
Class definition for Youtube Parser.

Code

private function parseAtom(SimpleXMLElement $sxml, FeedsImportBatch $batch, FeedsSource $source) {
  $batch->title = $feed_title = (string) $sxml->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->entry as $entry) {

    // get video ID
    $arr = explode('/', $entry->id);
    $id = $arr[count($arr) - 1];

    // 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();
    $watch = str_replace('&feature=youtube_gdata_player', '', $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 = $attrs['seconds'];

    // get <yt:stats> node for viewer statistics
    $yt = $entry
      ->children('http://gdata.youtube.com/schemas/2007');
    $attrs = $yt->statistics
      ->attributes();
    $viewCount = $attrs['viewCount'];
    $favCount = $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 = $attrs['average'];
    }
    $updated = (string) $entry->updated;
    $published = (string) $entry->published;
    $item = array(
      'feed_title' => $feed_title,
      'guid' => (string) $entry->id,
      'video_id' => $id,
      'url' => 'http://www.youtube.com/watch?v=' . $id,
      'watch_page' => $watch,
      'title' => (string) $media->group->title,
      'author' => (string) $entry->author->name,
      'description' => (string) $media->group->description,
      'thumbnail' => $thumbnail,
      'category' => (string) $media->group->category,
      'tags' => explode(',', $media->group->keywords),
      'embedded_player' => '',
      'duration' => $this
        ->secsToTime($length),
      'duration_raw' => (int) $length,
      'view_count' => (string) $viewCount,
      'fav_count' => (string) $favCount,
      'rating' => (string) $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($published)),
      'published_timestamp' => strtotime($published),
    );

    // Populate the FeedsImportBatch object with the parsed results.
    $batch->items[] = $item;
  }
}