-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathred_ops_better
More file actions
421 lines (376 loc) · 20.5 KB
/
red_ops_better
File metadata and controls
421 lines (376 loc) · 20.5 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
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
#!/usr/bin/env python3
import configparser
import argparse
import pickle
import os
import shutil
import sys
import tempfile
import traceback
from urllib.parse import urlparse, parse_qsl
from multiprocessing import cpu_count
from getpass import getpass
import tagged
import transcode
import RedOpsAPI
import re
import logging
import base64
from cryptography.fernet import Fernet
import json
from os.path import exists
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
if sys.version_info < (3, 6, 0):
raise Exception("Requires Python 3.6.0 or newer")
def create_description(torrent, flac_dir, format, permalink):
# Create an example command to document the transcode process.
cmds = transcode.transcode_commands(format,
transcode.needs_resampling(flac_dir),
transcode.resample_rate(flac_dir),
'input.flac', 'output' + transcode.encoders[format]['ext'])
description = [
'Transcode process:',
'',
'[code]{0}[/code]'.format(' | '.join(cmds)),
'',
'Created using [url=https://github.com/kpdean/RED_OPS_Better]REDBetter (Chachhing fork)[/url]'
''
]
return description
def formats_needed(group, torrent, supported_formats):
same_group = lambda t: t['media'] == torrent['media'] and\
t['remasterYear'] == torrent['remasterYear'] and\
t['remasterTitle'] == torrent['remasterTitle'] and\
t['remasterRecordLabel'] == torrent['remasterRecordLabel'] and\
t['remasterCatalogueNumber'] == torrent['remasterCatalogueNumber']
others = filter(same_group, group['torrents'])
current_formats = set((t['format'], t['encoding']) for t in others)
missing_formats = [format for format, details in [(f, RedOpsAPI.formats[f]) for f in supported_formats]\
if (details['format'], details['encoding']) not in current_formats]
allowed_formats = RedOpsAPI.allowed_transcodes(torrent)
return [format for format in missing_formats if format in allowed_formats]
def cred_file_encrypt_decryption(cipher, authFile, creds=None, encry_decry=None):
if encry_decry == 1:
with open(authFile, 'w') as outfile:
json.dump(creds, outfile, indent=2)
byte_string = json.dumps(creds).encode('utf-8')
byte_ciphertext = cipher.encrypt(byte_string)
with open(authFile_enc, "wb") as encrypt_file:
encrypt_file.write(byte_ciphertext)
encrp_str_dict = json.loads(byte_string)
return encrp_str_dict
elif encry_decry == 0:
with open(authFile_enc, "rb") as decrypt_file:
data = decrypt_file.read()
decrypted_data = cipher.decrypt(data).decode('utf-8')
decrypted_data_dict = json.loads(decrypted_data)
return decrypted_data_dict
def main():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog='red_ops_better')
parser.add_argument('release_urls', nargs='*', help='the URL where the release is located')
parser.add_argument('-s', '--single', action='store_true', help='only add one format per release (useful for getting unique groups)')
parser.add_argument('-j', '--threads', type=int, help='number of threads to use when transcoding',
default=max(cpu_count() - 1, 1))
parser.add_argument('--config', help='the location of the configuration file', \
default=os.path.expanduser(os.getcwd()+'/config'))
parser.add_argument('--cache', help='the location of the cache', \
default=os.path.expanduser(os.getcwd()+'/cache'))
parser.add_argument('-U', '--no-upload', action='store_true', help='don\'t upload new torrents (in case you want to do it manually)')
parser.add_argument('-E', '--no-24bit-edit', action='store_true', help='don\'t try to edit 24-bit torrents mistakenly labeled as 16-bit')
parser.add_argument('-S', '--skip', action='store_true', help="treats a torrent as already processed")
parser.add_argument('-t', '--totp', help="time based one time password for 2FA", default=None)
parser.add_argument('-T', '--tracker', help="pick your tracker for transcoding (choose from RED or OPS)", default='RED')
args = parser.parse_args()
config = configparser.ConfigParser(interpolation=None)
try:
open(args.config)
config.read(args.config)
dictConfig = config._sections
OrderedDict_dict = (dict(dictConfig))
except:
if not os.path.exists(os.path.dirname(args.config)) and not os.path.exists(authFile_enc):
os.makedirs(os.path.dirname(args.config))
if not exists(authFile) and not exists(authFile_enc):
config.add_section('RED')
config.set('RED', 'username', '')
config.set('RED', 'password', '')
config.set('RED', 'session_cookie', '')
config.set('RED', 'data_dir', '')
config.set('RED', 'output_dir', '')
config.set('RED', 'torrent_dir', '')
config.set('RED', 'formats', 'flac, V0, 320')
config.set('RED', 'media', ', '.join(RedOpsAPI.lossless_media))
config.set('RED', '24bit_behaviour','0')
config.set('RED', 'tracker', 'https://flacsfor.me/')
config.set('RED', 'api', 'https://redacted.ch')
config.set('RED', 'source', 'RED')
config.set('RED', 'piece_length', '18')
config.add_section('OPS')
config.set('OPS', 'username', '')
config.set('OPS', 'password', '')
config.set('OPS', 'data_dir', '')
config.set('OPS', 'output_dir', '')
config.set('OPS', 'torrent_dir', '')
config.set('OPS', 'formats', 'flac, V0, 320')
config.set('OPS', 'media', ', '.join(RedOpsAPI.lossless_media))
config.set('OPS', '24bit_behaviour','0')
config.set('OPS', 'tracker', 'https://home.opsfet.ch/')
config.set('OPS', 'api', 'https://orpheus.network')
config.set('OPS', 'source', 'OPS')
config.set('OPS', 'piece_length', '18')
config.write(open(args.config, 'w'))
print("Please edit the configuration file: {0}".format(args.config))
os._exit(0)
finally:
print("Password Must include Minimum length (10), Capital and lower letter, number and special character")
password_provided = getpass("Provide a Password for Encry and Decrp: ") # This is input in the form of a string
count = 0
while count < 1:
if(any(x.isupper() for x in password_provided) and any(x.islower() for x in password_provided) and any(x.isdigit() for x in password_provided) and len(password_provided) >= 10):
password_provided = password_provided
count += 1
else:
print("Password Must include Minimum length (10), Capital and lower letter, number and special character.")
password_provided = getpass("Provide a Password for Encry and Decrp: ")
password = password_provided.encode() # Convert to type bytes
#CHANGE THIS - Recommend--To Change os.urandom(16), must be of type bytes
#$python3.6 >>> import os >>> os.urandom(16) (Copy Output and Replace salt value)
salt = b'\xcfFI\xe8lgR\xdf!tt\xeeO\xc6\x12l\x13\xfa:\xf2\x13p\xfed\xca\x00\xfcb\xfe_`\xce=0\t\xbcRS[\xa7e)P\xeb\nrO&Z\x16\x9dvV;\x84q\x1d\xc7\x17Hq\x11\x8e\xca'
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=1000000,
backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(password))
cipher = Fernet(key)
if exists(authFile) and not exists(authFile_enc):
file_response = cred_file_encrypt_decryption(cipher, authFile, OrderedDict_dict, encry_decry=1)
if exists(authFile):
os.remove(authFile)
elif exists(authFile_enc):
file_response = cred_file_encrypt_decryption(cipher, authFile_enc, encry_decry=0)
if exists(authFile):
os.remove(authFile)
TrackerArgs = args.tracker.upper()
if TrackerArgs == 'RED':
config_dict = file_response['RED']
trackerLogingto = 'Redacted'
elif TrackerArgs == 'OPS':
config_dict = file_response['OPS']
trackerLogingto = 'Orpheus Network'
else:
print("Tracker argument must be (RED or OPS).")
sys.exit(2)
username = config_dict['username']
password = config_dict['password']
try:
session_cookie = os.path.expanduser(config_dict['session_cookie'])
except configparser.NoOptionError:
session_cookie = None
if not username and not password and not session_cookie:
if exists(authFile_enc):
os.remove(authFile_enc)
print('Configuration file requires Username and password or Session Cookie.')
sys.exit(2)
do_24_bit = config_dict['24bit_behaviour']
data_dir = [os.path.expanduser(d) for d in config_dict['data_dir'].split(';')]
try:
output_dir = os.path.expanduser(config_dict['output_dir'])
except configparser.NoOptionError:
output_dir = None
if not output_dir:
output_dir = data_dir[0]
torrent_dir = os.path.expanduser(config_dict['torrent_dir'])
supported_formats = [format.strip().upper() for format in config_dict['formats'].split(',')]
try:
media_config = config_dict['media']
if not media_config:
supported_media = RedOpsAPI.lossless_media
else:
supported_media = set([medium.strip().lower() for medium in media_config.split(',')])
if not supported_media.issubset(set(RedOpsAPI.lossless_media)):
print('Unsupported media type "{0}", edit your configuration'.format((supported_media - RedOpsAPI.lossless_media).pop()))
print("Supported types are: {0}".format(', '.join(RedOpsAPI.lossless_media)))
sys.exit(2)
except configparser.NoOptionError:
supported_media = RedOpsAPI.lossless_media
tracker = config_dict['tracker']
endpoint = config_dict['api']
source = config_dict['source']
piece_length = config_dict['piece_length']
upload_torrent = not args.no_upload
print("Logging in to " + trackerLogingto + "....")
api = RedOpsAPI.RED_OPS_API(username, password, session_cookie, endpoint, args.totp)
try:
seen = pickle.load(open(args.cache, 'rb'))
except:
seen = set()
pickle.dump(seen, open(args.cache, 'wb'))
if args.skip:
skip = [int(query['torrentid']) for query in\
[dict(parse_qsl(urlparse(url).query)) for url in args.release_urls]]
for id in skip:
print("Skipping torrent {0}".format(str(id)))
seen.add(str(id))
pickle.dump(seen, open(args.cache, 'wb'))
return
print("Searching for transcode candidates...")
if args.release_urls:
if len(args.release_urls) == 1 and os.path.isfile(args.release_urls[0]):
print("You supplied a url list, ignoring your configuration's media types.")
with open(args.release_urls[0]) as f:
candidates = [(int(query['id']), int(query['torrentid'])) for query in\
[dict(parse_qsl(urlparse(url).query)) for url in f]]
else:
print("You supplied one or more release URLs, ignoring your configuration's media types.")
candidates = [(int(query['id']), int(query['torrentid'])) for query in\
[dict(parse_qsl(urlparse(url).query)) for url in args.release_urls]]
else:
candidates = api.get_candidates(skip=seen, media=supported_media)
for groupid, torrentid in candidates:
group = api.request('torrentgroup', id=groupid)
if group != None:
torrent = [t for t in group['torrents'] if t['id'] == torrentid][0]
artist = "";
if len(group['group']['musicInfo']['artists']) > 2:
artist = "Various Artists"
elif not group['group']['musicInfo']['artists'] and len(group['group']['musicInfo']['composers']) > 2:
artist = "Various Artists"
elif len(group['group']['musicInfo']['artists']) == 2:
artist = group['group']['musicInfo']['artists'][0]['name'] + " & " + group['group']['musicInfo']['artists'][1]['name']
elif not group['group']['musicInfo']['artists'] and len(group['group']['musicInfo']['composers']) == 2:
artist = group['group']['musicInfo']['composers'][0]['name'] + " & " + group['group']['musicInfo']['composers'][1]['name']
elif not group['group']['musicInfo']['artists'] and len(group['group']['musicInfo']['composers']):
artist = group['group']['musicInfo']['composers'][0]['name']
else:
artist = group['group']['musicInfo']['artists'][0]['name']
year = str(torrent['remasterYear'])
if year == "0":
year = str(group['group']['year'])
media = str(torrent['media'])
releaseartist = "Release artist(s): {0}" .format(artist)
releasename = "Release name : {0}" .format(RedOpsAPI.unescape(group['group']['name']))
releaseyear = "Release year : {0}" .format(year)
releasemedia = "Release Media : {0}" .format(media)
releaseurl = "Release URL : {0}" .format(api.release_url(group, torrent))
print("\n\n")
print((releaseartist + "\n" + releasename + "\n" + releaseyear + "\n" + releasemedia + "\n" + releaseurl + "\n\n" ))
if not torrent['filePath']:
for d in data_dir:
flac_file = os.path.join(d, RedOpsAPI.unescape(torrent['fileList']).split('{{{')[0])
if not os.path.exists(flac_file):
continue
flac_dir = os.path.join(d, "{0} ({1}) [FLAC]".format(
RedOpsAPI.unescape(group['group']['name']), group['group']['year']))
if not os.path.exists(flac_dir):
os.makedirs(flac_dir)
shutil.copy(flac_file, flac_dir)
break
if not os.path.exists(flac_file):
continue
else:
for d in data_dir:
flac_dir = os.path.join(d, RedOpsAPI.unescape(torrent['filePath']))
if os.path.exists(flac_dir):
break
if int(do_24_bit):
try:
if transcode.is_24bit(flac_dir) and torrent['encoding'] != '24bit Lossless':
# A lot of people are uploading FLACs from Bandcamp without realizing
# that they're actually 24 bit files (usually 24/44.1). Since we know for
# sure whether the files are 24 bit, we might as well correct the listing
# on the site (and get an extra upload in the process).
if args.no_24bit_edit:
print("Release is actually 24-bit lossless, skipping.")
continue
if int(do_24_bit) == 1:
confirmation = raw_input("Mark release as 24bit lossless? y/n: ")
if confirmation != 'y':
continue
print("Marking release as 24bit lossless.")
api.set_24bit(torrent)
group = api.request('torrentgroup', id=groupid)
torrent = [t for t in group['torrents'] if t['id'] == torrentid][0]
except Exception as e:
print("Error: can't edit 24-bit torrent - skipping: {0}".format(e))
continue
if transcode.is_multichannel(flac_dir):
print("This is a multichannel release, which is unsupported - skipping")
continue
needed = formats_needed(group, torrent, supported_formats)
print("Formats needed: {0}".format(', '.join(needed)))
if needed:
# Before proceeding, do the basic tag checks on the source
# files to ensure any uploads won't be reported, but punt
# on the tracknumber formatting; problems with tracknumber
# may be fixable when the tags are copied.
broken_tags = False
for flac_file in transcode.locate(flac_dir, transcode.ext_matcher('.flac')):
(ok, msg) = tagged.check_tags(flac_file, check_tracknumber_format=False)
if not ok:
print("A FLAC file in this release has unacceptable tags - skipping: {0}".format(msg))
print("You might be able to trump it.")
broken_tags = True
break
if broken_tags:
continue
while os.path.exists(flac_dir) == False:
print("Path not found: {0}".format(flac_dir))
alternative_file_path_exists = ""
while (alternative_file_path_exists.lower() != "y") and (alternative_file_path_exists.lower() != "n"):
alternative_file_path_exists = input("Do you wish to provide an alternative file path? (y/n): ")
if alternative_file_path_exists.lower() == "y":
flac_dir = input("Alternative file path: ")
else:
break
for format in needed:
if os.path.exists(flac_dir):
print("Adding format {0}...".format(format), end=" ")
tmpdir = tempfile.mkdtemp()
AlbumName = RedOpsAPI.unescape(group['group']['name'])
try:
local_output_dir = config_dict['output_dir_{}'.format(format).lower()]
except:
local_output_dir = output_dir
try:
local_torrent_dir = config_dict['torrent_dir_{}'.format(format).lower()]
except:
local_torrent_dir = torrent_dir
try:
if len(torrent['remasterTitle']) >= 1:
basename = artist + " - " + AlbumName + " (" + torrent['remasterTitle'] + ") " + "[" + year + "] (" + torrent['media'] + " - "
else:
basename = artist + " - " + AlbumName + " [" + year + "] (" + torrent['media'] + " - "
transcode_dir = transcode.transcode_release(flac_dir, local_output_dir, basename, format, max_threads=args.threads)
if transcode_dir == False:
print("Skipping - some file(s) in this release were incorrectly marked as 24bit.")
break
new_torrent = transcode.make_torrent(transcode_dir, tmpdir, tracker, api.passkey, piece_length, source)
if upload_torrent:
permalink = api.permalink(torrent)
description = create_description(torrent, flac_dir, format, permalink)
if TrackerArgs == 'OPS':
api.uploadOPS(group, torrent, new_torrent, format, description)
elif TrackerArgs == 'RED':
api.uploadRED(group, torrent, new_torrent, format, description)
shutil.copy(new_torrent, local_torrent_dir)
print("done!")
if args.single: break
except Exception as e:
print("Error adding format {0}: {1}".format(format, e))
traceback.print_exc()
finally:
shutil.rmtree(tmpdir)
else:
print("Path not found - skipping: {0}".format(flac_dir))
break
seen.add(str(torrentid))
pickle.dump(seen, open(args.cache, 'wb'))
if __name__ == "__main__":
authFile = "config"
authFile_enc = "config.enc"
main()