franck@0: array( franck@0: 'video' => array('A video hosted on DailyMotion') franck@0: ) franck@0: ); franck@0: } franck@0: franck@0: /** franck@0: * Implements hook_scald_fetch. franck@0: */ franck@0: function scald_dailymotion_scald_fetch(&$atom) { franck@0: $atom->thumbnail_source = file_directory_path() . '/dailymotion/'. $atom->base_id .'.jpg'; franck@0: $atom->file_source = $atom->thumbnail_source; franck@0: } franck@0: franck@0: /** franck@0: * Implements hook_scald_prerender. franck@0: */ franck@0: function scald_dailymotion_scald_prerender(&$atom, $context, $options, $type) { franck@0: if ($type == 'atom') { franck@0: if ($context != 'sdl_library_item') { franck@0: $atom->rendered->player = theme('scald_dailymotion_player', $atom->base_id, $atom->thumbnail_source); franck@0: } franck@0: } franck@0: } franck@0: franck@0: /** franck@0: * Implements hook_theme. franck@0: */ franck@0: function scald_dailymotion_theme() { franck@0: return array( franck@0: 'scald_dailymotion_player' => array( franck@0: 'arguments' => array('video' => NULL, 'thumbnail' => NULL), franck@0: 'template' => 'scald_dailymotion_player' franck@0: ) franck@0: ); franck@0: } franck@0: franck@0: /** franck@2: * Creates an atom based on a DailyMotion video id or an object franck@2: * containing the video informations.. franck@2: * @param $video franck@2: * Unique identifier of the video on dailymotion, or object franck@2: * returned by scald_dailymotion_video. franck@0: * @return integer franck@0: * Unique identifier of the new atom franck@0: */ franck@2: function scald_dailymotion_register($video) { franck@0: global $user; franck@0: // Fetch the needed informations from DailyMotion franck@2: if (is_object($video)) { franck@2: $infos = $video; franck@2: } franck@2: else { franck@2: $infos = scald_dailymotion_video($video); franck@2: } franck@2: // Check if the video has already been imported to prevent duplicated franck@2: $old = scald_dailymotion_already_imported($infos->id); franck@2: if ($old) { franck@2: return $old; franck@2: } franck@0: // Download a copy of the video thumbnail. This makes it possible franck@0: // to do interesting things when used with ImageCache for example. franck@0: $thumb = drupal_http_request($infos->thumbnail['src']); franck@0: $dir = file_directory_path() . '/dailymotion'; franck@0: if ($thumb->code == 200 && file_check_directory($dir)) { franck@0: $dest = $dir . '/' . $infos->id . '.jpg'; franck@0: $file = file_save_data($thumb->data, $dest); franck@0: if ($file) { franck@0: file_set_status($file, FILE_STATUS_PERMANENT); franck@0: } franck@0: } franck@0: // Create an atom franck@0: $atom = new stdClass; franck@0: $atom->type = 'video'; franck@0: $atom->provider = 'scald_dailymotion'; franck@0: $atom->base_id = $infos->id; franck@0: $atom->publisher = $user->uid; franck@0: $atom->title = $infos->title; franck@0: $aid = scald_author_get_id(array('name' => $infos->author)); franck@0: $atom->authors = array($aid); franck@0: // And save it franck@0: $atom_sid = scald_register_atom((array)$atom); franck@0: // Finally, return this id franck@0: return $atom_sid; franck@0: } franck@0: franck@0: /** franck@0: * Analyze a DailyMotion RSS feed, reformating the informations about its franck@0: * items in an easy to manipulate objects containing the informations we're franck@0: * interested in. franck@0: * @param $type franck@0: * DailyMotion RSS feed type. Examples include 'user', 'video', 'tag'... franck@0: * @param $id franck@0: * The identifier, related to the type mentionned above. If you're requestion franck@0: * a user feed, then, its the username... franck@0: * @return array franck@0: * Each array value is an object containing the following members: franck@0: * - id: the DailyMotion video id franck@0: * - author: the DailyMotion username of the user who uploaded the video franck@0: * - title: the video title franck@0: * - thumbnail: an associative array, containing the source ('src'), width franck@0: * and height of the video's thumbnail franck@0: * - pubDate: the publication date of the video franck@0: */ franck@0: function scald_dailymotion_feed($type, $id) { franck@0: $url = DAILYMOTION_RSS . $type .'/'. $id; franck@0: $xml = drupal_http_request($url); franck@0: $items = array(); franck@0: if ($xml->code != 404 && !empty($xml->data)) { franck@0: $dom = DOMDocument::loadXML($xml->data); franck@0: if ($dom) { franck@0: foreach($dom->getElementsByTagName('item') as $item) { franck@0: $info = new stdClass(); franck@0: // Fetch from the feed franck@0: // ... the video id franck@0: $info->id = $item->getElementsByTagNameNS(NS_DM, 'id')->item(0)->nodeValue; franck@0: // ... its title franck@0: $title = $item->getElementsByTagName('title')->item(0); franck@0: $info->title = $title->nodeValue; franck@0: // ... and usefull informations about its thumbnails franck@0: $thumb = $item->getElementsByTagNameNS(NS_MEDIA, 'thumbnail')->item(0); franck@0: $info->thumbnail = array( franck@0: 'src' => $thumb->getAttribute('url'), franck@0: 'width' => $thumb->getAttribute('width'), franck@0: 'height' => $thumb->getAttribute('height') franck@0: ); franck@0: // ... also get the author franck@0: $info->author = $item->getElementsByTagNameNS(NS_DM, 'author')->item(0)->nodeValue; franck@0: // ... and the publication date franck@0: $info->pubDate = date('c', strtotime($item->getElementsByTagName('pubDate')->item(0)->nodeValue)); franck@1: $items[] = $info; franck@0: } franck@0: } franck@0: } franck@0: return $items; franck@0: } franck@0: franck@0: /** franck@0: * Get information on a specific video. franck@0: * @param $id franck@0: * The video id franck@0: * @return object franck@0: * An object containing the video informations. For information on franck@0: * the object format, see @scald_dailymotion_feed. franck@0: */ franck@0: function scald_dailymotion_video($id) { franck@0: $items = scald_dailymotion_feed('video', $id); franck@0: return $items[0]; franck@0: } franck@2: franck@2: /** franck@2: * Checks if a video has already been imported, based on its video franck@2: * id. franck@2: * @param $id franck@2: * The video identifier franck@2: * @return mixed franck@2: * FALSE if the video was never imported, the scald identifier of franck@2: * the video otherwise. franck@2: */ franck@2: function scald_dailymotion_already_imported($id) { franck@2: $query = array('provider' => 'scald_dailymotion', 'base_id' => $id); franck@2: return scald_search($query, FALSE, TRUE); franck@2: }