2
2
3
3
namespace Proxy \Plugin ;
4
4
5
- use Proxy \Plugin \AbstractPlugin ;
6
5
use Proxy \Event \ProxyEvent ;
7
-
8
6
use Proxy \Html ;
7
+ use YouTube \YouTubeDownloader ;
9
8
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
+ }
73
46
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