-
Notifications
You must be signed in to change notification settings - Fork 0
/
gclone_pp.py
82 lines (70 loc) · 2.89 KB
/
gclone_pp.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
#!/usr/bin/env python3
################################################################################
### NZBGET POST-PROCESSING SCRIPT ###
################################################################################
# Rclone move script by ssnjr.
#
# Uploads successful downloads to your gdrive.
################################################################################
### NZBGET POST-PROCESSING SCRIPT ###
################################################################################
import os
import sys
import subprocess
from xmlrpc.client import ServerProxy
# NZBGET rpc ctrl creds:
host = os.environ.get('NZBOP_CONTROLIP')
port = os.environ.get('NZBOP_CONTROLPORT')
username = os.environ.get('NZBOP_CONTROLUSERNAME')
password = os.environ.get('NZBOP_CONTROLPASSWORD')
# Downloaded NZB name:
down_nzb_name = os.environ.get('NZBPP_NZBNAME')
# Post processed NZB upload directory:
upload_dir = os.environ.get('UPLOAD_DIR')
# Gives downloaded file directory without the trailing '/':
nzb_down_dir = os.environ.get('NZBPP_DIRECTORY')
# Checks download status:
status = os.environ.get('NZBPP_TOTALSTATUS')
# Uses rclone listremote to list availabe remotes and chooses the first one:
remote_name = str(
subprocess.check_output('gclone listremotes', shell=True).decode('UTF-8')
).splitlines()[0]
if host == '0.0.0.0': host = '127.0.0.1'
# Build an URL for XML-RPC requests:
rpcUrl = f'http://{username}:{password}@{host}:{port}/xmlrpc'
# Create remote server object:
server = ServerProxy(rpcUrl)
# Function for running rclone cmds with live output to logs:
def gclone(command):
with subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True) as proc:
while True:
output = proc.stdout.readline().decode('UTF-8').strip()
if output != '':
server.writelog('INFO', output)
if output == '' and proc.poll() is not None:
server.writelog(
'INFO', f'"{down_nzb_name}" has been uploaded to ' + \
f'"{remote_name}" sucessfully!')
break
nzb_upload_dir = f"{remote_name}/{upload_dir}/{down_nzb_name}"
if status == 'SUCCESS':
server.writelog(
'INFO', f'Commencing upload of "{down_nzb_name}" to "{remote_name}"')
gclone(
f'gclone move "{nzb_down_dir}" "{nzb_upload_dir}" ' + \
'-v --stats=1s --stats-one-line')
sys.exit(93)
elif status == 'WARNING':
server.writelog(
'WARNING', f'Upload of "{down_nzb_name}" is cancelled. Running ' + \
'post-proc again to possibly fix damages.')
sys.exit(93)
elif status in ['FAILURE', 'DELETED']:
server.writelog(
'ERROR', f'Upload of "{down_nzb_name}" is cancelled ' + \
f'due to the file status being: {status}')
sys.exit(94)