franck@0
|
1 <?php |
franck@4
|
2 // $Id$ |
franck@4
|
3 /** |
franck@4
|
4 * @file |
franck@4
|
5 * Defines a DailyMotion provider for Scald. |
franck@4
|
6 */ |
franck@0
|
7 define('DAILYMOTION_RSS', 'http://www.dailymotion.com/rss/'); |
franck@0
|
8 define('NS_MEDIA', 'http://search.yahoo.com/mrss'); |
franck@0
|
9 define('NS_DM', 'http://www.dailymotion.com/dmrss'); |
franck@0
|
10 |
franck@0
|
11 /** |
franck@0
|
12 * Implements hook_scald_providers. |
franck@0
|
13 * Tell Scald that we'll be providing some video atoms. |
franck@0
|
14 */ |
franck@0
|
15 function scald_dailymotion_scald_provider() { |
franck@0
|
16 return array( |
franck@0
|
17 'atoms' => array( |
franck@0
|
18 'video' => array('A video hosted on DailyMotion') |
franck@0
|
19 ) |
franck@0
|
20 ); |
franck@0
|
21 } |
franck@0
|
22 |
franck@0
|
23 /** |
franck@0
|
24 * Implements hook_scald_fetch. |
franck@0
|
25 */ |
franck@0
|
26 function scald_dailymotion_scald_fetch(&$atom) { |
franck@0
|
27 $atom->thumbnail_source = file_directory_path() . '/dailymotion/'. $atom->base_id .'.jpg'; |
franck@0
|
28 $atom->file_source = $atom->thumbnail_source; |
franck@0
|
29 } |
franck@0
|
30 |
franck@0
|
31 /** |
franck@0
|
32 * Implements hook_scald_prerender. |
franck@0
|
33 */ |
franck@0
|
34 function scald_dailymotion_scald_prerender(&$atom, $context, $options, $type) { |
franck@0
|
35 if ($type == 'atom') { |
franck@0
|
36 if ($context != 'sdl_library_item') { |
franck@0
|
37 $atom->rendered->player = theme('scald_dailymotion_player', $atom->base_id, $atom->thumbnail_source); |
franck@0
|
38 } |
franck@0
|
39 } |
franck@0
|
40 } |
franck@0
|
41 |
franck@0
|
42 /** |
franck@0
|
43 * Implements hook_theme. |
franck@0
|
44 */ |
franck@0
|
45 function scald_dailymotion_theme() { |
franck@0
|
46 return array( |
franck@0
|
47 'scald_dailymotion_player' => array( |
franck@0
|
48 'arguments' => array('video' => NULL, 'thumbnail' => NULL), |
franck@0
|
49 'template' => 'scald_dailymotion_player' |
franck@4
|
50 ), |
franck@4
|
51 'scald_dailymotion_imports_table' => array( |
franck@4
|
52 'arguments' => array('form' => NULL), |
franck@4
|
53 'file' => 'scald_dailymotion.admin.inc' |
franck@7
|
54 ), |
franck@7
|
55 'scald_dailymotion_search_results_table' => array( |
franck@7
|
56 'arguments' => array('form' => NULL), |
franck@7
|
57 'file' => 'scald_dailymotion.pages.inc' |
franck@0
|
58 ) |
franck@0
|
59 ); |
franck@0
|
60 } |
franck@0
|
61 |
franck@0
|
62 /** |
franck@4
|
63 * Implements hook_perm. |
franck@4
|
64 */ |
franck@4
|
65 function scald_dailymotion_perm() { |
franck@7
|
66 return array('administer dailymotion imports', 'import dailymotion videos'); |
franck@4
|
67 } |
franck@4
|
68 |
franck@4
|
69 /** |
franck@4
|
70 * Implements hook_cron. |
franck@4
|
71 */ |
franck@4
|
72 function scald_dailymotion_cron() { |
franck@4
|
73 $imports = variable_get('scald_dailymotion_imports', array()); |
franck@4
|
74 foreach ($imports as $import) { |
franck@4
|
75 $items = scald_dailymotion_feed($import['type'], $import['value']); |
franck@4
|
76 foreach ($items as $item) { |
franck@4
|
77 scald_dailymotion_register($item); |
franck@4
|
78 } |
franck@4
|
79 } |
franck@4
|
80 } |
franck@4
|
81 |
franck@4
|
82 /** |
franck@4
|
83 * Implements hook_menu. |
franck@4
|
84 */ |
franck@4
|
85 function scald_dailymotion_menu() { |
franck@4
|
86 $items = array(); |
franck@4
|
87 $items['admin/settings/scald_dailymotion'] = array( |
franck@4
|
88 'title' => 'DailyMotion imports', |
franck@4
|
89 'page callback' => 'drupal_get_form', |
franck@4
|
90 'page arguments' => array('scald_dailymotion_imports_form'), |
franck@4
|
91 'access arguments' => array('administer dailymotion imports'), |
franck@4
|
92 'description' => 'Configure the videos that should be automatically imported from DailyMotion', |
franck@4
|
93 'file' => 'scald_dailymotion.admin.inc' |
franck@4
|
94 ); |
franck@7
|
95 $items['dailymotion/search'] = array( |
franck@7
|
96 'title' => 'DailyMotion Video search', |
franck@7
|
97 'page callback' => 'drupal_get_form', |
franck@7
|
98 'page arguments' => array('scald_dailymotion_search_form', 2), |
franck@7
|
99 'access arguments' => array('import dailymotion videos'), |
franck@7
|
100 'description' => 'Search for new videos to import into this site', |
franck@7
|
101 'file' => 'scald_dailymotion.pages.inc' |
franck@7
|
102 ); |
franck@4
|
103 return $items; |
franck@4
|
104 } |
franck@4
|
105 |
franck@4
|
106 /** |
franck@2
|
107 * Creates an atom based on a DailyMotion video id or an object |
franck@2
|
108 * containing the video informations.. |
franck@2
|
109 * @param $video |
franck@2
|
110 * Unique identifier of the video on dailymotion, or object |
franck@2
|
111 * returned by scald_dailymotion_video. |
franck@0
|
112 * @return integer |
franck@0
|
113 * Unique identifier of the new atom |
franck@0
|
114 */ |
franck@2
|
115 function scald_dailymotion_register($video) { |
franck@0
|
116 global $user; |
franck@0
|
117 // Fetch the needed informations from DailyMotion |
franck@2
|
118 if (is_object($video)) { |
franck@2
|
119 $infos = $video; |
franck@2
|
120 } |
franck@2
|
121 else { |
franck@2
|
122 $infos = scald_dailymotion_video($video); |
franck@2
|
123 } |
franck@2
|
124 // Check if the video has already been imported to prevent duplicated |
franck@2
|
125 $old = scald_dailymotion_already_imported($infos->id); |
franck@2
|
126 if ($old) { |
franck@2
|
127 return $old; |
franck@2
|
128 } |
franck@0
|
129 // Download a copy of the video thumbnail. This makes it possible |
franck@0
|
130 // to do interesting things when used with ImageCache for example. |
franck@0
|
131 $thumb = drupal_http_request($infos->thumbnail['src']); |
franck@0
|
132 $dir = file_directory_path() . '/dailymotion'; |
franck@0
|
133 if ($thumb->code == 200 && file_check_directory($dir)) { |
franck@0
|
134 $dest = $dir . '/' . $infos->id . '.jpg'; |
franck@0
|
135 $file = file_save_data($thumb->data, $dest); |
franck@0
|
136 } |
franck@0
|
137 // Create an atom |
franck@0
|
138 $atom = new stdClass; |
franck@0
|
139 $atom->type = 'video'; |
franck@0
|
140 $atom->provider = 'scald_dailymotion'; |
franck@0
|
141 $atom->base_id = $infos->id; |
franck@0
|
142 $atom->publisher = $user->uid; |
franck@0
|
143 $atom->title = $infos->title; |
franck@0
|
144 $aid = scald_author_get_id(array('name' => $infos->author)); |
franck@0
|
145 $atom->authors = array($aid); |
franck@0
|
146 // And save it |
franck@0
|
147 $atom_sid = scald_register_atom((array)$atom); |
franck@0
|
148 // Finally, return this id |
franck@0
|
149 return $atom_sid; |
franck@0
|
150 } |
franck@0
|
151 |
franck@0
|
152 /** |
franck@0
|
153 * Analyze a DailyMotion RSS feed, reformating the informations about its |
franck@0
|
154 * items in an easy to manipulate objects containing the informations we're |
franck@0
|
155 * interested in. |
franck@0
|
156 * @param $type |
franck@0
|
157 * DailyMotion RSS feed type. Examples include 'user', 'video', 'tag'... |
franck@0
|
158 * @param $id |
franck@0
|
159 * The identifier, related to the type mentionned above. If you're requestion |
franck@0
|
160 * a user feed, then, its the username... |
franck@0
|
161 * @return array |
franck@0
|
162 * Each array value is an object containing the following members: |
franck@0
|
163 * - id: the DailyMotion video id |
franck@0
|
164 * - author: the DailyMotion username of the user who uploaded the video |
franck@0
|
165 * - title: the video title |
franck@0
|
166 * - thumbnail: an associative array, containing the source ('src'), width |
franck@0
|
167 * and height of the video's thumbnail |
franck@0
|
168 * - pubDate: the publication date of the video |
franck@0
|
169 */ |
franck@0
|
170 function scald_dailymotion_feed($type, $id) { |
franck@6
|
171 $url = DAILYMOTION_RSS . $type .'/'. urlencode($id); |
franck@0
|
172 $xml = drupal_http_request($url); |
franck@0
|
173 $items = array(); |
franck@0
|
174 if ($xml->code != 404 && !empty($xml->data)) { |
franck@0
|
175 $dom = DOMDocument::loadXML($xml->data); |
franck@0
|
176 if ($dom) { |
franck@0
|
177 foreach($dom->getElementsByTagName('item') as $item) { |
franck@0
|
178 $info = new stdClass(); |
franck@0
|
179 // Fetch from the feed |
franck@0
|
180 // ... the video id |
franck@0
|
181 $info->id = $item->getElementsByTagNameNS(NS_DM, 'id')->item(0)->nodeValue; |
franck@0
|
182 // ... its title |
franck@0
|
183 $title = $item->getElementsByTagName('title')->item(0); |
franck@0
|
184 $info->title = $title->nodeValue; |
franck@0
|
185 // ... and usefull informations about its thumbnails |
franck@0
|
186 $thumb = $item->getElementsByTagNameNS(NS_MEDIA, 'thumbnail')->item(0); |
franck@0
|
187 $info->thumbnail = array( |
franck@0
|
188 'src' => $thumb->getAttribute('url'), |
franck@0
|
189 'width' => $thumb->getAttribute('width'), |
franck@0
|
190 'height' => $thumb->getAttribute('height') |
franck@0
|
191 ); |
franck@0
|
192 // ... also get the author |
franck@0
|
193 $info->author = $item->getElementsByTagNameNS(NS_DM, 'author')->item(0)->nodeValue; |
franck@0
|
194 // ... and the publication date |
franck@0
|
195 $info->pubDate = date('c', strtotime($item->getElementsByTagName('pubDate')->item(0)->nodeValue)); |
franck@1
|
196 $items[] = $info; |
franck@0
|
197 } |
franck@0
|
198 } |
franck@0
|
199 } |
franck@0
|
200 return $items; |
franck@0
|
201 } |
franck@0
|
202 |
franck@0
|
203 /** |
franck@0
|
204 * Get information on a specific video. |
franck@0
|
205 * @param $id |
franck@0
|
206 * The video id |
franck@0
|
207 * @return object |
franck@0
|
208 * An object containing the video informations. For information on |
franck@0
|
209 * the object format, see @scald_dailymotion_feed. |
franck@0
|
210 */ |
franck@0
|
211 function scald_dailymotion_video($id) { |
franck@0
|
212 $items = scald_dailymotion_feed('video', $id); |
franck@0
|
213 return $items[0]; |
franck@0
|
214 } |
franck@2
|
215 |
franck@2
|
216 /** |
franck@2
|
217 * Checks if a video has already been imported, based on its video |
franck@2
|
218 * id. |
franck@2
|
219 * @param $id |
franck@2
|
220 * The video identifier |
franck@2
|
221 * @return mixed |
franck@2
|
222 * FALSE if the video was never imported, the scald identifier of |
franck@2
|
223 * the video otherwise. |
franck@2
|
224 */ |
franck@2
|
225 function scald_dailymotion_already_imported($id) { |
franck@2
|
226 $query = array('provider' => 'scald_dailymotion', 'base_id' => $id); |
franck@2
|
227 return scald_search($query, FALSE, TRUE); |
franck@2
|
228 } |