Repository navigation
Expand file tree
/
Copy pathMultiHoster.py
More file actions
190 lines (144 loc) · 6.64 KB
/
Copy pathMultiHoster.py
File metadata and controls
190 lines (144 loc) · 6.64 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
from module.utils import remove_chars
from module.plugins.Hook import Hook
class MultiHoster(Hook):
"""
Generic MultiHoster plugin
"""
__version__ = "0.19"
replacements = [("2shared.com", "twoshared.com"), ("4shared.com", "fourshared.com"), ("cloudnator.com", "shragle.com"),
("ifile.it", "filecloud.io"), ("easy-share.com", "crocko.com"), ("freakshare.net", "freakshare.com"),
("hellshare.com", "hellshare.cz"), ("share-rapid.cz", "sharerapid.com"), ("sharerapid.cz", "sharerapid.com"),
("ul.to", "uploaded.to"), ("uploaded.net", "uploaded.to"), ("1fichier.com", "onefichier.com")]
ignored = []
interval = 24 * 60 * 60 #: reload hosters daily
def setup(self):
self.hosters = []
self.supported = []
self.new_supported = []
def getConfig(self, option, default=''):
"""getConfig with default value - sublass may not implements all config options"""
try:
return self.getConf(option)
except KeyError:
return default
def getHosterCached(self):
if not self.hosters:
try:
hosterSet = self.toHosterSet(self.getHoster()) - set(self.ignored)
except Exception, e:
self.logError("%s" % str(e))
return []
try:
configMode = self.getConfig('hosterListMode', 'all')
if configMode in ("listed", "unlisted"):
configSet = self.toHosterSet(self.getConfig('hosterList', '').replace('|', ',').replace(';', ',').split(','))
if configMode == "listed":
hosterSet &= configSet
else:
hosterSet -= configSet
except Exception, e:
self.logError("%s" % str(e))
self.hosters = list(hosterSet)
return self.hosters
def toHosterSet(self, hosters):
hosters = set((str(x).strip().lower() for x in hosters))
for rep in self.replacements:
if rep[0] in hosters:
hosters.remove(rep[0])
hosters.add(rep[1])
hosters.discard('')
return hosters
def getHoster(self):
"""Load list of supported hoster
:return: List of domain names
"""
raise NotImplementedError
def coreReady(self):
if self.cb:
self.core.scheduler.removeJob(self.cb)
self.setConfig("activated", True) #: config not in sync after plugin reload
cfg_interval = self.getConfig("interval", None) #: reload interval in hours
if cfg_interval is not None:
self.interval = cfg_interval * 60 * 60
if self.interval:
self._periodical()
else:
self.periodical()
def initPeriodical(self):
pass
def periodical(self):
"""reload hoster list periodically"""
self.logInfo("Reloading supported hoster list")
old_supported = self.supported
self.supported, self.new_supported, self.hosters = [], [], []
self.overridePlugins()
old_supported = [hoster for hoster in old_supported if hoster not in self.supported]
if old_supported:
self.logDebug("UNLOAD: %s" % ", ".join(old_supported))
for hoster in old_supported:
self.unloadHoster(hoster)
def overridePlugins(self):
pluginMap = {}
for name in self.core.pluginManager.hosterPlugins.keys():
pluginMap[name.lower()] = name
accountList = [name.lower() for name, data in self.core.accountManager.accounts.items() if data]
excludedList = []
for hoster in self.getHosterCached():
name = remove_chars(hoster.lower(), "-.")
if name in accountList:
excludedList.append(hoster)
else:
if name in pluginMap:
self.supported.append(pluginMap[name])
else:
self.new_supported.append(hoster)
if not self.supported and not self.new_supported:
self.logError(_("No Hoster loaded"))
return
module = self.core.pluginManager.getPlugin(self.__name__)
klass = getattr(module, self.__name__)
# inject plugin plugin
self.logDebug("Overwritten Hosters: %s" % ", ".join(sorted(self.supported)))
for hoster in self.supported:
dict = self.core.pluginManager.hosterPlugins[hoster]
dict["new_module"] = module
dict["new_name"] = self.__name__
if excludedList:
self.logInfo("The following hosters were not overwritten - account exists: %s" % ", ".join(sorted(excludedList)))
if self.new_supported:
self.logDebug("New Hosters: %s" % ", ".join(sorted(self.new_supported)))
# create new regexp
regexp = r".*(%s).*" % "|".join([x.replace(".", "\\.") for x in self.new_supported])
if hasattr(klass, "__pattern__") and isinstance(klass.__pattern__, basestring) and '://' in klass.__pattern__:
regexp = r"%s|%s" % (klass.__pattern__, regexp)
self.logDebug("Regexp: %s" % regexp)
dict = self.core.pluginManager.hosterPlugins[self.__name__]
dict["pattern"] = regexp
dict["re"] = re.compile(regexp)
def unloadHoster(self, hoster):
dict = self.core.pluginManager.hosterPlugins[hoster]
if "module" in dict:
del dict["module"]
if "new_module" in dict:
del dict["new_module"]
del dict["new_name"]
def unload(self):
"""Remove override for all hosters. Scheduler job is removed by hookmanager"""
for hoster in self.supported:
self.unloadHoster(hoster)
# reset pattern
klass = getattr(self.core.pluginManager.getPlugin(self.__name__), self.__name__)
dict = self.core.pluginManager.hosterPlugins[self.__name__]
dict["pattern"] = getattr(klass, '__pattern__', r"^unmatchable$")
dict["re"] = re.compile(dict["pattern"])
def downloadFailed(self, pyfile):
"""remove plugin override if download fails but not if file is offline/temp.offline"""
if pyfile.hasStatus("failed") and self.getConfig("unloadFailing", True):
hdict = self.core.pluginManager.hosterPlugins[pyfile.pluginname]
if "new_name" in hdict and hdict['new_name'] == self.__name__:
self.logDebug("Unload MultiHoster", pyfile.pluginname, hdict)
self.unloadHoster(pyfile.pluginname)
pyfile.setStatus("queued")