forked from pyload/pyload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExternalScripts.py
More file actions
117 lines (89 loc) · 4.49 KB
/
ExternalScripts.py
File metadata and controls
117 lines (89 loc) · 4.49 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
# -*- coding: utf-8 -*-
"""
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
@author: mkaay
@interface-version: 0.1
"""
import subprocess
from os import listdir, access, X_OK, makedirs
from os.path import join, exists, basename, abspath
from module.plugins.Hook import Hook
from module.utils import save_join
class ExternalScripts(Hook):
__name__ = "ExternalScripts"
__version__ = "0.23"
__description__ = """Run external scripts"""
__config__ = [("activated", "bool", "Activated", "True")]
__author_name__ = ("mkaay", "RaNaN", "spoob")
__author_mail__ = ("mkaay@mkaay.de", "ranan@pyload.org", "spoob@pyload.org")
event_list = ["unrarFinished", "allDownloadsFinished", "allDownloadsProcessed"]
def setup(self):
self.scripts = {}
folders = ['download_preparing', 'download_finished', 'package_finished',
'before_reconnect', 'after_reconnect', 'unrar_finished',
'all_dls_finished', 'all_dls_processed']
for folder in folders:
self.scripts[folder] = []
self.initPluginType(folder, join(pypath, 'scripts', folder))
self.initPluginType(folder, join('scripts', folder))
for script_type, names in self.scripts.iteritems():
if names:
self.logInfo((_("Installed scripts for %s: ") % script_type ) + ", ".join([basename(x) for x in names]))
def initPluginType(self, folder, path):
if not exists(path):
try:
makedirs(path)
except:
self.logDebug("Script folder %s not created" % folder)
return
for f in listdir(path):
if f.startswith("#") or f.startswith(".") or f.startswith("_") or f.endswith("~") or f.endswith(".swp"):
continue
if not access(join(path, f), X_OK):
self.logWarning(_("Script not executable:") + " %s/%s" % (folder, f))
self.scripts[folder].append(join(path, f))
def callScript(self, script, *args):
try:
cmd = [script] + [str(x) if not isinstance(x, basestring) else x for x in args]
self.logDebug("Executing %(script)s: %(cmd)s" % {"script": abspath(script), "cmd": " ".join(cmd)})
#output goes to pyload
subprocess.Popen(cmd, bufsize=-1)
except Exception, e:
self.logError(_("Error in %(script)s: %(error)s") % {"script": basename(script), "error": str(e)})
def downloadPreparing(self, pyfile):
for script in self.scripts['download_preparing']:
self.callScript(script, pyfile.pluginname, pyfile.url, pyfile.id)
def downloadFinished(self, pyfile):
for script in self.scripts['download_finished']:
self.callScript(script, pyfile.pluginname, pyfile.url, pyfile.name,
save_join(self.config['general']['download_folder'],
pyfile.package().folder, pyfile.name), pyfile.id)
def packageFinished(self, pypack):
for script in self.scripts['package_finished']:
folder = self.config['general']['download_folder']
folder = save_join(folder, pypack.folder)
self.callScript(script, pypack.name, folder, pypack.password, pypack.id)
def beforeReconnecting(self, ip):
for script in self.scripts['before_reconnect']:
self.callScript(script, ip)
def afterReconnecting(self, ip):
for script in self.scripts['after_reconnect']:
self.callScript(script, ip)
def unrarFinished(self, folder, fname):
for script in self.scripts["unrar_finished"]:
self.callScript(script, folder, fname)
def allDownloadsFinished(self):
for script in self.scripts["all_dls_finished"]:
self.callScript(script)
def allDownloadsProcessed(self):
for script in self.scripts["all_dls_processed"]:
self.callScript(script)