Tudou.class.php
5.4 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
class Util_Video_Api_Tudou extends Util_Video_Api
{
private $jsonurl = "http://www.tudou.com/playlist/service/getPlaylistBigPic.html?lid={id}";
private $htmlurl = "http://www.tudou.com/playlist/playindex.do?lid={id}";
private $getflashurl = "http://www.tudou.com/programs/share2.php?lid={id}";
private $swf_url_1 = "http://www.tudou.com/v/{id}/v.swf";
const TUDOU_VIDEO_PID = 'c57a52a5393e16ca'; // 土豆视频PID
private static $tudou_video_api = 'http://api.tudou.com/v3/gw?method=repaste.info.get&appKey=%s&format=json&url=%s'; // 土豆视频接口
protected function getDomain()
{
return "tudou.com";
}
public function getData($url)
{
if (empty ( $url ))
{
throw new Util_Video_ApiException('没有数据');
}
$api_url = sprintf ( self::$tudou_video_api, self::TUDOU_VIDEO_PID, urlencode ( $url ) );
$result = Util_Video_ApiConnect::get($api_url);
if($result == '5001 : 服务错误' || $result=='{}' || $result === false)
{
return $this->getViolentData($url);
}
else
{
$result = json_decode($result);
$data = new Util_Video_Data($this->id);
if(@$result->repasteInfo->playlistInfo->outerPlayerUrl && @$result->repasteInfo->playlistInfo->title && @$result->repasteInfo->playlistInfo->playlistPicUrl)
{
$data->swf = $result->repasteInfo->playlistInfo->outerPlayerUrl;
$data->title = htmlspecialchars ( $result->repasteInfo->playlistInfo->title );
$data->thumb = $result->repasteInfo->playlistInfo->playlistPicUrl;
}
else
{
$data->swf = $result->repasteInfo->itemInfo->outerPlayerUrl;
$data->title = htmlspecialchars ( $result->repasteInfo->itemInfo->title );
$data->thumb = $result->repasteInfo->itemInfo->picUrl;
}
return $data;
}
}
public function getViolentData($url)
{
$dataobj = new Util_Video_Data($this->id);
if(preg_match_all("/programs\/view\/(.*)$/si", $url, $vid, 2))
{
$vid = trim(preg_replace("/\/.*$/si", "", preg_replace("/\\.*$/si", "", $vid[0][1])));
if(empty($vid))
{
throw new Util_Video_ApiException('url解析出错');
}
$swfurl = str_ireplace("{id}", $vid, $this->swf_url_1);
$urlarr = get_headers($swfurl);
$daturl = "";
if(is_array($urlarr))
{
foreach($urlarr as $v)
{
if(preg_match_all("/\s*Location\s*:(.*)/", $v, $date, 2))
{
$daturl = urldecode(trim($date[0][1]));
}
}
}
if(empty($daturl))
{
throw new Util_Video_ApiException("url解析出错");
}
$pas = parse_url($daturl);
if(empty($pas['query']))
{
throw new Util_Video_ApiException("url解析出错");
}
parse_str($pas['query'], $qdata);
if(isset($qdata['tag']))
{
$tag = explode(",", trim($qdata['tag']));
}
$dataobj->id = isset($qdata['iid']) ? $qdata['iid'] : 0;
$dataobj->thumb = isset($qdata['snap_pic']) ? $qdata['snap_pic'] : "";
$dataobj->time = isset($qdata['totalTime']) ? $qdata['totalTime'] : "";
$dataobj->tags = isset($tag) ? $tag : array();
$dataobj->swf = $swfurl;
$dataobj->title = isset($qdata['title']) ? $qdata['title'] : "";
}
else if(preg_match("/([0-9]+)/si", $url))
{
$idandswf = $this->getid($url);
$jsonurl = str_ireplace("{id}", $this->id, $this->jsonurl);
$dat = Util_Video_ApiConnect::get($jsonurl, array(
CURLOPT_ENCODING => "gzip"
));
$jsonobj = json_decode($dat);
$dataobj->thumb = $jsonobj->message;
$dataobj->time = 0;
$dataobj->tags = array();
$dataobj->swf = $idandswf['swf'];
$dataobj->title = $idandswf['title'];
}
else
{
throw new Util_Video_ApiException("url解析出错");
}
return $dataobj;
}
private function getid($url)
{
$id = $this->getUrl($url);
$titledb = Util_Video_ApiConnect::get($url, array(
CURLOPT_ENCODING => "gzip"
));
if(preg_match_all("/<\s*title\s*>(.*)<\s*\/title\s*>/siU", $titledb, $title, 2))
{
$title = $title[0][1];
}
if(preg_match_all("/,lid.*=(.*),/isU", $titledb, $lid, 2))
{
$id = trim($lid[0][1]);
}
$url = str_ireplace("{id}", $id, $this->getflashurl);
$content = Util_Video_ApiConnect::get($url, array(
CURLOPT_ENCODING => "gzip"
));
if(preg_match_all("/<\s*embed\s*src=\"(.*)\"/siU", $content, $flashurl, 2))
{
$swf = $flashurl[0][1];
}
else
{
throw new Util_Video_ApiException("解析url出错");
}
$this->id = $id;
return array(
"swf" => $swf,
"id" => $id,
"title" => iconv("GBK", "UTF-8", $title)
);
}
}
?>