comparison scald_dailymotion.module @ 0:ad3d1dbbb66e

Scald Dailymotion: First stab at a DailyMotion provider not based on node
author Franck Deroche <franck@defr.org>
date Fri, 16 Jul 2010 17:24:41 +0200
parents
children e851124eabe3
comparison
equal deleted inserted replaced
-1:000000000000 0:ad3d1dbbb66e
1 <?php
2 define('DAILYMOTION_RSS', 'http://www.dailymotion.com/rss/');
3 define('NS_MEDIA', 'http://search.yahoo.com/mrss');
4 define('NS_DM', 'http://www.dailymotion.com/dmrss');
5
6 /**
7 * Implements hook_scald_providers.
8 * Tell Scald that we'll be providing some video atoms.
9 */
10 function scald_dailymotion_scald_provider() {
11 return array(
12 'atoms' => array(
13 'video' => array('A video hosted on DailyMotion')
14 )
15 );
16 }
17
18 /**
19 * Implements hook_scald_fetch.
20 */
21 function scald_dailymotion_scald_fetch(&$atom) {
22 $atom->thumbnail_source = file_directory_path() . '/dailymotion/'. $atom->base_id .'.jpg';
23 $atom->file_source = $atom->thumbnail_source;
24 }
25
26 /**
27 * Implements hook_scald_prerender.
28 */
29 function scald_dailymotion_scald_prerender(&$atom, $context, $options, $type) {
30 if ($type == 'atom') {
31 if ($context != 'sdl_library_item') {
32 $atom->rendered->player = theme('scald_dailymotion_player', $atom->base_id, $atom->thumbnail_source);
33 }
34 }
35 }
36
37 /**
38 * Implements hook_theme.
39 */
40 function scald_dailymotion_theme() {
41 return array(
42 'scald_dailymotion_player' => array(
43 'arguments' => array('video' => NULL, 'thumbnail' => NULL),
44 'template' => 'scald_dailymotion_player'
45 )
46 );
47 }
48
49 /**
50 * Creates an atom based on a DailyMotion video id.
51 * @param $video_id
52 * Unique identifier of the video on dailymotion
53 * @return integer
54 * Unique identifier of the new atom
55 */
56 function scald_dailymotion_register($video_id) {
57 global $user;
58 // Fetch the needed informations from DailyMotion
59 $infos = scald_dailymotion_video($video_id);
60 // Download a copy of the video thumbnail. This makes it possible
61 // to do interesting things when used with ImageCache for example.
62 $thumb = drupal_http_request($infos->thumbnail['src']);
63 $dir = file_directory_path() . '/dailymotion';
64 if ($thumb->code == 200 && file_check_directory($dir)) {
65 $dest = $dir . '/' . $infos->id . '.jpg';
66 $file = file_save_data($thumb->data, $dest);
67 if ($file) {
68 file_set_status($file, FILE_STATUS_PERMANENT);
69 }
70 }
71 // Create an atom
72 $atom = new stdClass;
73 $atom->type = 'video';
74 $atom->provider = 'scald_dailymotion';
75 $atom->base_id = $infos->id;
76 $atom->publisher = $user->uid;
77 $atom->title = $infos->title;
78 $aid = scald_author_get_id(array('name' => $infos->author));
79 $atom->authors = array($aid);
80 // And save it
81 $atom_sid = scald_register_atom((array)$atom);
82 // Finally, return this id
83 return $atom_sid;
84 }
85
86 /**
87 * Analyze a DailyMotion RSS feed, reformating the informations about its
88 * items in an easy to manipulate objects containing the informations we're
89 * interested in.
90 * @param $type
91 * DailyMotion RSS feed type. Examples include 'user', 'video', 'tag'...
92 * @param $id
93 * The identifier, related to the type mentionned above. If you're requestion
94 * a user feed, then, its the username...
95 * @return array
96 * Each array value is an object containing the following members:
97 * - id: the DailyMotion video id
98 * - author: the DailyMotion username of the user who uploaded the video
99 * - title: the video title
100 * - thumbnail: an associative array, containing the source ('src'), width
101 * and height of the video's thumbnail
102 * - pubDate: the publication date of the video
103 */
104 function scald_dailymotion_feed($type, $id) {
105 $url = DAILYMOTION_RSS . $type .'/'. $id;
106 $xml = drupal_http_request($url);
107 $items = array();
108 if ($xml->code != 404 && !empty($xml->data)) {
109 $dom = DOMDocument::loadXML($xml->data);
110 if ($dom) {
111 foreach($dom->getElementsByTagName('item') as $item) {
112 $info = new stdClass();
113 // Fetch from the feed
114 // ... the video id
115 $info->id = $item->getElementsByTagNameNS(NS_DM, 'id')->item(0)->nodeValue;
116 // ... its title
117 $title = $item->getElementsByTagName('title')->item(0);
118 $info->title = $title->nodeValue;
119 // ... and usefull informations about its thumbnails
120 $thumb = $item->getElementsByTagNameNS(NS_MEDIA, 'thumbnail')->item(0);
121 $info->thumbnail = array(
122 'src' => $thumb->getAttribute('url'),
123 'width' => $thumb->getAttribute('width'),
124 'height' => $thumb->getAttribute('height')
125 );
126 // ... also get the author
127 $info->author = $item->getElementsByTagNameNS(NS_DM, 'author')->item(0)->nodeValue;
128 // ... and the publication date
129 $info->pubDate = date('c', strtotime($item->getElementsByTagName('pubDate')->item(0)->nodeValue));
130 $items[] = $infos;
131 }
132 }
133 }
134 return $items;
135 }
136
137 /**
138 * Get information on a specific video.
139 * @param $id
140 * The video id
141 * @return object
142 * An object containing the video informations. For information on
143 * the object format, see @scald_dailymotion_feed.
144 */
145 function scald_dailymotion_video($id) {
146 $items = scald_dailymotion_feed('video', $id);
147 return $items[0];
148 }