annotate scald_dailymotion.module @ 3:2a63a6e15166

Thumbnails: Don't try to file_set_status, they're not in the {files} table
author Franck Deroche <franck@defr.org>
date Mon, 19 Jul 2010 14:45:25 +0200
parents c57b2ac8f84c
children fd5fb845d0bc
rev   line source
franck@0 1 <?php
franck@0 2 define('DAILYMOTION_RSS', 'http://www.dailymotion.com/rss/');
franck@0 3 define('NS_MEDIA', 'http://search.yahoo.com/mrss');
franck@0 4 define('NS_DM', 'http://www.dailymotion.com/dmrss');
franck@0 5
franck@0 6 /**
franck@0 7 * Implements hook_scald_providers.
franck@0 8 * Tell Scald that we'll be providing some video atoms.
franck@0 9 */
franck@0 10 function scald_dailymotion_scald_provider() {
franck@0 11 return array(
franck@0 12 'atoms' => array(
franck@0 13 'video' => array('A video hosted on DailyMotion')
franck@0 14 )
franck@0 15 );
franck@0 16 }
franck@0 17
franck@0 18 /**
franck@0 19 * Implements hook_scald_fetch.
franck@0 20 */
franck@0 21 function scald_dailymotion_scald_fetch(&$atom) {
franck@0 22 $atom->thumbnail_source = file_directory_path() . '/dailymotion/'. $atom->base_id .'.jpg';
franck@0 23 $atom->file_source = $atom->thumbnail_source;
franck@0 24 }
franck@0 25
franck@0 26 /**
franck@0 27 * Implements hook_scald_prerender.
franck@0 28 */
franck@0 29 function scald_dailymotion_scald_prerender(&$atom, $context, $options, $type) {
franck@0 30 if ($type == 'atom') {
franck@0 31 if ($context != 'sdl_library_item') {
franck@0 32 $atom->rendered->player = theme('scald_dailymotion_player', $atom->base_id, $atom->thumbnail_source);
franck@0 33 }
franck@0 34 }
franck@0 35 }
franck@0 36
franck@0 37 /**
franck@0 38 * Implements hook_theme.
franck@0 39 */
franck@0 40 function scald_dailymotion_theme() {
franck@0 41 return array(
franck@0 42 'scald_dailymotion_player' => array(
franck@0 43 'arguments' => array('video' => NULL, 'thumbnail' => NULL),
franck@0 44 'template' => 'scald_dailymotion_player'
franck@0 45 )
franck@0 46 );
franck@0 47 }
franck@0 48
franck@0 49 /**
franck@2 50 * Creates an atom based on a DailyMotion video id or an object
franck@2 51 * containing the video informations..
franck@2 52 * @param $video
franck@2 53 * Unique identifier of the video on dailymotion, or object
franck@2 54 * returned by scald_dailymotion_video.
franck@0 55 * @return integer
franck@0 56 * Unique identifier of the new atom
franck@0 57 */
franck@2 58 function scald_dailymotion_register($video) {
franck@0 59 global $user;
franck@0 60 // Fetch the needed informations from DailyMotion
franck@2 61 if (is_object($video)) {
franck@2 62 $infos = $video;
franck@2 63 }
franck@2 64 else {
franck@2 65 $infos = scald_dailymotion_video($video);
franck@2 66 }
franck@2 67 // Check if the video has already been imported to prevent duplicated
franck@2 68 $old = scald_dailymotion_already_imported($infos->id);
franck@2 69 if ($old) {
franck@2 70 return $old;
franck@2 71 }
franck@0 72 // Download a copy of the video thumbnail. This makes it possible
franck@0 73 // to do interesting things when used with ImageCache for example.
franck@0 74 $thumb = drupal_http_request($infos->thumbnail['src']);
franck@0 75 $dir = file_directory_path() . '/dailymotion';
franck@0 76 if ($thumb->code == 200 && file_check_directory($dir)) {
franck@0 77 $dest = $dir . '/' . $infos->id . '.jpg';
franck@0 78 $file = file_save_data($thumb->data, $dest);
franck@0 79 }
franck@0 80 // Create an atom
franck@0 81 $atom = new stdClass;
franck@0 82 $atom->type = 'video';
franck@0 83 $atom->provider = 'scald_dailymotion';
franck@0 84 $atom->base_id = $infos->id;
franck@0 85 $atom->publisher = $user->uid;
franck@0 86 $atom->title = $infos->title;
franck@0 87 $aid = scald_author_get_id(array('name' => $infos->author));
franck@0 88 $atom->authors = array($aid);
franck@0 89 // And save it
franck@0 90 $atom_sid = scald_register_atom((array)$atom);
franck@0 91 // Finally, return this id
franck@0 92 return $atom_sid;
franck@0 93 }
franck@0 94
franck@0 95 /**
franck@0 96 * Analyze a DailyMotion RSS feed, reformating the informations about its
franck@0 97 * items in an easy to manipulate objects containing the informations we're
franck@0 98 * interested in.
franck@0 99 * @param $type
franck@0 100 * DailyMotion RSS feed type. Examples include 'user', 'video', 'tag'...
franck@0 101 * @param $id
franck@0 102 * The identifier, related to the type mentionned above. If you're requestion
franck@0 103 * a user feed, then, its the username...
franck@0 104 * @return array
franck@0 105 * Each array value is an object containing the following members:
franck@0 106 * - id: the DailyMotion video id
franck@0 107 * - author: the DailyMotion username of the user who uploaded the video
franck@0 108 * - title: the video title
franck@0 109 * - thumbnail: an associative array, containing the source ('src'), width
franck@0 110 * and height of the video's thumbnail
franck@0 111 * - pubDate: the publication date of the video
franck@0 112 */
franck@0 113 function scald_dailymotion_feed($type, $id) {
franck@0 114 $url = DAILYMOTION_RSS . $type .'/'. $id;
franck@0 115 $xml = drupal_http_request($url);
franck@0 116 $items = array();
franck@0 117 if ($xml->code != 404 && !empty($xml->data)) {
franck@0 118 $dom = DOMDocument::loadXML($xml->data);
franck@0 119 if ($dom) {
franck@0 120 foreach($dom->getElementsByTagName('item') as $item) {
franck@0 121 $info = new stdClass();
franck@0 122 // Fetch from the feed
franck@0 123 // ... the video id
franck@0 124 $info->id = $item->getElementsByTagNameNS(NS_DM, 'id')->item(0)->nodeValue;
franck@0 125 // ... its title
franck@0 126 $title = $item->getElementsByTagName('title')->item(0);
franck@0 127 $info->title = $title->nodeValue;
franck@0 128 // ... and usefull informations about its thumbnails
franck@0 129 $thumb = $item->getElementsByTagNameNS(NS_MEDIA, 'thumbnail')->item(0);
franck@0 130 $info->thumbnail = array(
franck@0 131 'src' => $thumb->getAttribute('url'),
franck@0 132 'width' => $thumb->getAttribute('width'),
franck@0 133 'height' => $thumb->getAttribute('height')
franck@0 134 );
franck@0 135 // ... also get the author
franck@0 136 $info->author = $item->getElementsByTagNameNS(NS_DM, 'author')->item(0)->nodeValue;
franck@0 137 // ... and the publication date
franck@0 138 $info->pubDate = date('c', strtotime($item->getElementsByTagName('pubDate')->item(0)->nodeValue));
franck@1 139 $items[] = $info;
franck@0 140 }
franck@0 141 }
franck@0 142 }
franck@0 143 return $items;
franck@0 144 }
franck@0 145
franck@0 146 /**
franck@0 147 * Get information on a specific video.
franck@0 148 * @param $id
franck@0 149 * The video id
franck@0 150 * @return object
franck@0 151 * An object containing the video informations. For information on
franck@0 152 * the object format, see @scald_dailymotion_feed.
franck@0 153 */
franck@0 154 function scald_dailymotion_video($id) {
franck@0 155 $items = scald_dailymotion_feed('video', $id);
franck@0 156 return $items[0];
franck@0 157 }
franck@2 158
franck@2 159 /**
franck@2 160 * Checks if a video has already been imported, based on its video
franck@2 161 * id.
franck@2 162 * @param $id
franck@2 163 * The video identifier
franck@2 164 * @return mixed
franck@2 165 * FALSE if the video was never imported, the scald identifier of
franck@2 166 * the video otherwise.
franck@2 167 */
franck@2 168 function scald_dailymotion_already_imported($id) {
franck@2 169 $query = array('provider' => 'scald_dailymotion', 'base_id' => $id);
franck@2 170 return scald_search($query, FALSE, TRUE);
franck@2 171 }