forked from pyload/pyload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyLoadCore.py
executable file
·941 lines (742 loc) · 30.9 KB
/
pyLoadCore.py
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
#!/usr/bin/env python
# -*- 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: spoob
@author: sebnapi
@author: RaNaN
@author: mkaay
@version: v0.4.6
"""
CURRENT_VERSION = '0.4.6'
import __builtin__
from getopt import getopt, GetoptError
import gettext
from imp import find_module
import logging
import logging.handlers
import os
from os import _exit, execv, getcwd, makedirs, remove, sep
from os.path import exists, join
import signal
import subprocess
import sys
from sys import argv, executable, exit
from time import time, sleep
from traceback import print_exc
from module import InitHomeDir
from module.plugins.AccountManager import AccountManager
from module.CaptchaManager import CaptchaManager
from module.ConfigParser import ConfigParser
from module.HookManager import HookManager
from module.plugins.PluginManager import PluginManager
from module.PullEvents import PullManager
from module.network.RequestFactory import RequestFactory
from module.ThreadManager import ThreadManager
from module.web.ServerThread import WebServer
from module.PyFile import PyFile
from module.Scheduler import Scheduler
from module.common.JsEngine import JsEngine
from module.remote.RemoteManager import RemoteManager
from module.database import DatabaseBackend
from module.database import FileHandler
from module.utils import freeSpace, formatSize, compare_time
from codecs import getwriter
if os.name == "nt":
enc = "cp850"
else:
enc = "utf8"
sys.stdout = getwriter(enc)(sys.stdout, errors = "replace")
class Core(object):
"""pyLoad Core, one tool to rule them all... (the filehosters) :D"""
def __init__(self):
self.doDebug = False
self.startedInGui = False
self.running = False
self.daemon = False
self.arg_links = []
self.pidfile = "pyload.pid"
if len(argv) > 1:
try:
options, args = getopt(argv[1:], 'vchdusq',
["version", "clear", "help", "debug", "user", "setup", "configdir=", "changedir", "daemon", "quit", "status"])
for option, argument in options:
if option in ("-v", "--version"):
print "pyLoad", CURRENT_VERSION
exit()
elif option == "--daemon":
self.daemon = True
elif option in ("-c", "--clear"):
try:
remove("files.db")
print "Removed Linklist"
except:
print "No Linklist found"
elif option in ("-h", "--help"):
self.print_help()
exit()
elif option in ("-d", "--debug"):
self.doDebug = True
elif option in ("-u", "--user"):
from module.setup import Setup
self.config = ConfigParser()
s = Setup(pypath, self.config)
s.set_user()
exit()
elif option in ("-s", "--setup"):
from module.setup import Setup
self.config = ConfigParser()
s = Setup(pypath, self.config)
s.start()
exit()
elif option == "--changedir":
from module.setup import Setup
self.config = ConfigParser()
s = Setup(pypath, self.config)
s.conf_path(True)
exit()
elif option in ("-q", "--quit"):
self.quitInstance()
exit()
elif option == "--status":
print self.isAlreadyRunning()
exit()
except GetoptError:
print 'Unknown Argument(s) "%s"' % " ".join(argv[1:])
self.print_help()
exit()
def print_help(self):
print ""
print "pyLoad v%s 2008-2011 the pyLoad Team" % CURRENT_VERSION
print ""
print "Usage: [python] pyLoadCore.py [options]"
print ""
print "<Options>"
print " -v, --version", " " * 10, "Print version to terminal"
print " -c, --clear", " " * 12, "Delete the saved linklist"
#print " -a, --add=<link/list>", " " * 2, "Add the specified links"
print " -u, --user", " " * 13, "Manages users"
print " -d, --debug", " " * 12, "Enable debug mode"
print " -s, --setup", " " * 12, "Run Setup Assistent"
print " --configdir=<dir>", " " * 6, "Run with <dir> as config directory"
print " --changedir", " "* 12, "Change config dir permanently"
print " --daemon", " " * 15, "Daemonize after start"
print " --status", " " * 15, "Display pid if running or False"
print " -q, --quit", " " * 13, "Try to quit running pyLoad"
print " -h, --help", " " * 13, "Display this help screen"
print ""
def toggle_pause(self):
if self.threadManager.pause:
self.threadManager.pause = False
return False
elif not self.threadManager.pause:
self.threadManager.pause = True
return True
def quit(self, a, b):
self.shutdown()
self.log.info(_("Received Quit signal"))
_exit(1)
def writePidFile(self):
self.deletePidFile()
pid = os.getpid()
f = open(self.pidfile, "wb")
f.write(str(pid))
f.close()
def deletePidFile(self):
if self.checkPidFile():
self.log.debug("Deleting old pidfile %s" % self.pidfile)
os.remove(self.pidfile)
def checkPidFile(self):
""" return pid as int or 0"""
if os.path.isfile(self.pidfile):
f = open(self.pidfile, "rb")
pid = f.read().strip()
f.close()
if pid:
pid = int(pid)
return pid
return 0
def isAlreadyRunning(self):
pid = self.checkPidFile()
if not pid or os.name == "nt": return False
try:
os.kill(pid, 0) # 0 - default signal (does nothing)
except:
return 0
return pid
def quitInstance(self):
if os.name == "nt":
print "Not supported on windows."
return
pid = self.isAlreadyRunning()
if not pid:
print "No pyLoad running."
try:
os.kill(pid, 3) #SIGUIT
t = time()
print "waiting for pyLoad to quit"
while exists(self.pidfile) and t + 10 > time():
sleep(0.25)
if not exists(self.pidfile):
print "pyLoad successfully stopped"
else:
os.kill(pid, 9) #SIGKILL
print "pyLoad did not respond"
print "Kill signal was send to process with id %s" % pid
except:
print "Error quitting pyLoad"
def start(self, rpc=True, web=True):
""" starts the fun :D """
if not exists("pyload.conf"):
from module.setup import Setup
print "This is your first start, running configuration assistent now."
self.config = ConfigParser()
s = Setup(pypath, self.config)
try:
res = s.start()
except:
res = False
print_exc()
print "Setup failed"
if not res:
remove("pyload.conf")
exit()
try: signal.signal(signal.SIGQUIT, self.quit)
except: pass
self.config = ConfigParser()
translation = gettext.translation("pyLoad", self.path("locale"),
languages=["en", self.config['general']['language']])
translation.install(True)
self.debug = self.doDebug or self.config['general']['debug_mode']
pid = self.isAlreadyRunning()
if pid:
print _("pyLoad already running with pid %s") % pid
exit()
if os.name != "nt" and self.config["general"]["renice"]:
os.system("renice %d %d" % (self.config["general"]["renice"], os.getpid()) )
if self.config["permission"]["change_group"]:
if os.name != "nt":
try:
from grp import getgrnam
group = getgrnam(self.config["permission"]["group"])
os.setgid(group[2])
except Exception, e:
print _("Failed changing group: %s") % e
if self.config["permission"]["change_user"]:
if os.name != "nt":
try:
from pwd import getpwnam
user = getpwnam(self.config["permission"]["user"])
os.setuid(user[2])
except Exception, e:
print _("Failed changing user: %s") % e
self.check_file(self.config['log']['log_folder'], _("folder for logs"), True)
if self.debug:
self.init_logger(logging.DEBUG) # logging level
else:
self.init_logger(logging.INFO) # logging level
self.do_kill = False
self.do_restart = False
self.shuttedDown = False
self.log.info(_("Starting") + " pyLoad %s" % CURRENT_VERSION)
self.log.info(_("Using home directory: %s") % getcwd())
self.writePidFile()
#@TODO refractor
self.check_install("Crypto", _("pycrypto to decode container files"))
#img = self.check_install("Image", _("Python Image Libary (PIL) for captcha reading"))
#self.check_install("pycurl", _("pycurl to download any files"), True, True)
self.check_file("tmp", _("folder for temporary files"), True)
#tesser = self.check_install("tesseract", _("tesseract for captcha reading"), False) if os.name != "nt" else True
self.captcha = True # checks seems to fail, althoug tesseract is available
self.check_file(self.config['general']['download_folder'], _("folder for downloads"), True)
self.check_file("links.txt", _("file for links"))
if self.config['ssl']['activated']:
self.check_install("OpenSSL", _("OpenSSL for secure connection"), True)
self.setupDB()
if self.config.oldRemoteData:
self.log.info(_("Moving old user config to DB"))
self.db.addUser(self.config.oldRemoteData["username"], self.config.oldRemoteData["password"])
self.log.info(_("Please check your logindata with ./pyLoadCore.py -u"))
self.requestFactory = RequestFactory(self)
__builtin__.pyreq = self.requestFactory
self.lastClientConnected = 0
self.server_methods = ServerMethods(self)
self.scheduler = Scheduler(self)
#hell yeah, so many important managers :D
self.pluginManager = PluginManager(self)
self.pullManager = PullManager(self)
self.accountManager = AccountManager(self)
self.threadManager = ThreadManager(self)
self.captchaManager = CaptchaManager(self)
self.hookManager = HookManager(self)
self.remoteManager = RemoteManager(self)
self.js = JsEngine()
self.log.info(_("Downloadtime: %s") % self.server_methods.is_time_download())
if rpc:
self.remoteManager.startBackends()
if web:
self.init_webserver()
#linkFile = self.config['general']['link_file']
spaceLeft = freeSpace(self.config["general"]["download_folder"])
self.log.info(_("Free space: %s") % formatSize(spaceLeft))
self.config.save() #save so config files gets filled
link_file = join(pypath, "links.txt")
if exists(link_file):
f = open(link_file, "rb")
if f.read().strip():
self.server_methods.add_package("links.txt", [link_file], 1)
f.close()
link_file = "links.txt"
if exists(link_file):
f = open(link_file, "rb")
if f.read().strip():
self.server_methods.add_package("links.txt", [link_file], 1)
f.close()
#self.scheduler.addJob(0, self.accountManager.getAccountInfos)
self.log.info(_("Activating Accounts..."))
self.accountManager.getAccountInfos()
self.threadManager.pause = False
self.running = True
self.log.info(_("Activating Plugins..."))
self.hookManager.coreReady()
self.log.info(_("pyLoad is up and running"))
#test api
# from module.common.APIExerciser import APIExerciser
# APIExerciser(self)
# APIExerciser(self)
# APIExerciser(self)
#some memory stats
# from guppy import hpy
# hp=hpy()
# import objgraph
# objgraph.show_most_common_types(limit=20)
# import memdebug
# memdebug.start(8002)
locals().clear()
while True:
sleep(2)
if self.do_restart:
self.log.info(_("restarting pyLoad"))
self.restart()
if self.do_kill:
self.shutdown()
self.log.info(_("pyLoad quits"))
self.removeLogger()
_exit(0) #@TODO thrift blocks shutdown
self.threadManager.work()
self.scheduler.work()
def setupDB(self):
self.db = DatabaseBackend(self) # the backend
self.db.setup()
self.files = FileHandler(self)
self.db.manager = self.files #ugly?
def init_webserver(self):
if self.config['webinterface']['activated']:
self.webserver = WebServer(self)
self.webserver.start()
def init_logger(self, level):
console = logging.StreamHandler(sys.stdout)
frm = logging.Formatter("%(asctime)s %(levelname)-8s %(message)s", "%d.%m.%Y %H:%M:%S")
console.setFormatter(frm)
self.log = logging.getLogger("log") # settable in config
if self.config['log']['file_log']:
file_handler = logging.handlers.RotatingFileHandler(join(self.config['log']['log_folder'], 'log.txt'),
maxBytes=102400,
backupCount=int(self.config['log']['log_count']),
encoding="utf8"
) #100 kib each
file_handler.setFormatter(frm)
self.log.addHandler(file_handler)
self.log.addHandler(console) #if console logging
self.log.setLevel(level)
def removeLogger(self):
for h in list(self.log.handlers):
self.log.removeHandler(h)
h.close()
def check_install(self, check_name, legend, python=True, essential=False):
"""check wether needed tools are installed"""
try:
if python:
find_module(check_name)
else:
pipe = subprocess.PIPE
subprocess.Popen(check_name, stdout=pipe, stderr=pipe)
return True
except:
if essential:
self.log.info(_("Install %s") % legend)
exit()
return False
def check_file(self, check_names, description="", folder=False, empty=True, essential=False, quiet=False):
"""check wether needed files exists"""
tmp_names = []
if not type(check_names) == list:
tmp_names.append(check_names)
else:
tmp_names.extend(check_names)
file_created = True
file_exists = True
for tmp_name in tmp_names:
if not exists(tmp_name):
file_exists = False
if empty:
try:
if folder:
tmp_name = tmp_name.replace("/", sep)
makedirs(tmp_name)
else:
open(tmp_name, "w")
except:
file_created = False
else:
file_created = False
if not file_exists and not quiet:
if file_created:
#self.log.info( _("%s created") % description )
pass
else:
if not empty:
self.log.warning(_("could not find %(desc)s: %(name)s") % {"desc": description, "name": tmp_name})
else:
print _("could not create %(desc)s: %(name)s") % {"desc": description, "name": tmp_name}
if essential:
exit()
def isClientConnected(self):
return (self.lastClientConnected + 30) > time()
def restart(self):
self.shutdown()
execv(executable, [executable, "pyLoadCore.py"])
def shutdown(self):
self.log.info(_("shutting down..."))
try:
if self.config['webinterface']['activated'] and hasattr(self, "webserver"):
self.webserver.quit()
#self.webserver.join()
for thread in self.threadManager.threads:
thread.put("quit")
pyfiles = self.files.cache.values()
for pyfile in pyfiles:
pyfile.abortDownload()
except:
if self.debug:
print_exc()
self.log.info(_("error while shutting down"))
finally:
self.files.syncSave()
self.shuttedDown = True
self.deletePidFile()
def path(self, * args):
return join(pypath, * args)
####################################
########## XMLRPC Methods ##########
####################################
class ServerMethods():
""" methods that can be used by clients with xmlrpc connection"""
def __init__(self, core):
self.core = core
def _dispatch(self, method, params):
f = getattr(self, method)
try:
return f(*params)
except:
print_exc()
raise
def status_downloads(self):
""" gives status about all files currently processed """
downloads = []
for pyfile in [x.active for x in self.core.threadManager.threads + self.core.threadManager.localThreads if
x.active and x.active != "quit"]:
if not isinstance(pyfile, PyFile) or not hasattr(pyfile, "plugin"):
continue
download = {'id': pyfile.id,
'name': pyfile.name,
'speed': pyfile.getSpeed() / 1024,
'eta': pyfile.getETA(),
'format_eta': pyfile.formatETA(),
'kbleft': pyfile.getBytesLeft(),
'bleft': pyfile.getBytesLeft(),
'size': pyfile.getSize(),
'format_size': pyfile.formatSize(),
'percent': pyfile.getPercent(),
'status': pyfile.status,
'statusmsg': pyfile.m.statusMsg[pyfile.status],
'format_wait': pyfile.formatWait(),
'wait_until': pyfile.waitUntil,
'package': pyfile.package().name}
downloads.append(download)
return downloads
def get_conf_val(self, cat, var, sec="core"):
""" get config value """
if sec == "core":
return self.core.config[cat][var]
elif sec == "plugin":
return self.core.config.getPlugin(cat, var)
def set_conf_val(self, cat, opt, val, sec="core"):
""" set config value """
if sec == "core":
opt = str(opt)
self.core.config[str(cat)][opt] = val
if opt in ("limit_speed", "max_speed"): #not so nice to update the limit
self.core.requestFactory.updateBucket()
elif sec == "plugin":
self.core.config.setPlugin(cat, opt, val)
def get_config(self):
""" gets complete config """
return self.core.config.config
def get_plugin_config(self):
""" gets complete plugin config """
return self.core.config.plugin
def pause_server(self):
self.core.threadManager.pause = True
def unpause_server(self):
self.core.threadManager.pause = False
def toggle_pause(self):
self.core.threadManager.pause ^= True
return self.core.threadManager.pause
def toggle_reconnect(self):
self.core.config["reconnect"]["activated"] ^= True
return self.core.config["reconnect"]["activated"]
def status_server(self):
""" dict with current server status """
status = {'pause': self.core.threadManager.pause,
'activ': len(self.core.threadManager.processingIds()),
'queue': self.core.files.getFileCount(),
'total': self.core.files.getFileCount(),
'speed': 0}
for pyfile in [x.active for x in self.core.threadManager.threads if x.active and x.active != "quit"]:
status['speed'] += pyfile.getSpeed()/1024
status['download'] = not self.core.threadManager.pause and self.is_time_download()
status['reconnect'] = self.core.config['reconnect']['activated'] and self.is_time_reconnect()
return status
def free_space(self):
return freeSpace(self.core.config["general"]["download_folder"]) / 1024 / 1024 #mb
def get_server_version(self):
return CURRENT_VERSION
def add_package(self, name, links, queue=0):
#0 is collector
if self.core.config['general']['folder_per_package']:
folder = name
else:
folder = ""
folder = folder.replace("http://","").replace(":","").replace("/","_").replace("\\","_")
pid = self.core.files.addPackage(name, folder, queue)
self.core.files.addLinks(links, pid)
self.core.log.info(_("Added package %(name)s containing %(count)d links") % {"name": name, "count": len(links)})
self.core.files.save()
return pid
def get_package_data(self, id):
return self.core.files.getPackageData(int(id))
def get_file_data(self, id):
info = self.core.files.getFileData(int(id))
if not info:
return None
info = {str(info.keys()[0]): info[info.keys()[0]]}
return info
def del_links(self, ids):
for id in ids:
self.core.files.deleteLink(int(id))
self.core.files.save()
def del_packages(self, ids):
for id in ids:
self.core.files.deletePackage(int(id))
self.core.files.save()
def kill(self):
self.core.do_kill = True
return True
def restart(self):
self.core.do_restart = True
def get_queue(self):
return self.core.files.getCompleteData(1)
def get_collector(self):
return self.core.files.getCompleteData(0)
def get_queue_info(self):
return self.core.files.getInfoData(1)
def get_collector_info(self):
return self.core.files.getInfoData(0)
def add_files(self, pid, links):
pid = int(pid)
self.core.files.addLinks(links, pid)
self.core.log.info(_("Added %(count)d links to package #%(package)d ") % {"count": len(links), "package": pid})
self.core.files.save()
return pid
def push_package_to_queue(self, id):
self.core.files.setPackageLocation(id, 1)
def restart_package(self, packid):
self.core.files.restartPackage(int(packid))
def recheck_package(self, packid):
self.core.files.reCheckPackage(int(packid))
def restart_file(self, fileid):
self.core.files.restartFile(int(fileid))
def upload_container(self, filename, content):
th = open(join(self.core.config["general"]["download_folder"], "tmp_" + filename), "wb")
th.write(str(content))
th.close()
self.add_package(th.name, [th.name], 1)
def get_log(self, offset=0):
filename = join(self.core.config['log']['log_folder'], 'log.txt')
try:
fh = open(filename, "r")
lines = fh.readlines()
fh.close()
if offset >= len(lines):
return None
return lines[offset:]
except:
return ('No log available', )
def stop_downloads(self):
pyfiles = self.core.files.cache.values()
for pyfile in pyfiles:
pyfile.abortDownload()
def abort_files(self, fids):
pyfiles = self.core.files.cache.values()
for pyfile in pyfiles:
if pyfile.id in fids:
pyfile.abortDownload()
def stop_download(self, type, id):
if self.core.files.cache.has_key(id):
self.core.files.cache[id].abortDownload()
def set_package_name(self, pid, name):
pack = self.core.files.getPackage(pid)
pack.name = name
pack.sync()
def pull_out_package(self, pid):
"""put package back to collector"""
self.core.files.setPackageLocation(pid, 0)
def move_package(self, dest, pid):
if dest not in (0,1): return
self.core.files.setPackageLocation(pid, dest)
def is_captcha_waiting(self):
self.core.lastClientConnected = time()
task = self.core.captchaManager.getTask()
return not task is None
def get_captcha_task(self, exclusive=False):
""" returns tid, data, type, resulttype """
self.core.lastClientConnected = time()
task = self.core.captchaManager.getTask()
if task:
task.setWatingForUser(exclusive=exclusive)
captcha_info = task.getCaptcha()
return (task.id,) + captcha_info
else:
return None, None, None, None
def get_task_status(self, tid):
self.core.lastClientConnected = time()
t = self.core.captchaManager.getTaskByID(tid)
return t.getStatus() if t else ""
def set_captcha_result(self, tid, result):
self.core.lastClientConnected = time()
task = self.core.captchaManager.getTaskByID(tid)
if task:
task.setResult(result)
self.core.captchaManager.removeTask(task)
return True
else:
return False
def get_events(self, uuid):
return self.core.pullManager.getEvents(uuid)
def get_accounts(self, force=False, refresh=True):
return self.core.accountManager.getAccountInfos(force, refresh)
def update_account(self, plugin, account, password=None, options={}):
""" create and update account """
self.core.accountManager.updateAccount(plugin, account, password, options)
def remove_account(self, plugin, account):
self.core.accountManager.removeAccount(plugin, account)
def set_priority(self, id, priority):
p = self.core.files.getPackage(id)
p.setPriority(priority)
def order_package(self, id, pos):
self.core.files.reorderPackage(id, pos)
def order_file(self, id, pos):
self.core.files.reorderFile(id, pos)
def set_package_data(self, id, data):
p = self.core.files.getPackage(id)
if not p: return
for key, value in data.iteritems():
if key == "id": continue
setattr(p, key, value)
p.sync()
self.core.files.save()
def checkURLs(self, urls):
support = self.core.pluginManager.parseUrls(urls)
return [(u, p) if not p == "BasePlugin" else (u, None) for u, p in support]
def is_time_download(self):
start = self.core.config['downloadTime']['start'].split(":")
end = self.core.config['downloadTime']['end'].split(":")
return compare_time(start, end)
def is_time_reconnect(self):
start = self.core.config['reconnect']['startTime'].split(":")
end = self.core.config['reconnect']['endTime'].split(":")
return compare_time(start, end) and self.core.config["reconnect"]["activated"]
def delete_finished(self):
""" delete all finished links + packages, returns deleted packages """
deleted = self.core.files.deleteFinishedLinks()
return deleted
def restart_failed(self):
""" restart all failed links """
self.core.files.restartFailed()
def checkAuth(self, username, password):
return self.core.db.checkAuth(username, password)
def get_user_data(self):
return self.core.db.getAllUserData()
def set_user_permission(self, user, permission, role):
self.core.db.setPermission(user, permission)
self.core.db.setRole(user, role)
def change_password(self, user, oldpw, newpw):
return self.core.db.changePassword(user, oldpw, newpw)
def deamon():
try:
pid = os.fork()
if pid > 0:
sys.exit(0)
except OSError, e:
print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
sys.exit(1)
# decouple from parent environment
os.setsid()
os.umask(0)
# do second fork
try:
pid = os.fork()
if pid > 0:
# exit from second parent, print eventual PID before
print "Daemon PID %d" % pid
sys.exit(0)
except OSError, e:
print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
sys.exit(1)
# Iterate through and close some file descriptors.
for fd in range(0, 3):
try:
os.close(fd)
except OSError: # ERROR, fd wasn't open to begin with (ignored)
pass
os.open(os.devnull, os.O_RDWR) # standard input (0)
os.dup2(0, 1) # standard output (1)
os.dup2(0, 2)
pyload_core = Core()
pyload_core.start()
# And so it begins...
if __name__ == "__main__":
#change name to 'pyLoadCore'
#from module.lib.rename_process import renameProcess
#renameProcess('pyLoadCore')
if "--daemon" in sys.argv:
deamon()
else:
pyload_core = Core()
try:
pyload_core.start()
except KeyboardInterrupt:
pyload_core.shutdown()
pyload_core.log.info(_("killed pyLoad from Terminal"))
pyload_core.removeLogger()
_exit(1)