1 | <?php |
||
11 | class Youtubecom extends Http |
||
12 | { |
||
13 | |||
14 | 3 | public function fetchLinkList($link) |
|
15 | { |
||
16 | 3 | if (!Util::startsWith($link, 'https://www.youtube.com/playlist?')) { |
|
17 | 1 | return parent::fetchLinkList($link); |
|
18 | } |
||
19 | |||
20 | 2 | $resp = $this->getClient()->request('get', $link); |
|
21 | |||
22 | 2 | $crawler = new Crawler($resp->getBody()->getContents()); |
|
23 | |||
24 | // Extracting the links which belong to the playlist |
||
25 | 2 | $links = $crawler->filter('a.pl-video-title-link')->extract([ |
|
26 | 'href' |
||
27 | 2 | ]); |
|
28 | |||
29 | 2 | $data = []; |
|
30 | 2 | foreach ($links as $link) { |
|
31 | 2 | $uri = parse_url($link); |
|
32 | |||
33 | 2 | if (!key_exists('path', $uri) || $uri['path'] != '/watch') { |
|
34 | 1 | continue; |
|
35 | } |
||
36 | |||
37 | 1 | $params = []; |
|
38 | 1 | parse_str($uri['query'], $params); |
|
39 | 1 | if (key_exists('v', $params)) { |
|
40 | 1 | $data[] = 'https://www.youtube.com/watch?v=' . $params['v']; |
|
41 | } |
||
42 | } |
||
43 | 2 | return array_unique($data); |
|
44 | } |
||
45 | |||
46 | 4 | public function fetchDownloadInfo(array $downloads) |
|
47 | { |
||
48 | 4 | foreach ($downloads as $download) { |
|
49 | // Connection check |
||
50 | try { |
||
51 | 4 | $data = $this->getStreamData($download); |
|
52 | 3 | if (!empty($data['title']) && empty($download->getFileName())) { |
|
53 | 1 | $download->setFileName($this->makeSafe($data['title'] . '.mp4')); |
|
54 | } |
||
55 | |||
56 | 3 | if (key_exists('errorcode', $data) && $data['errorcode'] > 0 && key_exists('reason', $data)) { |
|
57 | 1 | $download->setMessage($data['reason']); |
|
58 | 3 | $download->setState(Download::STATE_DOWNLOADING_ERROR); |
|
59 | } |
||
60 | 1 | } catch (\Exception $e) { |
|
61 | 1 | $this->log('Exception fetching head for connection test: ' . $e->getMessage()); |
|
62 | 1 | $download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_URL'); |
|
63 | 4 | $download->setState(Download::STATE_DOWNLOADING_ERROR); |
|
64 | } |
||
65 | } |
||
66 | 4 | } |
|
67 | |||
68 | 3 | protected function getUrlToDownload(Download $download) |
|
69 | { |
||
70 | 3 | $data = $this->getStreamData($download); |
|
71 | |||
72 | 3 | if (!key_exists('url_encoded_fmt_stream_map', $data)) { |
|
73 | 1 | return null; |
|
74 | } |
||
75 | |||
76 | 2 | foreach (explode(',', $data['url_encoded_fmt_stream_map']) as $streamData) { |
|
77 | 2 | parse_str($streamData, $streamData); |
|
78 | |||
79 | 2 | if (key_exists('url', $streamData)) { |
|
80 | 1 | $url = urldecode($streamData['url']); |
|
81 | 1 | $this->log('Youtube real url: ' . $url); |
|
82 | 2 | return $url; |
|
83 | } |
||
84 | } |
||
85 | 1 | return null; |
|
86 | } |
||
87 | |||
88 | 7 | private function getStreamData(Download $download) |
|
133 | |||
134 | /** |
||
135 | * |
||
136 | * @param string $file |
||
137 | * @return string |
||
138 | * @see http://stackoverflow.com/questions/2668854/sanitizing-strings-to-make-them-url-and-filename-safe |
||
139 | * @see http://stackoverflow.com/a/24812812/356375 |
||
140 | */ |
||
141 | 1 | private function makeSafe($file) |
|
154 | } |
||
155 |