-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwget.py
More file actions
80 lines (65 loc) · 2.32 KB
/
wget.py
File metadata and controls
80 lines (65 loc) · 2.32 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
import json, subprocess, threading, re, os
DISABLE_LOGS = False # Set to true if you don't want logs to be sent when wget fails
HAVE_WGET = subprocess.run(["wget", "-V"]).returncode == 0
_processes = {}
def _update_progress(proc_id : int):
proc = _processes[proc_id]["process"]
while proc.poll() == None:
line = proc.stderr.readline().decode(errors='ignore').strip() # Don't ask me why wget outputs status info to stderr
if not DISABLE_LOGS:
_processes[proc_id]["log"] += [line]
prog_match = re.search(r" ([0-9]+)% ", line)
if prog_match:
_processes[proc_id]["progress"] = int(prog_match.group(1))
_processes[proc_id]["status"] = "error" if proc.returncode else "success"
print("-- Finished download ID", proc_id, "with return code", proc.returncode)
def main(args : dict):
if not HAVE_WGET:
args["log"]("WARNING: wget is not installed! Downloader won't be available", 3)
try:
cmd = json.loads(args["txt"])
except:
return
action = cmd.get("action")
# Download action
if action == "wget.download" and "url" in cmd:
if "w" not in args["perms"]:
return
if not HAVE_WGET:
return {"stdout": "-1"}
# Replace all spaces so it counts as one argument in the command
proc_cmd = ["wget", cmd.get("url").replace(" ", "%20"), "-P", args["ap"]]
filename = cmd.get("filename")
if filename:
proc_cmd += ["-O", os.path.join(args["ap"], filename)]
# Create new process entry
proc_id = len(_processes)
_processes[proc_id] = {
"process": subprocess.Popen(proc_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE),
"progress": 0,
"status": "running",
"log": [],
"user": args["user"]
}
threading.Thread(target=_update_progress, args=(proc_id, )).start()
print("-- Started download with ID", proc_id, "to", args["ap"])
# Return process ID
return {"stdout": str(proc_id)}
# Status action
if action == "wget.status" and "id" in cmd:
proc_id = cmd.get("id")
proc = _processes.get(proc_id)
if not proc or proc["user"] != args["user"]:
return {"stdout": '{"error": "can\'t access download"}'}
# Clear process data after being queried once
if proc["status"] != "running":
_processes.pop(proc_id)
resp = {
"progress": proc["progress"],
"status": proc["status"]
}
if proc["status"] == "error":
resp["log"] = proc["log"]
return {
"stdout": json.dumps(resp)
}