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