-
Notifications
You must be signed in to change notification settings - Fork 6
/
join.py
executable file
·119 lines (101 loc) · 3.54 KB
/
join.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
#!/usr/bin/env python2
##############################################################################
### NZBGET POST-PROCESSING SCRIPT ###
# Join numbered split files after download
#
#
# This script joins *.001 etc files
#
#
# NOTE: Requires python
#
#
# Please note on the second pass it will skip output as this is by design because we just
# want the pars to check out the record again.
##############################################################################
### OPTIONS ###
# Preserve orginal files (yes, no).
#
#Preservefiles=no
#
# Overwrite output file (yes, no).
#
#
# In the case that both *.001 and the output file exists but is not the
# expected size should the output file be overwritten.
#
#Overwritefiles=yes
#
### NZBGET POST-PROCESSING SCRIPT ###
##############################################################################
import re
import os
import sys
# Exit codes used by NZBGet
POSTPROCESS_SUCCESS=93
POSTPROCESS_ERROR=94
POSTPROCESS_PAR2=92
POSTPROCESS_SKIPPED=95
if not os.environ.has_key('NZBOP_SCRIPTDIR'):
print('*** NZBGet post-processing script ***')
print('This script is supposed to be called from nzbget (11.0 or later).')
sys.exit(POSTPROCESS_ERROR)
sys.stdout.flush()
matcher = re.compile('\.[0-9]+$')
chunks = []
chunks2 = []
for dirpath, dirnames, filenames in os.walk(os.environ['NZBPP_DIRECTORY']):
for file in filenames:
chunks.append(os.path.join(dirpath,file))
chunks2 = [f for f in chunks if matcher.search(f)]
if not chunks2:
print('[DETAIL] No files found to join. Skipping join!')
sys.exit(POSTPROCESS_SKIPPED)
sets = {}
set = None
for file in chunks2:
start, finish = os.path.splitext(file)
if start not in sets:
sets[start] = []
sets[start].append(file)
for set in sets:
sets[set].sort()
current = sets[set]
outfile = set
if os.path.isfile(outfile):
if os.path.getsize(outfile) <= os.path.getsize(current[0]):
if outfile + '.001' not in current:
os.rename(outfile, outfile + '.001')
current.append(outfile + '.001')
current.sort()
else:
if os.environ['NZBPO_OVERWRITEFILES'] == 'yes':
os.unlink(outfile)
else:
print('[ERROR] %s was found and does not match expected output size. Not overwriting, OVERWRITEFILES is sent to %s' % (outfile, os.environ['NZBPO_OVERWRITEFILES']))
sys.exit(POSTPROCESS_ERROR)
else:
print('[DETAIL] %s was found at %s bytes. Output matches at %s bytes. Skipping join!' % (outfile, os.path.getsize(outfile),size))
sys.exit(POSTPROCESS_SKIPPED)
wdata = ''
with open(outfile, "ab") as newfile:
i = 0
for f in current:
i = i + 1
print('[INFO] joining file %s' % f)
with open(f, "rb") as part:
reading = True
while reading:
wdata = part.read(4096)
if (len(wdata) > 0):
newfile.write(wdata)
else:
reading = False
part.close()
if os.environ['NZBPO_PRESERVEFILES'] == 'yes':
print ('[INFO] File %s was not deleted' % f)
else:
print('[INFO] deleted file %s' % f)
os.unlink(f)
newfile.close()
sys.exit(POSTPROCESS_SUCCESS)