View source
<?php
define('DAILYMOTION_RSS', 'http://www.dailymotion.com/rss/');
define('DAILYMOTION_WEB', 'http://www.dailymotion.com/video/');
define('DAILYMOTION_OEMBED', 'http://www.dailymotion.com/services/oembed');
define('NS_MEDIA', 'http://search.yahoo.com/mrss');
define('NS_DM', 'http://www.dailymotion.com/dmrss');
function scald_dailymotion_scald_provider() {
return array(
'atoms' => array(
'video' => array(
'A video hosted on DailyMotion',
),
),
);
}
function scald_dailymotion_scald_fetch(&$atom, $type) {
$file = file_directory_path() . '/dailymotion/' . $atom->base_id . '.jpg';
if (file_exists($file)) {
$atom->thumbnail_source = $file;
$atom->file_source = $atom->thumbnail_source;
}
}
function scald_dailymotion_scald_prerender(&$atom, $context, $options, $type) {
if ($type == 'atom') {
if ($context != 'sdl_library_item') {
$atom->rendered->player = theme('scald_dailymotion_player', $atom->base_id, $atom->thumbnail_source);
}
}
}
function scald_dailymotion_theme() {
return array(
'scald_dailymotion_player' => array(
'arguments' => array(
'video' => NULL,
'thumbnail' => NULL,
),
'template' => 'scald_dailymotion_player',
),
'scald_dailymotion_imports_table' => array(
'arguments' => array(
'form' => NULL,
),
'file' => 'scald_dailymotion.admin.inc',
),
'scald_dailymotion_search_results_table' => array(
'arguments' => array(
'form' => NULL,
),
'file' => 'scald_dailymotion.pages.inc',
),
);
}
function scald_dailymotion_perm() {
return array(
'administer dailymotion imports',
'import dailymotion videos',
);
}
function scald_dailymotion_cron() {
$imports = variable_get('scald_dailymotion_imports', array());
foreach ($imports as $import) {
$items = scald_dailymotion_feed($import['type'], $import['value']);
foreach ($items as $item) {
scald_dailymotion_register($item);
}
}
}
function scald_dailymotion_menu() {
$items = array();
$items['admin/settings/scald_dailymotion'] = array(
'title' => 'DailyMotion imports',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'scald_dailymotion_imports_form',
),
'access arguments' => array(
'administer dailymotion imports',
),
'description' => 'Configure the videos that should be automatically imported from DailyMotion',
'file' => 'scald_dailymotion.admin.inc',
);
$items['dailymotion/search'] = array(
'title' => 'DailyMotion Video search',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'scald_dailymotion_search_form',
2,
),
'access arguments' => array(
'import dailymotion videos',
),
'description' => 'Search for new videos to import into this site',
'file' => 'scald_dailymotion.pages.inc',
);
return $items;
}
function scald_dailymotion_register($video) {
global $user;
if (is_object($video)) {
$infos = $video;
}
else {
$infos = scald_dailymotion_video($video);
}
$old = scald_dailymotion_already_imported($infos->id);
if ($old) {
return $old;
}
$thumb = drupal_http_request($infos->thumbnail['src']);
$dir = file_directory_path() . '/dailymotion';
if ($thumb->code == 200 && file_check_directory($dir, FILE_CREATE_DIRECTORY)) {
$dest = $dir . '/' . $infos->id . '.jpg';
$file = file_save_data($thumb->data, $dest);
}
$atom = new stdClass();
$atom->type = 'video';
$atom->provider = 'scald_dailymotion';
$atom->base_id = $infos->id;
$atom->publisher = $user->uid;
$atom->title = $infos->title;
$aid = scald_author_get_id(array(
'name' => $infos->author,
'url' => 'dailymotion://user/' . $info->author,
));
$atom->authors = array(
$aid,
);
$atom_sid = scald_register_atom((array) $atom);
return $atom_sid;
}
function scald_dailymotion_feed($type, $id) {
$url = DAILYMOTION_RSS . $type . '/' . urlencode($id);
$xml = drupal_http_request($url);
$items = array();
if ($xml->code != 404 && !empty($xml->data)) {
$dom = new DOMDocument();
$dom
->loadXML($xml->data);
if ($dom) {
foreach ($dom
->getElementsByTagName('item') as $item) {
$info = new stdClass();
$info->id = $item
->getElementsByTagNameNS(NS_DM, 'id')
->item(0)->nodeValue;
$title = $item
->getElementsByTagName('title')
->item(0);
$info->title = $title->nodeValue;
$thumb = $item
->getElementsByTagNameNS(NS_MEDIA, 'thumbnail')
->item(0);
$info->thumbnail = array(
'src' => $thumb
->getAttribute('url'),
'width' => $thumb
->getAttribute('width'),
'height' => $thumb
->getAttribute('height'),
);
$info->author = $item
->getElementsByTagNameNS(NS_DM, 'author')
->item(0)->nodeValue;
$info->pubDate = date('c', strtotime($item
->getElementsByTagName('pubDate')
->item(0)->nodeValue));
$items[] = $info;
}
}
}
return $items;
}
function scald_dailymotion_video($id) {
$url = DAILYMOTION_OEMBED . '?url=' . urlencode(DAILYMOTION_WEB . $id) . '&format=json';
$response = drupal_http_request($url);
if ($response->code >= 200 && $response->code < 400) {
$data = json_decode($response->data);
$item = new stdClass();
$item->id = $id;
$item->title = $data->title;
$item->thumbnail = array(
'src' => $data->thumbnail_url,
'width' => $data->thumbnail_width,
'height' => $data->thumbnail_height,
);
$item->author = $data->author_name;
}
else {
$item = FALSE;
}
return $item;
}
function scald_dailymotion_already_imported($id) {
$query = array(
'provider' => 'scald_dailymotion',
'base_id' => $id,
);
return scald_search($query, FALSE, TRUE);
}