forked from pyload/pyload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasePlugin.py
More file actions
113 lines (94 loc) · 4 KB
/
BasePlugin.py
File metadata and controls
113 lines (94 loc) · 4 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from urlparse import urlparse
from re import search
from urllib import unquote
from module.network.HTTPRequest import BadHeader
from module.plugins.Hoster import Hoster
from module.utils import html_unescape, remove_chars
class BasePlugin(Hoster):
__name__ = "BasePlugin"
__type__ = "hoster"
__pattern__ = r"^unmatchable$"
__version__ = "0.19"
__description__ = """Base Plugin when any other didnt fit"""
__author_name__ = ("RaNaN")
__author_mail__ = ("RaNaN@pyload.org")
def setup(self):
self.chunkLimit = -1
self.resumeDownload = True
def process(self, pyfile):
"""main function"""
#debug part, for api exerciser
if pyfile.url.startswith("DEBUG_API"):
self.multiDL = False
return
# self.__name__ = "NetloadIn"
# pyfile.name = "test"
# self.html = self.load("http://localhost:9000/short")
# self.download("http://localhost:9000/short")
# self.api = self.load("http://localhost:9000/short")
# self.decryptCaptcha("http://localhost:9000/captcha")
#
# if pyfile.url == "79":
# self.core.api.addPackage("test", [str(i) for i in range(80)], 1)
#
# return
if pyfile.url.startswith("http"):
try:
self.downloadFile(pyfile)
except BadHeader, e:
if e.code in (401, 403):
self.logDebug("Auth required")
account = self.core.accountManager.getAccountPlugin('Http')
servers = [x['login'] for x in account.getAllAccounts()]
server = urlparse(pyfile.url).netloc
if server in servers:
self.logDebug("Logging on to %s" % server)
self.req.addAuth(account.accounts[server]["password"])
else:
for pwd in pyfile.package().password.splitlines():
if ":" in pwd:
self.req.addAuth(pwd.strip())
break
else:
self.fail(_("Authorization required (username:password)"))
self.downloadFile(pyfile)
else:
raise
else:
self.fail("No Plugin matched and not a downloadable url.")
def downloadFile(self, pyfile):
url = pyfile.url
for i in range(5):
header = self.load(url, just_header=True)
# self.load does not raise a BadHeader on 404 responses, do it here
if 'code' in header and header['code'] == 404:
raise BadHeader(404)
if 'location' in header:
self.logDebug("Location: " + header['location'])
base = search(r'https?://[^/]+', url).group(0)
if header['location'].startswith("http"):
url = unquote(header['location'])
elif header['location'].startswith("/"):
url = base + unquote(header['location'])
else:
url = '%s/%s' % (base, unquote(header['location']))
else:
break
name = html_unescape(unquote(urlparse(url).path.split("/")[-1]))
if 'content-disposition' in header:
self.logDebug("Content-Disposition: " + header['content-disposition'])
m = search("filename(?P<type>=|\*=(?P<enc>.+)'')(?P<name>.*)", header['content-disposition'])
if m:
disp = m.groupdict()
self.logDebug(disp)
if not disp['enc']:
disp['enc'] = 'utf-8'
name = remove_chars(disp['name'], "\"';").strip()
name = unicode(unquote(name), disp['enc'])
if not name:
name = url
pyfile.name = name
self.logDebug("Filename: %s" % pyfile.name)
self.download(url, disposition=True)