11
11
from pywb .utils .wbexception import WbException
12
12
13
13
import json
14
- import requests
15
14
import hashlib
16
15
17
16
@@ -28,19 +27,17 @@ class RewriteHandler(SearchPageWbUrlHandler):
28
27
29
28
YT_DL_TYPE = 'application/vnd.youtube-dl_formats+json'
30
29
31
- youtubedl = None
32
-
33
30
def __init__ (self , config ):
34
31
super (RewriteHandler , self ).__init__ (config )
35
32
36
33
proxyhostport = config .get ('proxyhostport' )
37
34
38
35
live_rewriter_cls = config .get ('live_rewriter_cls' , LiveRewriter )
39
36
40
- self .rewriter = live_rewriter_cls (is_framed_replay = self .is_frame_mode ,
41
- proxies = proxyhostport )
37
+ self .live_fetcher = live_rewriter_cls (is_framed_replay = self .is_frame_mode ,
38
+ proxies = proxyhostport )
42
39
43
- self .proxies = self .rewriter . proxies
40
+ self .recording = self .live_fetcher . is_recording ()
44
41
45
42
self .head_insert_view = HeadInsertView .init_from_config (config )
46
43
@@ -73,7 +70,7 @@ def handle_request(self, wbrequest):
73
70
def _live_request_headers (self , wbrequest ):
74
71
return {}
75
72
76
- def _ignore_proxies (self , wbrequest ):
73
+ def _skip_recording (self , wbrequest ):
77
74
return False
78
75
79
76
def render_content (self , wbrequest ):
@@ -87,7 +84,7 @@ def render_content(self, wbrequest):
87
84
if ref_wburl_str :
88
85
wbrequest .env ['REL_REFERER' ] = WbUrl (ref_wburl_str ).url
89
86
90
- ignore_proxies = self ._ignore_proxies (wbrequest )
87
+ skip_recording = self ._skip_recording (wbrequest )
91
88
92
89
use_206 = False
93
90
url = None
@@ -96,7 +93,7 @@ def render_content(self, wbrequest):
96
93
readd_range = False
97
94
cache_key = None
98
95
99
- if self .proxies and not ignore_proxies :
96
+ if self .recording and not skip_recording :
100
97
rangeres = wbrequest .extract_range ()
101
98
102
99
if rangeres :
@@ -110,17 +107,17 @@ def render_content(self, wbrequest):
110
107
readd_range = True
111
108
else :
112
109
# disables proxy
113
- ignore_proxies = True
110
+ skip_recording = True
114
111
115
112
# sets cache_key only if not already cached
116
113
cache_key = self ._get_cache_key ('r:' , url )
117
114
118
- result = self .rewriter .fetch_request (wbrequest .wb_url .url ,
115
+ result = self .live_fetcher .fetch_request (wbrequest .wb_url .url ,
119
116
wbrequest .urlrewriter ,
120
117
head_insert_func = head_insert_func ,
121
118
req_headers = req_headers ,
122
119
env = wbrequest .env ,
123
- ignore_proxies = ignore_proxies ,
120
+ skip_recording = skip_recording ,
124
121
verify = self .verify )
125
122
126
123
wbresponse = self ._make_response (wbrequest , * result )
@@ -135,8 +132,8 @@ def render_content(self, wbrequest):
135
132
except (ValueError , TypeError ):
136
133
pass
137
134
138
- if cache_key :
139
- self ._add_proxy_ping (cache_key , url , wbrequest , wbresponse )
135
+ if self . recording and cache_key :
136
+ self ._add_rec_ping (cache_key , url , wbrequest , wbresponse )
140
137
141
138
if rangeres :
142
139
referrer = wbrequest .env .get ('REL_REFERER' )
@@ -183,7 +180,7 @@ def create_cache_key(prefix, url):
183
180
key = prefix + key
184
181
return key
185
182
186
- def _add_proxy_ping (self , key , url , wbrequest , wbresponse ):
183
+ def _add_rec_ping (self , key , url , wbrequest , wbresponse ):
187
184
def do_ping ():
188
185
headers = self ._live_request_headers (wbrequest )
189
186
headers ['Connection' ] = 'close'
@@ -192,15 +189,8 @@ def do_ping():
192
189
# mark as pinged
193
190
self ._cache [key ] = '1'
194
191
195
- resp = requests .get (url = url ,
196
- headers = headers ,
197
- proxies = self .proxies ,
198
- verify = False ,
199
- stream = True )
192
+ self .live_fetcher .fetch_async (url , headers )
200
193
201
- # don't actually read whole response,
202
- # proxy response for writing it
203
- resp .close ()
204
194
except :
205
195
del self ._cache [key ]
206
196
raise
@@ -219,20 +209,17 @@ def wrap_buff_gen(gen):
219
209
return wbresponse
220
210
221
211
def _get_video_info (self , wbrequest , info_url = None , video_url = None ):
222
- if not self .youtubedl :
223
- self .youtubedl = YoutubeDLWrapper ()
224
-
225
212
if not video_url :
226
213
video_url = wbrequest .wb_url .url
227
214
228
215
if not info_url :
229
216
info_url = wbrequest .wb_url .url
230
217
231
218
cache_key = None
232
- if self .proxies :
219
+ if self .recording :
233
220
cache_key = self ._get_cache_key ('v:' , video_url )
234
221
235
- info = self .youtubedl . extract_info (video_url )
222
+ info = self .live_fetcher . get_video_info (video_url )
236
223
if info is None : #pragma: no cover
237
224
msg = ('youtube-dl is not installed, pip install youtube-dl to ' +
238
225
'enable improved video proxy' )
@@ -244,42 +231,14 @@ def _get_video_info(self, wbrequest, info_url=None, video_url=None):
244
231
content_type = self .YT_DL_TYPE
245
232
metadata = json .dumps (info )
246
233
247
- if (self .proxies and cache_key ):
234
+ if (self .recording and cache_key ):
248
235
headers = self ._live_request_headers (wbrequest )
249
236
headers ['Content-Type' ] = content_type
250
237
251
238
info_url = HttpsUrlRewriter .remove_https (info_url )
252
239
253
- response = requests .request (method = 'PUTMETA' ,
254
- url = info_url ,
255
- data = metadata ,
256
- headers = headers ,
257
- proxies = self .proxies ,
258
- verify = False )
240
+ response = self .live_fetcher .add_metadata (info_url , headers , metadata )
259
241
260
242
self ._cache [cache_key ] = '1'
261
243
262
244
return WbResponse .text_response (metadata , content_type = content_type )
263
-
264
-
265
- #=================================================================
266
- class YoutubeDLWrapper (object ): #pragma: no cover
267
- """ YoutubeDL wrapper, inits youtubee-dl if it is available
268
- """
269
- def __init__ (self ):
270
- try :
271
- from youtube_dl import YoutubeDL as YoutubeDL
272
- except ImportError :
273
- self .ydl = None
274
- return
275
-
276
- self .ydl = YoutubeDL (dict (simulate = True ,
277
- youtube_include_dash_manifest = False ))
278
- self .ydl .add_default_info_extractors ()
279
-
280
- def extract_info (self , url ):
281
- if not self .ydl :
282
- return None
283
-
284
- info = self .ydl .extract_info (url )
285
- return info
0 commit comments