forked from Mechazawa/REDBetter-crawler
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathorpheusbetter
executable file
·327 lines (289 loc) · 14.9 KB
/
orpheusbetter
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
#!/usr/bin/env python3
import configparser
import argparse
import pickle
import os
import shutil
import sys
import tempfile
import traceback
from packaging.version import Version
from urllib.parse import urlparse, parse_qsl
from multiprocessing import cpu_count
import tagging
import transcode
import whatapi
from _version import __version__
if sys.version_info < (3, 6, 0):
raise Exception("Requires Python 3.6.0 or newer")
def banner():
return "Created with version of orpheusbetter-crawler {0}. Maintained by ApexWeed\n" \
"This transcoding was done by an autonomous system".format(__version__)
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 of [url={0}]{0}[/url]'.format(permalink),
'',
'Transcode process:',
'',
'[code]{0}[/code]'.format(' | '.join(cmds)),
'',
banner()
]
return description
def formats_needed(group, torrent, supported_formats):
if '' in supported_formats: supported_formats.remove('')
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, whatapi.formats[f]) for f in supported_formats]\
if (details['format'], details['encoding']) not in current_formats]
allowed_formats = whatapi.allowed_transcodes(torrent)
return [format for format in missing_formats if format in allowed_formats]
def main():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog='orpheusbetter')
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('~/.orpheusbetter/config'))
parser.add_argument('--cache', help='the location of the cache', \
default=os.path.expanduser('~/.orpheusbetter/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('--version', action='version', version='%(prog)s ' + __version__)
parser.add_argument('-m', '--mode', help='mode to search for transcode candidates; snatched, uploaded, both, seeding, or all')
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('-o', '--source', help="the value to put in the source flag in created torrents")
args = parser.parse_args()
config = configparser.ConfigParser(interpolation=None)
try:
open(args.config)
config.read(args.config)
except:
if not os.path.exists(os.path.dirname(args.config)):
os.makedirs(os.path.dirname(args.config))
config.add_section('orpheus')
config.set('orpheus', 'username', '')
config.set('orpheus', 'password', '')
config.set('orpheus', 'data_dir', '')
config.set('orpheus', 'output_dir', '')
config.set('orpheus', 'torrent_dir', '')
config.set('orpheus', 'formats', 'flac, v0, 320')
config.set('orpheus', 'media', ', '.join(whatapi.lossless_media))
config.set('orpheus', '24bit_behaviour','0')
config.set('orpheus', 'tracker', 'https://home.opsfet.ch/')
config.set('orpheus', 'mode', 'both')
config.set('orpheus', 'api', 'https://orpheus.network')
config.set('orpheus', 'source', 'OPS')
config.write(open(args.config, 'w'))
print("Please edit the configuration file: {0}".format(args.config))
sys.exit(2)
finally:
username = config.get('orpheus', 'username')
password = config.get('orpheus', 'password')
do_24_bit = config.get('orpheus', '24bit_behaviour')
data_dir = [os.path.expanduser(d) for d in config.get('orpheus', 'data_dir').split(';')]
try:
output_dir = os.path.expanduser(config.get('orpheus', 'output_dir'))
except configparser.NoOptionError:
output_dir = None
if not output_dir:
output_dir = data_dir[0]
torrent_dir = os.path.expanduser(config.get('orpheus', 'torrent_dir'))
supported_formats = [format.strip().upper() for format in config.get('orpheus', 'formats').split(',')]
try:
media_config = config.get('orpheus', 'media')
if not media_config:
supported_media = whatapi.lossless_media
else:
supported_media = set([medium.strip().lower() for medium in media_config.split(',')])
if not supported_media.issubset(set(whatapi.lossless_media)):
print('Unsupported media type "{0}", edit your configuration'.format((supported_media - whatapi.lossless_media).pop()))
print("Supported types are: {0}".format(', '.join(whatapi.lossless_media)))
sys.exit(2)
except configparser.NoOptionError:
supported_media = whatapi.lossless_media
if not config.has_option('orpheus', 'tracker'):
config.set('orpheus', 'tracker', 'https://home.opsfet.ch/')
config.write(open(args.config, 'w'))
print("Tracker added to config file")
if not config.has_option('orpheus', 'mode'):
config.set('orpheus', 'mode', 'both')
config.write(open(args.config, 'w'))
print("Candidate mode set to both")
if not config.has_option('orpheus', 'api'):
config.set('orpheus', 'api', 'https://orpheus.network')
config.write(open(args.config, 'w'))
print("API endpoint set to https://orpheus.network")
version_path = os.path.join(os.path.expanduser('~'), '.orpheusbetter', '.version')
if os.path.isfile(version_path):
with open(version_path, 'r') as f:
version = f.read()
else:
version = "1.0.0"
if Version(version) < Version(__version__):
updated = False
if Version(version) < Version('2.0.1'):
updated = True
config.set('orpheus', 'source', 'OPS')
if updated:
config.write(open(args.config, 'w'))
print("Migrated version {0} config".format(version))
with open(version_path, 'w') as f:
f.write(__version__)
tracker = config.get('orpheus', 'tracker')
mode = config.get('orpheus', 'mode')
endpoint = config.get('orpheus', 'api')
source = None
if config.has_option('orpheus', 'source'):
source = config.get('orpheus', 'source')
upload_torrent = not args.no_upload
print("Logging in to Orpheus Network...")
api = whatapi.WhatAPI(username, password, 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:
if args.mode is None:
if mode == 'none':
print("No urls provided and scraping is disabled")
else:
candidates = api.get_candidates(mode, skip=seen, media=supported_media)
elif args.mode == 'none':
print("No urls provided and scraping is disabled")
else:
candidates = api.get_candidates(args.mode, skip=seen, media=supported_media)
for groupid, torrentid in candidates:
group = api.request('torrentgroup', id=groupid)
torrent = [t for t in group['torrents'] if t['id'] == torrentid][0]
release = "Release found: {0} ({1})".format(whatapi.unescape(group['group']['name']), group['group']['year'])
releaseurl = "Release URL: {0}".format(api.release_url(group, torrent))
print("")
print(release)
print(releaseurl)
if not torrent['filePath']:
for d in data_dir:
flac_file = os.path.join(d, whatapi.unescape(torrent['fileList']).split('{{{')[0])
if not os.path.exists(flac_file):
continue
flac_dir = os.path.join(d, "{0} ({1}) [FLAC]".format(
whatapi.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):
print("Path not found - skipping: {0}".format(flac_file))
continue
else:
for d in data_dir:
flac_dir = os.path.join(d, whatapi.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 = 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) = tagging.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
for format in needed:
if os.path.exists(flac_dir):
print("Adding format {0}...".format(format), end=" ")
tmpdir = tempfile.mkdtemp()
try:
local_output_dir = config.get('orpheus', 'output_dir_{}'.format(format))
except:
local_output_dir = output_dir
try:
local_torrent_dir = config.get('orpheus', 'torrent_dir_{}'.format(format))
except:
local_torrent_dir = torrent_dir
try:
transcode_dir = transcode.transcode_release(flac_dir, local_output_dir, format, max_threads=args.threads)
new_torrent = transcode.make_torrent(transcode_dir, tmpdir, tracker, api.passkey, source)
if upload_torrent:
permalink = api.permalink(torrent)
description = create_description(torrent, flac_dir, format, permalink)
api.upload(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__":
main()