Repository navigation
Expand file tree
/
Copy pathplugin.py
More file actions
342 lines (277 loc) · 9.93 KB
/
Copy pathplugin.py
File metadata and controls
342 lines (277 loc) · 9.93 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
import inspect
import os
import sys
import pycurl
from pyload.core.network.exceptions import Fail, Skip
from pyload.core.network.request_factory import get_request
from pyload.core.utils import fs
from pyload.core.utils.old import fixurl
from ..helpers import DB, Config, exists
if os.name != "nt":
import grp
import pwd
class BasePlugin:
__name__ = "BasePlugin"
__type__ = "base"
__version__ = "0.79"
__status__ = "stable"
__config__ = [] #: [("name", "type", "desc", "default")]
__description__ = """Base plugin"""
__license__ = "GPLv3"
__authors__ = [("Walter Purcaro", "vuolter@gmail.com")]
def __init__(self, core):
self._init(core)
self.init()
def __repr__(self):
return "<{type} {name}>".format(
type=self.__type__.capitalize(), name=self.classname
)
@property
def classname(self):
return self.__class__.__name__
def _init(self, core):
#: Internal modules
self.pyload = core
self._ = core._
self.db = DB(self)
self.config = Config(self)
#: Provide information in dict here
self.info = {}
#: Browser instance, see `network.Browser`
self.req = self.pyload.request_factory.get_request(self.classname)
#: Last loaded html
self.last_html = ""
self.last_header = {}
def init(self):
"""
Initialize the plugin (in addition to `__init__`)
"""
pass
# TODO: Rewrite to use unique logger from logfactory
def _log(self, level, plugintype, pluginname, args, kwargs):
log = getattr(self.pyload.log, level)
log(
"{plugintype} {pluginname}: {msg}".format(
plugintype=plugintype.upper(),
pluginname=pluginname,
msg=" | ".join(["%s"] * len(args)),
),
*args,
**kwargs,
)
def log_debug(self, *args, **kwargs):
self._log("debug", self.__type__, self.__name__, args, kwargs)
def log_info(self, *args, **kwargs):
self._log("info", self.__type__, self.__name__, args, kwargs)
def log_warning(self, *args, **kwargs):
self._log("warning", self.__type__, self.__name__, args, kwargs)
def log_error(self, *args, **kwargs):
kwargs["exc_info"] = kwargs.get(
"exc_info", self.pyload.debug > 1 and sys.exc_info() != (None, None, None)
)
kwargs["stack_info"] = kwargs.get("stack_info", self.pyload.debug > 2)
self._log("error", self.__type__, self.__name__, args, kwargs)
def log_critical(self, *args, **kwargs):
kwargs["exc_info"] = kwargs.get(
"exc_info", sys.exc_info() != (None, None, None)
)
kwargs["stack_info"] = kwargs.get("stack_info", True)
self._log("critical", self.__type__, self.__name__, args, kwargs)
# def _print_exc(self):
# frame = inspect.currentframe()
# try:
# print(format_exc(frame.f_back))
# finally:
# del frame
def remove(self, path, try_trash=True): # TODO: Change to `trash=True` in 0.6.x
try:
fs.remove(path, try_trash=try_trash)
except (NameError, OSError) as exc:
self.log_warning(
self._("Error removing `{}`").format(os.path.realpath(path)), exc
)
return False
else:
self.log_info(self._("Path deleted: ") + os.path.realpath(path))
return True
def set_permissions(self, path):
path = os.fsdecode(path)
if not exists(path):
return
if self.pyload.config.get("permission", "change_file"):
permission = self.pyload.config.get(
"permission", "folder" if os.path.isdir(path) else "file"
)
mode = int(permission, 8)
os.chmod(path, mode)
if os.name != "nt" and self.pyload.config.get("permission", "change_dl"):
uid = pwd.getpwnam(self.pyload.config.get("permission", "user"))[2]
gid = grp.getgrnam(self.pyload.config.get("permission", "group"))[2]
os.chown(path, uid, gid)
def skip(self, msg):
"""
Skip and give msg.
"""
raise Skip(msg)
def fail(self, msg):
"""
Fail and give msg.
"""
raise Fail(msg)
def load(
self,
url,
get=None,
post=None,
referrer=True,
cookies=True,
just_header=False,
decode=True,
multipart=False,
redirect=True,
req=None,
):
"""
Load content at url and returns it.
:param url: URL to load
:param get: Query string parameters
:param post: POST parameters
:param referrer: Either a str with referrer, True to use default, False to disable
:param cookies: True or False or list of tuples [(domain, name, value)]
:param just_header: If True only the header will be retrieved and returned as dict
:param decode: The codec name to decode the output, True to use codec from http header, should be True in most cases
:param multipart: Weather to use multipart post
:param redirect: Either a number with maximum redirections, True to use default, False to disable
:param req: Either a request object, None to use default or False to use temporary request
:return: Loaded content
"""
if self.pyload.debug:
self.log_debug(
"LOAD URL " + url,
*[
"{}={}".format(key, value)
for key, value in locals().items()
if key not in ("self", "url", "_[1]")
],
)
url = fixurl(url, unquote=True) #: Recheck in 0.6.x
if req is False:
req = get_request()
elif not req:
req = self.req
html = req.load(
url,
get,
post,
referer=referrer,
cookies=cookies,
just_header=just_header,
multipart=multipart,
decode=decode,
redirect=redirect
)
if not just_header:
self.last_html = html
if self.pyload.debug:
self.dump_html()
# NOTE: req can be a HTTPRequest or a Browser object
http_req = self.req.http if hasattr(self.req, "http") else self.req
# TODO: Move to network in 0.6.x
header = http_req.response_headers.copy()
header.update({"code": req.code, "url": req.last_effective_url})
self.last_header = header
if just_header:
return header if decode else html
else:
return html
def upload(
self,
filename,
url,
get=None,
referrer=True,
cookies=True,
just_header=False,
decode=True,
redirect=True,
req=None,
):
"""
Uploads a file at url and returns response content.
:param filename: path to the file to upload
:param url: URL to upload to
:param get: Query string parameters
:param referrer: Either a str with referrer, True to use default, False to disable
:param cookies: True or False or list of tuples [(domain, name, value)]
:param just_header: If True only the header will be retrieved and returned as dict
:param decode: The codec name to decode the output, True to use codec from http header, should be True in most cases
:param redirect: Either a number with maximum redirections, True to use default, False to disable
:param req: Either a request object, None to use default or False to use temporary request
:return: Response content
"""
if self.pyload.debug:
self.log_debug(
"UPLOAD URL " + url,
*[
"{}={}".format(key, value)
for key, value in locals().items()
if key not in ("self", "url", "_[1]")
],
)
url = fixurl(url, unquote=True) #: Recheck in 0.6.x
if req is False:
req = get_request()
elif not req:
req = self.req
res = req.upload(
filename,
url,
get=get,
referer=referrer,
cookies=cookies,
just_header=just_header,
decode=decode,
redirect=redirect,
)
self.last_html = res
if self.pyload.debug:
self.dump_html()
# NOTE: req can be a HTTPRequest or a Browser object
http_req = req.http if hasattr(req, "http") else req
# TODO: Move to network in 0.6.x
header = http_req.response_headers.copy()
header.update({"code": req.code, "url": req.last_effective_url})
self.last_header = header
if just_header:
return header if decode else res
else:
return res
def dump_html(self):
frame = inspect.currentframe()
try:
framefile = os.path.join(
self.pyload.tempdir,
self.classname,
"{}_line{}.dump.html".format(
frame.f_back.f_code.co_name, frame.f_back.f_lineno
),
)
os.makedirs(os.path.dirname(framefile), exist_ok=True)
is_bytes = isinstance(self.last_html, (bytes, bytearray))
with open(framefile, mode="wb" if is_bytes else "w", encoding=None if is_bytes else "utf-8") as fp:
fp.write(self.last_html)
except IOError as exc:
self.log_error(exc)
finally:
del frame #: Delete the frame or it won't be cleaned
def clean(self):
"""
Remove references.
"""
try:
# self.req.clear_cookies()
self.req.close()
except AttributeError:
pass
else:
self.req = None