You are here

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

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

Parse Atom feed.

Parameters

SimpleXMLElement $sxml:

FeedsFetcherResult $fetcher_result:

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 219
Feeds parser class for YouTube.

Class

FeedsYoutubeParser
Class definition for YouTube Parser.

Code

private function parseAtom(SimpleXMLElement $sxml, FeedsSource $source, FeedsFetcherResult $fetcher_result) {
  $fetcher_result->title = $feed_title = (string) $sxml->title;
  $result = new FeedsParserResult();

  // 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 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 video ID.
    $id = (string) $yt->videoid;

    // Get video uploaded date.
    $uploaded = (string) $yt->uploaded;

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

    // Get <yt:rating> node for likes and dislikes statistics.
    $likes = 0;
    $dislikes = 0;
    if (isset($yt->rating) && ($attrs = $yt->rating
      ->attributes())) {
      $likes = (int) $attrs['numLikes'];
      $dislikes = (int) $attrs['numDislikes'];
    }

    // Get <gd:rating> node for video ratings.
    $gd = $entry
      ->children('http://schemas.google.com/g/2005');
    $rating = 0;
    $raters_number = 0;
    if ($gd->rating) {
      $attrs = $gd->rating
        ->attributes();
      $rating = (double) $attrs['average'];
      $raters_number = (int) $attrs['numRaters'];
    }
    $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,
      '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($published)),
      'published_timestamp' => strtotime($published),
      'uploaded_datetime' => date('Y-m-d H:i:s', strtotime($uploaded)),
      'uploaded_timestamp' => strtotime($uploaded),
      'likes' => $likes,
      'dislikes' => $dislikes,
    );

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