forked from pyload/pyload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase.py
More file actions
529 lines (368 loc) · 14.5 KB
/
Copy pathBase.py
File metadata and controls
529 lines (368 loc) · 14.5 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# -*- coding: utf-8 -*-
import inspect
import os
import re
import time
import urlparse
from module.plugins.internal.Captcha import Captcha
from module.plugins.internal.Plugin import Plugin, Abort, Fail, Reconnect, Retry, Skip
from module.plugins.internal.misc import (decode, encode, fixurl, format_size, format_time,
parse_html_form, parse_name, replace_patterns)
#@TODO: Recheck in 0.4.10
def getInfo(urls):
#: result = [ .. (name, size, status, url) .. ]
pass
#@TODO: Remove in 0.4.10
def parse_fileInfo(klass, url="", html=""):
info = klass.get_info(url, html)
return encode(info['name']), info['size'], info['status'], info['url']
class Base(Plugin):
__name__ = "Base"
__type__ = "base"
__version__ = "0.27"
__status__ = "stable"
__pattern__ = r'^unmatchable$'
__config__ = [("activated" , "bool", "Activated" , True),
("use_premium", "bool", "Use premium account if available", True)]
__description__ = """Base plugin for Hoster and Crypter"""
__license__ = "GPLv3"
URL_REPLACEMENTS = []
@classmethod
def get_info(cls, url="", html=""):
url = fixurl(url, unquote=True)
info = {'name' : parse_name(url),
'hash' : {},
'pattern': {},
'size' : 0,
'status' : 7 if url else 8,
'url' : replace_patterns(url, cls.URL_REPLACEMENTS)}
try:
info['pattern'] = re.match(cls.__pattern__, url).groupdict()
except Exception:
pass
return info
def __init__(self, pyfile):
self._init(pyfile.m.core)
#: Engage wan reconnection
self.wantReconnect = False #@TODO: Change to `want_reconnect` in 0.4.10
#: Enable simultaneous processing of multiple downloads
self.multiDL = True #@TODO: Change to `multi_dl` in 0.4.10
#: time.time() + wait in seconds
self.waiting = False
#: Account handler instance, see :py:class:`Account`
self.account = None
self.user = None #@TODO: Remove in 0.4.10
self.premium = None
#: Associated pyfile instance, see `PyFile`
self.pyfile = pyfile
#: Holds thread in future
self.thread = None
#: Js engine, see `JsEngine`
self.js = self.pyload.js
#: Captcha stuff
#@TODO: Replace in 0.4.10:
#_Captcha = self.pyload.pluginManager.loadClass("captcha", self.classname) or Captcha
# self.captcha = _Captcha(pyfile)
self.captcha = Captcha(pyfile)
#: Some plugins store html code here
self.data = ""
#: Dict of the amount of retries already made
self.retries = {}
self.init_base()
self.init()
def _log(self, level, plugintype, pluginname, messages):
log = getattr(self.pyload.log, level)
msg = u" | ".join(decode(a).strip() for a in messages if a)
#: Hide any user/password
try:
msg = msg.replace(self.account.user, self.account.user[:3] + "*******")
except Exception:
pass
try:
msg = msg.replace(self.account.info['login']['password'], "**********")
except Exception:
pass
log("%(plugintype)s %(pluginname)s[%(id)s]: %(msg)s" %
{'plugintype': plugintype.upper(),
'pluginname': pluginname,
'id' : self.pyfile.id,
'msg' : msg})
def init_base(self):
pass
def setup_base(self):
pass
def setup(self):
"""
Setup for enviroment and other things, called before downloading (possibly more than one time)
"""
pass
def _setup(self):
#@TODO: Remove in 0.4.10
self.pyfile.error = ""
self.data = ""
self.last_html = ""
self.last_header = {}
if self.config.get('use_premium', True):
self.load_account() #@TODO: Move to PluginThread in 0.4.10
else:
self.account = False
self.user = None #@TODO: Remove in 0.4.10
try:
self.req.close()
except Exception:
pass
if self.account:
self.req = self.pyload.requestFactory.getRequest(self.classname, self.account.user)
self.premium = self.account.info['data']['premium'] #@NOTE: Avoid one unnecessary get_info call by `self.account.premium` here
else:
self.req = self.pyload.requestFactory.getRequest(self.classname)
self.premium = False
self.req.setOption("timeout", 60) #@TODO: Remove in 0.4.10
self.setup_base()
self.grab_info()
self.setup()
self.check_status()
def load_account(self):
if not self.account:
self.account = self.pyload.accountManager.getAccountPlugin(self.classname)
if not self.account:
self.account = False
self.user = None #@TODO: Remove in 0.4.10
else:
self.account.choose()
self.user = self.account.user #@TODO: Remove in 0.4.10
if self.account.user is None:
self.account = False
def _update_name(self):
name = self.info.get('name')
if name and name != self.info.get('url'):
self.pyfile.name = name
else:
name = self.pyfile.name
self.log_info(_("Link name: ") + name)
def _update_size(self):
size = self.info.get('size')
if size > 0:
self.pyfile.size = int(self.info.get('size')) #@TODO: Fix int conversion in 0.4.10
else:
size = self.pyfile.size
if size:
self.log_info(_("Link size: %s (%s bytes)") % (format_size(size), size))
else:
self.log_info(_("Link size: N/D"))
def _update_status(self):
self.pyfile.status = self.info.get('status', 14)
self.pyfile.sync()
self.log_info(_("Link status: ") + self.pyfile.getStatusName())
def sync_info(self):
self._update_name()
self._update_size()
self._update_status()
def grab_info(self):
self.log_info(_("Grabbing link info..."))
old_info = dict(self.info)
new_info = self.get_info(self.pyfile.url, self.data)
self.info.update(new_info)
self.log_debug("Link info: %s" % self.info)
self.log_debug("Previous link info: %s" % old_info)
self.sync_info()
def check_status(self):
status = self.pyfile.status
if status == 1:
self.offline()
elif status == 4:
self.skip(self.pyfile.statusname)
elif status == 6:
self.temp_offline()
elif status == 8:
self.fail()
elif status == 9 or self.pyfile.abort:
self.abort()
def _initialize(self):
self.log_debug("Plugin version: " + self.__version__)
self.log_debug("Plugin status: " + self.__status__)
if self.__status__ == "broken":
self.abort(_("Plugin is temporarily unavailable"))
elif self.__status__ == "testing":
self.log_warning(_("Plugin may be unstable"))
def _process(self, thread):
"""
Handles important things to do before starting
"""
self.thread = thread
self._initialize()
self._setup()
#@TODO: Enable in 0.4.10
# self.pyload.hookManager.downloadPreparing(self.pyfile)
# self.check_status()
self.pyfile.setStatus("starting")
self.log_info(_("Processing url: ") + self.pyfile.url)
self.process(self.pyfile)
self.check_status()
#: Deprecated method, use `_process` instead (Remove in 0.4.10)
def preprocessing(self, *args, **kwargs):
#@NOTE: Set pyfile status from `queued` to `starting` as soon as possible to avoid race condition in ThreadManager's assignJob function
#@NOTE: Move to ThreadManager in 0.4.10
self.pyfile.setStatus("starting")
#@NOTE: Recheck info thread synchronization in 0.4.10
return self._process(*args, **kwargs)
def process(self, pyfile):
"""
The "main" method of every hoster plugin, you **have to** overwrite it
"""
raise NotImplementedError
def set_reconnect(self, reconnect):
self.log_debug("RECONNECT %s required" % ("" if reconnect else "not"),
"Previous wantReconnect: %s" % self.wantReconnect)
self.wantReconnect = bool(reconnect)
return True
def set_wait(self, seconds, strict=False):
"""
Set a specific wait time later used with `wait`
:param seconds: wait time in seconds
:param reconnect: True if a reconnect would avoid wait time
"""
wait_time = float(seconds)
if wait_time < 0:
return False
old_wait_until = self.pyfile.waitUntil
new_wait_until = time.time() + wait_time + float(not strict)
self.log_debug("WAIT set to timestamp %f" % new_wait_until,
"Previous waitUntil: %f" % old_wait_until)
self.pyfile.waitUntil = new_wait_until
return True
def wait(self, seconds=None, reconnect=None):
"""
Waits the time previously set
"""
if seconds is not None:
self.set_wait(seconds)
if reconnect is not None:
self.set_reconnect(reconnect)
wait_time = self.pyfile.waitUntil - time.time()
if wait_time < 1:
self.log_warning(_("Invalid wait time interval"))
return
self.waiting = True
status = self.pyfile.status #@NOTE: Recheck in 0.4.10
self.pyfile.setStatus("waiting")
self.log_info(_("Waiting %s...") % format_time(wait_time))
if self.wantReconnect:
self.log_info(_("Requiring reconnection..."))
if self.account:
self.log_warning(_("Reconnection ignored due logged account"))
if not self.wantReconnect or self.account:
while self.pyfile.waitUntil > time.time():
self.check_status()
time.sleep(2)
else:
while self.pyfile.waitUntil > time.time():
self.check_status()
self.thread.m.reconnecting.wait(1)
if self.thread.m.reconnecting.isSet():
self.waiting = False
self.wantReconnect = False
self.req.clearCookies()
raise Reconnect
time.sleep(2)
self.waiting = False
self.pyfile.status = status #@NOTE: Recheck in 0.4.10
def skip(self, msg=""):
"""
Skip and give msg
"""
raise Skip(encode(msg or self.pyfile.error or self.pyfile.pluginname)) #@TODO: Remove `encode` in 0.4.10
#@TODO: Remove in 0.4.10
def fail(self, msg=""):
"""
Fail and give msg
"""
msg = msg.strip()
if msg:
self.pyfile.error = msg
else:
msg = self.pyfile.error or self.info.get('error') or self.pyfile.getStatusName()
raise Fail(encode(msg)) #@TODO: Remove `encode` in 0.4.10
def error(self, msg="", type=_("Parse")):
type = _("%s error") % type.strip().capitalize() if type else _("Unknown")
msg = _("%(type)s: %(msg)s | Plugin may be out of date"
% {'type': type, 'msg': msg or self.pyfile.error})
self.fail(msg)
def abort(self, msg=""):
"""
Abort and give msg
"""
if msg: #@TODO: Remove in 0.4.10
self.pyfile.error = encode(msg)
raise Abort
#@TODO: Recheck in 0.4.10
def offline(self, msg=""):
"""
Fail and indicate file is offline
"""
self.fail("offline")
#@TODO: Recheck in 0.4.10
def temp_offline(self, msg=""):
"""
Fail and indicates file ist temporary offline, the core may take consequences
"""
self.fail("temp. offline")
def restart(self, msg="", premium=True):
if not msg:
msg = _("Restart plugin") if premium else _("Fallback to free processing")
if not premium:
if self.premium:
self.restart_free = True
else:
self.fail("%s | %s" % (msg, _("Url was already processed as free")))
self.req.clearCookies()
raise Retry(encode(msg)) #@TODO: Remove `encode` in 0.4.10
def retry(self, attemps=5, wait=1, msg=""):
"""
Retries and begin again from the beginning
:param attemps: number of maximum retries
:param wait: time to wait in seconds before retry
:param msg: message passed to fail if attemps value was reached
"""
frame = inspect.currentframe()
try:
id = frame.f_back.f_lineno
finally:
del frame #: Delete the frame or it wont be cleaned
if id not in self.retries:
self.retries[id] = 0
if 0 < attemps <= self.retries[id]:
self.fail(msg or _("Max retries reached"))
self.wait(wait, False)
self.retries[id] += 1
raise Retry(encode(msg)) #@TODO: Remove `encode` in 0.4.10
def retry_captcha(self, attemps=10, wait=1, msg=_("Max captcha retries reached")):
self.captcha.invalid()
self.retry(attemps, wait, msg)
def fixurl(self, url, baseurl=None, unquote=True):
url = fixurl(url, unquote=True)
baseurl = fixurl(baseurl or self.pyfile.url, unquote=True)
if not urlparse.urlparse(url).scheme:
url_p = urlparse.urlparse(baseurl)
baseurl = "%s://%s" % (url_p.scheme, url_p.netloc)
url = urlparse.urljoin(baseurl, url)
return fixurl(url, unquote)
def load(self, *args, **kwargs):
self.check_status()
return super(Base, self).load(*args, **kwargs)
def parse_html_form(self, attr_str="", input_names={}):
return parse_html_form(attr_str, self.data, input_names)
def get_password(self):
"""
Get the password the user provided in the package
"""
return self.pyfile.package().password or ""
def clean(self):
"""
Clean everything and remove references
"""
super(Base, self).clean()
for attr in ("account", "html", "pyfile", "thread"):
if hasattr(self, attr):
setattr(self, attr, None)