view scald_dailymotion.module @ 0:ad3d1dbbb66e

Scald Dailymotion: First stab at a DailyMotion provider not based on node
author Franck Deroche <franck@defr.org>
date Fri, 16 Jul 2010 17:24:41 +0200
parents
children e851124eabe3
line wrap: on
line source
<?php
define('DAILYMOTION_RSS', 'http://www.dailymotion.com/rss/');
define('NS_MEDIA',              'http://search.yahoo.com/mrss');
define('NS_DM',                 'http://www.dailymotion.com/dmrss');

/**
 * Implements hook_scald_providers.
 * Tell Scald that we'll be providing some video atoms.
 */
function scald_dailymotion_scald_provider() {
  return array(
    'atoms' => array(
      'video' => array('A video hosted on DailyMotion')
    )
  );
}

/**
 * Implements hook_scald_fetch.
 */
function scald_dailymotion_scald_fetch(&$atom) {
  $atom->thumbnail_source = file_directory_path() . '/dailymotion/'. $atom->base_id .'.jpg';
  $atom->file_source = $atom->thumbnail_source;
}

/**
 * Implements hook_scald_prerender.
 */
function scald_dailymotion_scald_prerender(&$atom, $context, $options, $type) {
  if ($type == 'atom') {
    if ($context != 'sdl_library_item') {
      $atom->rendered->player = theme('scald_dailymotion_player', $atom->base_id, $atom->thumbnail_source);
    }
  }
}

/**
 * Implements hook_theme.
 */
function scald_dailymotion_theme() {
  return array(
    'scald_dailymotion_player' => array(
      'arguments' => array('video' => NULL, 'thumbnail' => NULL),
      'template' => 'scald_dailymotion_player'
    )
  );
}

/**
 * Creates an atom based on a DailyMotion video id.
 * @param $video_id
 *   Unique identifier of the video on dailymotion
 * @return integer
 *   Unique identifier of the new atom
 */
function scald_dailymotion_register($video_id) {
  global $user;
  // Fetch the needed informations from DailyMotion
  $infos = scald_dailymotion_video($video_id);
  // Download a copy of the video thumbnail. This makes it possible
  // to do interesting things when used with ImageCache for example.
  $thumb = drupal_http_request($infos->thumbnail['src']);
  $dir = file_directory_path() . '/dailymotion';
  if ($thumb->code == 200 && file_check_directory($dir)) {
    $dest = $dir . '/' . $infos->id . '.jpg';
    $file = file_save_data($thumb->data, $dest);
    if ($file) {
      file_set_status($file, FILE_STATUS_PERMANENT);
    }
  }
  // Create an atom
  $atom = new stdClass;
  $atom->type      = 'video';
  $atom->provider  = 'scald_dailymotion';
  $atom->base_id   = $infos->id;
  $atom->publisher = $user->uid;
  $atom->title     = $infos->title;
  $aid = scald_author_get_id(array('name' => $infos->author));
  $atom->authors   = array($aid);
  // And save it
  $atom_sid = scald_register_atom((array)$atom);
  // Finally, return this id
  return $atom_sid;
}

/**
 * Analyze a DailyMotion RSS feed, reformating the informations about its
 * items in an easy to manipulate objects containing the informations we're
 * interested in.
 * @param $type
 *   DailyMotion RSS feed type. Examples include 'user', 'video', 'tag'...
 * @param $id
 *   The identifier, related to the type mentionned above. If you're requestion
 *   a user feed, then, its the username...
 * @return array
 *   Each array value is an object containing the following members:
 *     - id: the DailyMotion video id
 *     - author: the DailyMotion username of the user who uploaded the video
 *     - title: the video title
 *     - thumbnail: an associative array, containing the source ('src'), width
 *       and height of the video's thumbnail
 *     - pubDate: the publication date of the video
 */
function scald_dailymotion_feed($type, $id) {
  $url = DAILYMOTION_RSS . $type .'/'. $id;
  $xml = drupal_http_request($url);
  $items = array();
  if ($xml->code != 404 && !empty($xml->data)) {
    $dom = DOMDocument::loadXML($xml->data);
    if ($dom) {
      foreach($dom->getElementsByTagName('item') as $item) {
        $info = new stdClass();
        // Fetch from the feed
        // ... the video id
        $info->id = $item->getElementsByTagNameNS(NS_DM, 'id')->item(0)->nodeValue;
        // ... its title
        $title = $item->getElementsByTagName('title')->item(0);
        $info->title = $title->nodeValue;
        // ... and usefull informations about its thumbnails
        $thumb = $item->getElementsByTagNameNS(NS_MEDIA, 'thumbnail')->item(0);
        $info->thumbnail = array(
          'src'    => $thumb->getAttribute('url'),
          'width'  => $thumb->getAttribute('width'),
          'height' => $thumb->getAttribute('height')
        );
        // ... also get the author
        $info->author = $item->getElementsByTagNameNS(NS_DM, 'author')->item(0)->nodeValue;
        // ... and the publication date
        $info->pubDate = date('c', strtotime($item->getElementsByTagName('pubDate')->item(0)->nodeValue));
        $items[] = $infos;
      }
    }
  }
  return $items;
}

/**
 * Get information on a specific video.
 * @param $id
 *   The video id
 * @return object
 *   An object containing the video informations. For information on
 *   the object format, see @scald_dailymotion_feed.
 */
function scald_dailymotion_video($id) {
  $items = scald_dailymotion_feed('video', $id);
  return $items[0];
}