aboutsummaryrefslogtreecommitdiff
path: root/lib/videolist.php
blob: 9e29f3061517d0c39972c8803be0b89e3c11eb47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php

define('YOUTUBE', 1);
define('VIMEO', 2);
define('METACAFE', 3);

function videolist_parseurl_youtube($url) {
	if (!preg_match('/(http:\/\/)([a-zA-Z]{2,3}\.)(youtube\.com\/)(.*)/', $url, $matches)) {
	return false;
	}

	$domain = $matches[2] . $matches[3];
	$path = $matches[4];

	if (!preg_match('/^(watch\?v=)([a-zA-Z0-9_-]*)(&.*)?$/',$path, $matches)) {
	return false;
	}

	$hash = $matches[2];
	
	return array(
		'domain' => $domain,
		'video_id' => $hash,
	);
}

function videolist_parseurl_vimeo($url) {
	if (!preg_match('/(http:\/\/)([a-zA-Z]{2,3}\.)*(vimeo\.com\/)(.*)/', $url, $matches)) {
		return false;
	}

	$domain = $matches[2] . $matches[3];
	$hash = $matches[4];

	return array(
		'domain' => $domain,
		'video_id' => $hash,
	);
}

function videolist_parseurl_metacafe($url) {
	if (!preg_match('/(http:\/\/)([a-zA-Z]{2,3}\.)(metacafe\.com\/)(.*)/', $url, $matches)) {
		return false;
	}

	$domain = $matches[2] . $matches[3];
	$path = $matches[4];

	$hash = $matches[2];

	return array(
		'domain' => $domain,
		'video_id' => $hash,
	);
}

function videolist_parseurl($url){
	if ($parsed = videolist_parseurl_youtube($url)){
		$parsed['site'] = YOUTUBE;
		return $parsed;
	} elseif ($parsed = videolist_parseurl_vimeo($url)) {
		$parsed['site'] = VIMEO;
		return $parsed;
	} elseif ($parsed = videolist_parseurl_metacafe($url)) {
		$parsed['site'] = METACAFE;
		return $parsed;
	} else {
		return array();
	}
}

function videolist_get_data($video_parsed_url) {
	$site = $video_parsed_url['site'];
	$video_id = $video_parsed_url['video_id'];
	switch($site){
		case YOUTUBE: return videolist_get_data_youtube($video_id);
		case VIMEO: return videolist_get_data_vimeo($video_id);
		case METACAFE: return videolist_get_data_metacafe($video_id);
		default: return array();
	}
}


function videolist_get_data_youtube($video_id){
	$buffer = file_get_contents('http://gdata.youtube.com/feeds/api/videos/'.$video_id);
	$xml = new SimpleXMLElement($buffer);
	
	return array(
		'title' => sanitize_string($xml->title),
		'description' => sanitize_string($xml->content),
		'thumbnail' => "http://img.youtube.com/vi/$video_id/default.jpg",
		'video_id' => $video_id,
		'videotype' => 'youtube',
	);
}

function videolist_get_data_vimeo($video_id){
	$buffer = file_get_contents("http://vimeo.com/api/v2/video/$video_id.xml");
	$xml = new SimpleXMLElement($buffer);
	
	$videos = $xml->children();
	$video = $videos[0];
	
	return array(
		'title' => sanitize_string($video->title),
		'description' => sanitize_string($video->description),
		'thumbnail' => sanitize_string($video->thumbnail_medium),
		'video_id' => $video_id,
		'videotype' => 'vimeo',
	);
}

function videolist_get_data_metacafe($video_id){ //FIXME
	$buffer = file_get_contents("http://www.metacafe.com/api/item/$video_id");
	$xml = new SimpleXMLElement($buffer);
	
	$children = $xml->children();
	$channel = $children[1];
	
	preg_match('/<img[^>]+src[\\s=\'"]+([^"\'>\\s]+)/is', $channel->description, $matches);
	
	return array(
		'title' => $channel->title,
		'description' => $channel->description,
		'thumbnail' => $matches[1],
		'video_id' => $video_id,
		'videotype' => 'metacafe',
	);
}