Skip to content

Commit ce1d5d5

Browse files
committed
we are now using latest YouTubeDownloader
1 parent 41a52e6 commit ce1d5d5

File tree

2 files changed

+66
-67
lines changed

2 files changed

+66
-67
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
]
1313
},
1414
"require": {
15-
"athlon1600/youtube-downloader": "^1.0"
15+
"athlon1600/php-proxy": "^5.0",
16+
"athlon1600/youtube-downloader": "^2.0"
1617
}
1718
}

src/YoutubePlugin.php

+64-66
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,71 @@
22

33
namespace Proxy\Plugin;
44

5-
use Proxy\Plugin\AbstractPlugin;
65
use Proxy\Event\ProxyEvent;
7-
86
use Proxy\Html;
7+
use YouTube\YouTubeDownloader;
98

10-
class YoutubePlugin extends AbstractPlugin {
11-
12-
protected $url_pattern = 'youtube.com';
13-
14-
// force old YouTube layout!
15-
public function onBeforeRequest(ProxyEvent $event){
16-
$event['request']->headers->set('Cookie', 'PREF=f6=8');
17-
$event['request']->headers->set('User-Agent', 'Opera/7.50 (Windows XP; U)');
18-
}
19-
20-
public function onCompleted(ProxyEvent $event){
21-
22-
$response = $event['response'];
23-
$url = $event['request']->getUrl();
24-
$output = $response->getContent();
25-
26-
// remove top banner that's full of ads
27-
$output = Html::remove("#header", $output);
28-
29-
// do this on all youtube pages
30-
$output = preg_replace('@masthead-positioner">@', 'masthead-positioner" style="position:static;">', $output, 1);
31-
32-
// data-thumb holds real image when it is available!
33-
$output = preg_replace_callback('/<img[^>]+data-thumb="(.*?)"[^>]*/is', function($matches){
34-
35-
// may or may not have src= attribute
36-
$has_src = strpos($matches[0], 'src="') !== false;
37-
38-
// proxified thumb url
39-
$thumb_url = proxify_url($matches[1], false);
40-
41-
if($has_src){
42-
// TODO: maybe remove data-thumb too?
43-
$matches[0] = str_replace('data-thumb', 'remove-this', $matches[0]);
44-
return preg_replace('/src="(.*?)"/i', 'src_replaced="1" src="'.$thumb_url.'"', $matches[0]);
45-
}
46-
47-
return preg_replace('/data-thumb="(.*?)"/i', 'src_added="1" src="'.$thumb_url.'"', $matches[0]);
48-
}, $output);
49-
50-
$youtube = new \YouTubeDownloader();
51-
// cannot pass HTML directly because all the links in it are already "proxified"...
52-
$links = $youtube->getDownloadLinks($url, "mp4 360, mp4");
53-
54-
if($links){
55-
56-
$url = current($links)['url'];
57-
58-
$player = vid_player($url, 640, 390, 'mp4');
59-
60-
// this div blocks our player controls
61-
$output = Html::remove("#theater-background", $output);
62-
63-
// replace youtube player div block with our own
64-
$output = Html::replace_inner("#player-api", $player, $output);
65-
}
66-
67-
// causes too many problems...
68-
$output = Html::remove_scripts($output);
69-
70-
$response->setContent($output);
71-
}
72-
}
9+
class YoutubePlugin extends AbstractPlugin
10+
{
11+
protected $url_pattern = 'youtube.com';
12+
13+
// force old YouTube layout!
14+
public function onBeforeRequest(ProxyEvent $event)
15+
{
16+
$event['request']->headers->set('Cookie', 'PREF=f6=8');
17+
$event['request']->headers->set('User-Agent', 'Opera/7.50 (Windows XP; U)');
18+
}
19+
20+
public function onCompleted(ProxyEvent $event)
21+
{
22+
$response = $event['response'];
23+
$url = $event['request']->getUrl();
24+
$output = $response->getContent();
25+
26+
// remove top banner that's full of ads
27+
$output = Html::remove("#header", $output);
28+
29+
// do this on all youtube pages
30+
$output = preg_replace('@masthead-positioner">@', 'masthead-positioner" style="position:static;">', $output, 1);
31+
32+
// data-thumb holds real image when it is available!
33+
$output = preg_replace_callback('/<img[^>]+data-thumb="(.*?)"[^>]*/is', function ($matches) {
34+
35+
// may or may not have src= attribute
36+
$has_src = strpos($matches[0], 'src="') !== false;
37+
38+
// proxified thumb url
39+
$thumb_url = proxify_url($matches[1], false);
40+
41+
if ($has_src) {
42+
// TODO: maybe remove data-thumb too?
43+
$matches[0] = str_replace('data-thumb', 'remove-this', $matches[0]);
44+
return preg_replace('/src="(.*?)"/i', 'src_replaced="1" src="' . $thumb_url . '"', $matches[0]);
45+
}
7346

74-
?>
47+
return preg_replace('/data-thumb="(.*?)"/i', 'src_added="1" src="' . $thumb_url . '"', $matches[0]);
48+
}, $output);
49+
50+
$youtube = new YouTubeDownloader();
51+
// cannot pass HTML directly because all the links in it are already "proxified"...
52+
$links = $youtube->getDownloadLinks($url, "mp4 360, mp4");
53+
54+
if ($links) {
55+
56+
$url = current($links)['url'];
57+
58+
$player = vid_player($url, 640, 390, 'mp4');
59+
60+
// this div blocks our player controls
61+
$output = Html::remove("#theater-background", $output);
62+
63+
// replace youtube player div block with our own
64+
$output = Html::replace_inner("#player-api", $player, $output);
65+
}
66+
67+
// causes too many problems...
68+
$output = Html::remove_scripts($output);
69+
70+
$response->setContent($output);
71+
}
72+
}

0 commit comments

Comments
 (0)