Repository navigation
Expand file tree
/
Copy pathDecrypterThread.py
More file actions
123 lines (96 loc) · 3.99 KB
/
Copy pathDecrypterThread.py
File metadata and controls
123 lines (96 loc) · 3.99 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from time import sleep
from pyload.Api import LinkStatus, DownloadStatus as DS, ProgressInfo, ProgressType
from pyload.utils import uniqify, accumulate
from pyload.plugins.Base import Abort, Retry
from pyload.plugins.Crypter import Package
from BaseThread import BaseThread
class DecrypterThread(BaseThread):
"""thread for decrypting"""
def __init__(self, manager, data, pid):
# TODO: owner
BaseThread.__init__(self, manager)
# [... (plugin, url) ...]
self.data = data
self.pid = pid
# holds the progress, while running
self.progress = None
self.m.addThread(self)
self.start()
def getProgress(self):
return self.progress
def run(self):
pack = self.m.core.files.getPackage(self.pid)
links, packages = self.decrypt(accumulate(self.data), pack.password)
if links:
self.log.info(
_("Decrypted %(count)d links into package %(name)s") % {"count": len(links), "name": pack.name})
self.m.core.api.addLinks(self.pid, [l.url for l in links])
# TODO: add single package into this one and rename it?
# TODO: nested packages
for p in packages:
self.m.core.api.addPackage(p.name, p.getURLs(), pack.password)
self.finished()
def decrypt(self, plugin_map, password=None, err=False):
result = []
self.progress = ProgressInfo("BasePlugin", "", _("decrypting"),
0, 0, len(self.data), self.owner, ProgressType.Decrypting)
# TODO QUEUE_DECRYPT
for name, urls in plugin_map.iteritems():
klass = self.m.core.pluginManager.loadClass("crypter", name)
plugin = None
plugin_result = []
# updating progress
self.progress.plugin = name
self.progress.name = _("Decrypting %s links") % len(urls) if len(urls) > 1 else urls[0]
#TODO: dependency check, there is a new error code for this
# TODO: decrypting with result yielding
if not klass:
if err:
plugin_result.extend(LinkStatus(url, url, -1, DS.NotPossible, name) for url in urls)
self.log.debug("Plugin for decrypting was not loaded")
else:
try:
plugin = klass(self.m.core, password)
try:
plugin_result = plugin._decrypt(urls)
except Retry:
sleep(1)
plugin_result = plugin._decrypt(urls)
plugin.logDebug("Decrypted", plugin_result)
except Abort:
plugin.logInfo(_("Decrypting aborted"))
except Exception, e:
plugin.logError(_("Decrypting failed"), e)
# generate error linkStatus
if err:
plugin_result.extend(LinkStatus(url, url, -1, DS.Failed, name) for url in urls)
if self.core.debug:
self.core.print_exc()
self.writeDebugReport(plugin.__name__, plugin=plugin)
finally:
if plugin:
plugin.clean()
self.progress.done += len(urls)
result.extend(plugin_result)
# clear the progress
self.progress = None
# generated packages
packs = {}
# urls without package
urls = []
# merge urls and packages
for p in result:
if isinstance(p, Package):
if p.name in packs:
packs[p.name].urls.extend(p.urls)
else:
if not p.name:
urls.extend(p.links)
else:
packs[p.name] = p
else:
urls.append(p)
urls = uniqify(urls)
return urls, packs.values()