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@0
|
50 * Creates an atom based on a DailyMotion video id. |
franck@0
|
51 * @param $video_id |
franck@0
|
52 * Unique identifier of the video on dailymotion |
franck@0
|
53 * @return integer |
franck@0
|
54 * Unique identifier of the new atom |
franck@0
|
55 */ |
franck@0
|
56 function scald_dailymotion_register($video_id) { |
franck@0
|
57 global $user; |
franck@0
|
58 // Fetch the needed informations from DailyMotion |
franck@0
|
59 $infos = scald_dailymotion_video($video_id); |
franck@0
|
60 // Download a copy of the video thumbnail. This makes it possible |
franck@0
|
61 // to do interesting things when used with ImageCache for example. |
franck@0
|
62 $thumb = drupal_http_request($infos->thumbnail['src']); |
franck@0
|
63 $dir = file_directory_path() . '/dailymotion'; |
franck@0
|
64 if ($thumb->code == 200 && file_check_directory($dir)) { |
franck@0
|
65 $dest = $dir . '/' . $infos->id . '.jpg'; |
franck@0
|
66 $file = file_save_data($thumb->data, $dest); |
franck@0
|
67 if ($file) { |
franck@0
|
68 file_set_status($file, FILE_STATUS_PERMANENT); |
franck@0
|
69 } |
franck@0
|
70 } |
franck@0
|
71 // Create an atom |
franck@0
|
72 $atom = new stdClass; |
franck@0
|
73 $atom->type = 'video'; |
franck@0
|
74 $atom->provider = 'scald_dailymotion'; |
franck@0
|
75 $atom->base_id = $infos->id; |
franck@0
|
76 $atom->publisher = $user->uid; |
franck@0
|
77 $atom->title = $infos->title; |
franck@0
|
78 $aid = scald_author_get_id(array('name' => $infos->author)); |
franck@0
|
79 $atom->authors = array($aid); |
franck@0
|
80 // And save it |
franck@0
|
81 $atom_sid = scald_register_atom((array)$atom); |
franck@0
|
82 // Finally, return this id |
franck@0
|
83 return $atom_sid; |
franck@0
|
84 } |
franck@0
|
85 |
franck@0
|
86 /** |
franck@0
|
87 * Analyze a DailyMotion RSS feed, reformating the informations about its |
franck@0
|
88 * items in an easy to manipulate objects containing the informations we're |
franck@0
|
89 * interested in. |
franck@0
|
90 * @param $type |
franck@0
|
91 * DailyMotion RSS feed type. Examples include 'user', 'video', 'tag'... |
franck@0
|
92 * @param $id |
franck@0
|
93 * The identifier, related to the type mentionned above. If you're requestion |
franck@0
|
94 * a user feed, then, its the username... |
franck@0
|
95 * @return array |
franck@0
|
96 * Each array value is an object containing the following members: |
franck@0
|
97 * - id: the DailyMotion video id |
franck@0
|
98 * - author: the DailyMotion username of the user who uploaded the video |
franck@0
|
99 * - title: the video title |
franck@0
|
100 * - thumbnail: an associative array, containing the source ('src'), width |
franck@0
|
101 * and height of the video's thumbnail |
franck@0
|
102 * - pubDate: the publication date of the video |
franck@0
|
103 */ |
franck@0
|
104 function scald_dailymotion_feed($type, $id) { |
franck@0
|
105 $url = DAILYMOTION_RSS . $type .'/'. $id; |
franck@0
|
106 $xml = drupal_http_request($url); |
franck@0
|
107 $items = array(); |
franck@0
|
108 if ($xml->code != 404 && !empty($xml->data)) { |
franck@0
|
109 $dom = DOMDocument::loadXML($xml->data); |
franck@0
|
110 if ($dom) { |
franck@0
|
111 foreach($dom->getElementsByTagName('item') as $item) { |
franck@0
|
112 $info = new stdClass(); |
franck@0
|
113 // Fetch from the feed |
franck@0
|
114 // ... the video id |
franck@0
|
115 $info->id = $item->getElementsByTagNameNS(NS_DM, 'id')->item(0)->nodeValue; |
franck@0
|
116 // ... its title |
franck@0
|
117 $title = $item->getElementsByTagName('title')->item(0); |
franck@0
|
118 $info->title = $title->nodeValue; |
franck@0
|
119 // ... and usefull informations about its thumbnails |
franck@0
|
120 $thumb = $item->getElementsByTagNameNS(NS_MEDIA, 'thumbnail')->item(0); |
franck@0
|
121 $info->thumbnail = array( |
franck@0
|
122 'src' => $thumb->getAttribute('url'), |
franck@0
|
123 'width' => $thumb->getAttribute('width'), |
franck@0
|
124 'height' => $thumb->getAttribute('height') |
franck@0
|
125 ); |
franck@0
|
126 // ... also get the author |
franck@0
|
127 $info->author = $item->getElementsByTagNameNS(NS_DM, 'author')->item(0)->nodeValue; |
franck@0
|
128 // ... and the publication date |
franck@0
|
129 $info->pubDate = date('c', strtotime($item->getElementsByTagName('pubDate')->item(0)->nodeValue)); |
franck@0
|
130 $items[] = $infos; |
franck@0
|
131 } |
franck@0
|
132 } |
franck@0
|
133 } |
franck@0
|
134 return $items; |
franck@0
|
135 } |
franck@0
|
136 |
franck@0
|
137 /** |
franck@0
|
138 * Get information on a specific video. |
franck@0
|
139 * @param $id |
franck@0
|
140 * The video id |
franck@0
|
141 * @return object |
franck@0
|
142 * An object containing the video informations. For information on |
franck@0
|
143 * the object format, see @scald_dailymotion_feed. |
franck@0
|
144 */ |
franck@0
|
145 function scald_dailymotion_video($id) { |
franck@0
|
146 $items = scald_dailymotion_feed('video', $id); |
franck@0
|
147 return $items[0]; |
franck@0
|
148 } |