-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathscene.py
More file actions
81 lines (64 loc) · 2.33 KB
/
scene.py
File metadata and controls
81 lines (64 loc) · 2.33 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
# -*- coding: utf-8 -*-
import os
import requests
import progressbar
from base64 import b64decode
from zlib import crc32
from .logging import log
srrdb = b64decode('aHR0cHM6Ly9zcnJkYi5jb20v').decode('utf8')
def check_scene_rename(fname, release):
release_url = srrdb + "release/details/{}".format(release)
r = requests.get(release_url)
if fname not in r.text:
log.warning('Possibly renamed scene file!\n'
'\tFilename {}\n\tnot found at {}',
fname, release_url)
def crc(path):
log.debug('Calculating CRC32 value')
checksum = 0
fsize = os.path.getsize(path)
i = 0
chunk_size = 4 * 2**20
with open(path, 'rb') as f:
with progressbar.DataTransferBar(max_value=fsize,
max_error=False) as bar:
while True:
i += 1
data = f.read(chunk_size)
if not data:
return checksum & 0xFFFFFFFF
bar.update(i*chunk_size)
checksum = crc32(data, checksum)
def is_scene_crc(path):
checksum = crc(path)
log.debug('CRC32 {:08X}', checksum)
r = requests.get(srrdb + 'api/search/archive-crc:%08X' % checksum)
r.raise_for_status()
scene = int(r.json()['resultsCount']) != 0
if int(r.json()['resultsCount']) > 1:
log.warning('More than one srrDB result for CRC32 query')
log.info('Scene checkbox set to {} '
'due to CRC query result'.format(scene))
if scene:
release = r.json()['results'][0]['release']
fname = os.path.basename(path)
check_scene_rename(fname, release)
return scene
def query_scene_fname(path):
if os.path.isfile(path):
query = os.path.splitext(os.path.basename(path))[0]
elif os.path.isdir(path):
query = os.path.basename(path)
elif not os.path.exists(path):
raise FileNotFoundError('File or directory not found: %s' % (path,))
else:
raise Exception('wat')
# full search (slow)
r = requests.get(srrdb + "api/search/{}".format(query))
r.raise_for_status()
results = r.json()['results']
if results:
print('Found srrDB results for filename:')
print("\t" + "\n".join(r['release'] for r in results))
else:
print('No results found in srrDB for query "{}"'.format(query))