-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchanarchiverenamer.py
More file actions
executable file
·85 lines (69 loc) · 2.39 KB
/
chanarchiverenamer.py
File metadata and controls
executable file
·85 lines (69 loc) · 2.39 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
#!/usr/bin/env python3
'''
TODO: documentation!
Created on 4 April 2013.
@author: Anton Eliasson <devel@antoneliasson.se>
'''
import logging
import os
from lxml.html import parse
def get_filename_mappings(root):
mappings = []
filetexts = root.xpath('//span[@class="fileText"]')
for filetext in filetexts:
src = filetext[0].text
dst = filetext[1].attrib['title']
mapping = (src, dst)
logging.debug('Found mapping: %s', mapping)
mappings.append(mapping)
return mappings
def safe_rename(src, dst):
''' os.rename() is subject to race conditions. This solution should be race-free. '''
try:
os.link(src, dst)
except FileNotFoundError:
logging.warning('File %s that should have been renamed to %s does not exist', src, dst)
# skip
return
except FileExistsError:
# destination file already exists, make up a new filename
nbr = os.path.splitext(src)[0]
name = os.path.splitext(dst)[0]
ext = os.path.splitext(dst)[1]
new_dst = '{}-{}{}'.format(name, nbr, ext)
logging.warning('%s already exists; renaming to %s instead', dst, new_dst)
os.link(src, new_dst)
# die if that didn't work
os.unlink(src)
def rename_all(filename_mappings):
for mapping in filename_mappings:
safe_rename(mapping[0], mapping[1])
def get_thumbnails(root):
thumbs = []
filethumbs = root.xpath('//a[@class="fileThumb"]')
for filethumb in filethumbs:
img = filethumb[0]
path = img.attrib['src']
fname = path.split('/').pop()
logging.debug('Found thumbnail: %s', fname)
thumbs.append(fname)
return thumbs
def cleanup(root):
thumbnails = get_thumbnails(root)
for f in thumbnails:
logging.debug('Removing thumbnail: %s', f)
try:
os.remove(f)
except FileNotFoundError:
logging.warning('Thumbnail %s that should have been deleted does not exist', f)
def main():
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
tree = parse('index.html')
root = tree.getroot()
filename_mappings = get_filename_mappings(root)
rename_all(filename_mappings)
cleanup(root)
logging.info('Done. You get to clean up the CSS and JS files manually.')
# TODO: remove <op's file>_{75,150}.jpg
if __name__ == '__main__':
main()