Mercurial > danboorufs
comparison danboorufs.py @ 8:c93cfd58112e draft default tip
Get the tags from danbooru’s json format. Warning: breaks the compatibility with older databases!
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 25 May 2013 18:30:25 +0200 |
parents | 09945ce42e28 |
children |
comparison
equal
deleted
inserted
replaced
7:09945ce42e28 | 8:c93cfd58112e |
---|---|
30 from threading import Lock | 30 from threading import Lock |
31 from time import time | 31 from time import time |
32 from functools import reduce | 32 from functools import reduce |
33 | 33 |
34 import os | 34 import os |
35 import json | |
35 | 36 |
36 from fuse import FUSE, FuseOSError, Operations, LoggingMixIn | 37 from fuse import FUSE, FuseOSError, Operations, LoggingMixIn |
37 | 38 |
38 | 39 |
39 class Danbooru(LoggingMixIn, Operations): | 40 class Danbooru(LoggingMixIn, Operations): |
40 ''' | 41 ''' |
41 Represent a list of images as a filesystem tree, with nice tag filtering. | 42 Represent a list of images as a filesystem tree, with nice tag filtering. |
42 ''' | 43 ''' |
43 | 44 |
44 def __init__(self, tagfiles, root, use_symlinks): | 45 def __init__(self, json_files, root, use_symlinks): |
45 ''' | 46 ''' |
46 Takes a list of files containing the tags. They have to be named as the | 47 Takes a list of files containing the tags. They have to be named as the |
47 image, with ".tags" at the end. | 48 image, with ".json" at the end. |
48 ''' | 49 ''' |
49 self.paths = {} | 50 self.paths = {} |
50 self.files = {} | 51 self.files = {} |
51 self.tags = {} | 52 self.tags = {} |
52 self.cache = {} | 53 self.cache = {} |
53 | 54 |
54 start = time() | 55 start = time() |
55 | 56 |
56 for name in tagfiles: | 57 for json_name in json_files: |
57 filename = name.replace('.tags', '') | 58 with open(json_name, 'r') as jsonfile: |
58 basename = os.path.basename(filename) | 59 data = json.load(jsonfile) |
59 self.paths[basename] = filename | 60 |
60 tags = [] | 61 tags = [tag.replace('/', '∕') for tag in data['tag_string'].split()] + ['rating:' + data['rating']] |
61 self.files[basename] = tags | 62 name = '{}.{}'.format(data['id'], data['file_ext']) |
62 with open(name, 'rb') as tagfile: | 63 self.paths[name] = json_name[:-4] + data['file_ext'] |
63 for line in tagfile: | 64 self.files[name] = tags |
64 for tag in line.split(): | 65 for tag in tags: |
65 try: | 66 self.tags.setdefault(tag, []).append(name) |
66 tag = tag.decode('UTF-8') | |
67 except UnicodeDecodeError: | |
68 continue | |
69 except AttributeError: | |
70 pass | |
71 tag = tag.replace('/', '�') #XXX | |
72 tags.append(tag) | |
73 self.tags.setdefault(tag, []).append(basename) | |
74 | 67 |
75 print('[%d] Index done.' % (time() - start)) | 68 print('[%d] Index done.' % (time() - start)) |
76 | 69 |
77 self.root = root | 70 self.root = root |
78 self.use_symlinks = use_symlinks | 71 self.use_symlinks = use_symlinks |
199 filelist = [] | 192 filelist = [] |
200 start = time() | 193 start = time() |
201 for directory in directories: | 194 for directory in directories: |
202 for (path, _, files) in os.walk(directory): | 195 for (path, _, files) in os.walk(directory): |
203 filelist.extend(os.path.join(path, filename) for filename in files | 196 filelist.extend(os.path.join(path, filename) for filename in files |
204 if filename.endswith('.tags')) | 197 if filename.endswith('.json')) |
205 print('[%d] Walk done.' % (time() - start)) | 198 print('[%d] Walk done.' % (time() - start)) |
206 | 199 |
207 FUSE(Danbooru(filelist, os.path.dirname(mountpoint), use_symlinks), | 200 FUSE(Danbooru(filelist, os.path.dirname(mountpoint), use_symlinks), |
208 mountpoint, foreground=True) | 201 mountpoint, foreground=True) |
209 | 202 |
210 | 203 |
211 if __name__ == '__main__': | 204 if __name__ == '__main__': |
212 if len(argv) < 3: | 205 if len(argv) < 3: |
213 print('USAGE: %s' % argv[0], '[-n|--no-symlinks]', | 206 print('USAGE: %s' % argv[0], '[-n|--no-symlinks]', |
214 '<tags directory>', '<mountpoint>') | 207 '<directory> [<directory> ...]', '<mountpoint>') |
215 exit(1) | 208 exit(1) |
216 | 209 |
217 main(argv) | 210 main(argv) |