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 if ($file) { |
franck@0
|
80 file_set_status($file, FILE_STATUS_PERMANENT); |
franck@0
|
81 } |
franck@0
|
82 } |
franck@0
|
83 // Create an atom |
franck@0
|
84 $atom = new stdClass; |
franck@0
|
85 $atom->type = 'video'; |
franck@0
|
86 $atom->provider = 'scald_dailymotion'; |
franck@0
|
87 $atom->base_id = $infos->id; |
franck@0
|
88 $atom->publisher = $user->uid; |
franck@0
|
89 $atom->title = $infos->title; |
franck@0
|
90 $aid = scald_author_get_id(array('name' => $infos->author)); |
franck@0
|
91 $atom->authors = array($aid); |
franck@0
|
92 // And save it |
franck@0
|
93 $atom_sid = scald_register_atom((array)$atom); |
franck@0
|
94 // Finally, return this id |
franck@0
|
95 return $atom_sid; |
franck@0
|
96 } |
franck@0
|
97 |
franck@0
|
98 /** |
franck@0
|
99 * Analyze a DailyMotion RSS feed, reformating the informations about its |
franck@0
|
100 * items in an easy to manipulate objects containing the informations we're |
franck@0
|
101 * interested in. |
franck@0
|
102 * @param $type |
franck@0
|
103 * DailyMotion RSS feed type. Examples include 'user', 'video', 'tag'... |
franck@0
|
104 * @param $id |
franck@0
|
105 * The identifier, related to the type mentionned above. If you're requestion |
franck@0
|
106 * a user feed, then, its the username... |
franck@0
|
107 * @return array |
franck@0
|
108 * Each array value is an object containing the following members: |
franck@0
|
109 * - id: the DailyMotion video id |
franck@0
|
110 * - author: the DailyMotion username of the user who uploaded the video |
franck@0
|
111 * - title: the video title |
franck@0
|
112 * - thumbnail: an associative array, containing the source ('src'), width |
franck@0
|
113 * and height of the video's thumbnail |
franck@0
|
114 * - pubDate: the publication date of the video |
franck@0
|
115 */ |
franck@0
|
116 function scald_dailymotion_feed($type, $id) { |
franck@0
|
117 $url = DAILYMOTION_RSS . $type .'/'. $id; |
franck@0
|
118 $xml = drupal_http_request($url); |
franck@0
|
119 $items = array(); |
franck@0
|
120 if ($xml->code != 404 && !empty($xml->data)) { |
franck@0
|
121 $dom = DOMDocument::loadXML($xml->data); |
franck@0
|
122 if ($dom) { |
franck@0
|
123 foreach($dom->getElementsByTagName('item') as $item) { |
franck@0
|
124 $info = new stdClass(); |
franck@0
|
125 // Fetch from the feed |
franck@0
|
126 // ... the video id |
franck@0
|
127 $info->id = $item->getElementsByTagNameNS(NS_DM, 'id')->item(0)->nodeValue; |
franck@0
|
128 // ... its title |
franck@0
|
129 $title = $item->getElementsByTagName('title')->item(0); |
franck@0
|
130 $info->title = $title->nodeValue; |
franck@0
|
131 // ... and usefull informations about its thumbnails |
franck@0
|
132 $thumb = $item->getElementsByTagNameNS(NS_MEDIA, 'thumbnail')->item(0); |
franck@0
|
133 $info->thumbnail = array( |
franck@0
|
134 'src' => $thumb->getAttribute('url'), |
franck@0
|
135 'width' => $thumb->getAttribute('width'), |
franck@0
|
136 'height' => $thumb->getAttribute('height') |
franck@0
|
137 ); |
franck@0
|
138 // ... also get the author |
franck@0
|
139 $info->author = $item->getElementsByTagNameNS(NS_DM, 'author')->item(0)->nodeValue; |
franck@0
|
140 // ... and the publication date |
franck@0
|
141 $info->pubDate = date('c', strtotime($item->getElementsByTagName('pubDate')->item(0)->nodeValue)); |
franck@1
|
142 $items[] = $info; |
franck@0
|
143 } |
franck@0
|
144 } |
franck@0
|
145 } |
franck@0
|
146 return $items; |
franck@0
|
147 } |
franck@0
|
148 |
franck@0
|
149 /** |
franck@0
|
150 * Get information on a specific video. |
franck@0
|
151 * @param $id |
franck@0
|
152 * The video id |
franck@0
|
153 * @return object |
franck@0
|
154 * An object containing the video informations. For information on |
franck@0
|
155 * the object format, see @scald_dailymotion_feed. |
franck@0
|
156 */ |
franck@0
|
157 function scald_dailymotion_video($id) { |
franck@0
|
158 $items = scald_dailymotion_feed('video', $id); |
franck@0
|
159 return $items[0]; |
franck@0
|
160 } |
franck@2
|
161 |
franck@2
|
162 /** |
franck@2
|
163 * Checks if a video has already been imported, based on its video |
franck@2
|
164 * id. |
franck@2
|
165 * @param $id |
franck@2
|
166 * The video identifier |
franck@2
|
167 * @return mixed |
franck@2
|
168 * FALSE if the video was never imported, the scald identifier of |
franck@2
|
169 * the video otherwise. |
franck@2
|
170 */ |
franck@2
|
171 function scald_dailymotion_already_imported($id) { |
franck@2
|
172 $query = array('provider' => 'scald_dailymotion', 'base_id' => $id); |
franck@2
|
173 return scald_search($query, FALSE, TRUE); |
franck@2
|
174 } |