You are here

function _parser_simplepie_feedapi_parse in FeedAPI 6

Same name and namespace in other branches
  1. 5 parser_simplepie/parser_simplepie.module \_parser_simplepie_feedapi_parse()

Parsing the feed

Parameters

$url: The feed's url

Return value

The structured datas extracted from the feed

1 call to _parser_simplepie_feedapi_parse()
parser_simplepie_feedapi_feed in parser_simplepie/parser_simplepie.module
Implementation of hook_feedapi_feed().

File

parser_simplepie/parser_simplepie.module, line 116
Parse the incoming URL with SimplePie then provide a data structure of the feed

Code

function _parser_simplepie_feedapi_parse($feed, $settings = array()) {
  $parser = _parser_simplepie_get_parser($feed->url, TRUE, $settings);
  if ($parser->error) {
    return FALSE;
  }

  // Do we have html_entity_decode? Some feeds return html entities in the links
  $entity_decode = function_exists('html_entity_decode');

  // Construct the standard form of the parsed feed
  $parsed_source = new stdClass();
  $parsed_source->description = $parser
    ->get_description();
  $parsed_source->title = _parser_simplepie_title($parser
    ->get_title(), $parser
    ->get_description());
  $parsed_source->options = new stdClass();
  $parsed_source->options->link = $entity_decode && $parser
    ->get_link() ? html_entity_decode($parser
    ->get_link()) : $parser
    ->get_link();
  $parsed_source->items = array();
  $items_num = $parser
    ->get_item_quantity();
  for ($i = 0; $i < $items_num; $i++) {
    $curr_item = new stdClass();
    $simplepie_item = $parser
      ->get_item($i);
    $curr_item->title = _parser_simplepie_title($simplepie_item
      ->get_title(), $simplepie_item
      ->get_content());
    $curr_item->description = $simplepie_item
      ->get_content();
    $curr_item->options = new stdClass();
    $curr_item->options->original_url = $entity_decode && $simplepie_item
      ->get_link() ? html_entity_decode($simplepie_item
      ->get_link()) : $simplepie_item
      ->get_link();

    // U = std. unix timestamp
    $curr_item->options->timestamp = $simplepie_item
      ->get_date("U");
    $curr_item->options->guid = $simplepie_item
      ->get_id();
    $curr_item->options->original_author = $simplepie_item
      ->get_author();

    // Enclosures
    $enclosures = $simplepie_item
      ->get_enclosures();
    if (is_array($enclosures)) {
      foreach ($enclosures as $enclosure) {
        $mime = $enclosure
          ->get_real_type();
        if ($mime != '') {
          list($type, $subtype) = split('/', $mime);
          $curr_item->options->enclosures[$type][$subtype][] = $enclosure;
        }
      }
    }

    // Location
    $latitude = $simplepie_item
      ->get_latitude();
    $longitude = $simplepie_item
      ->get_longitude();
    if (!is_null($latitude) && !is_null($longitude)) {
      $curr_item->options->location->latitude[] = $latitude;
      $curr_item->options->location->longitude[] = $longitude;
    }

    // Extract tags related to the item
    $simplepie_tags = $simplepie_item
      ->get_categories();
    $tags = array();
    $domains = array();
    if (count($simplepie_tags) > 0) {
      foreach ($simplepie_tags as $tag) {
        $tags[] = (string) $tag->term;
        $domain = (string) $tag
          ->get_scheme();
        if (!empty($domain)) {
          if (!isset($domains[$domain])) {
            $domains[$domain] = array();
          }
          $domains[$domain][] = count($tags) - 1;
        }
      }
    }
    $curr_item->options->domains = $domains;
    $curr_item->options->tags = $tags;

    // Stick the raw data onto the feed item.
    $curr_item->options->raw = $simplepie_item->data;
    $parsed_source->items[] = $curr_item;
  }
  return $parsed_source;
}