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