changeset 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
files danboorufs.py
diffstat 1 files changed, 15 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/danboorufs.py
+++ b/danboorufs.py
@@ -32,6 +32,7 @@ from time import time
 from functools import reduce
 
 import os
+import json
 
 from fuse import FUSE, FuseOSError, Operations, LoggingMixIn
 
@@ -41,10 +42,10 @@ class Danbooru(LoggingMixIn, Operations)
     Represent a list of images as a filesystem tree, with nice tag filtering.
     '''
 
-    def __init__(self, tagfiles, root, use_symlinks):
+    def __init__(self, json_files, root, use_symlinks):
         '''
         Takes a list of files containing the tags. They have to be named as the
-        image, with ".tags" at the end.
+        image, with ".json" at the end.
         '''
         self.paths = {}
         self.files = {}
@@ -53,24 +54,16 @@ class Danbooru(LoggingMixIn, Operations)
 
         start = time()
 
-        for name in tagfiles:
-            filename = name.replace('.tags', '')
-            basename = os.path.basename(filename)
-            self.paths[basename] = filename
-            tags = []
-            self.files[basename] = tags
-            with open(name, 'rb') as tagfile:
-                for line in tagfile:
-                    for tag in line.split():
-                        try:
-                            tag = tag.decode('UTF-8')
-                        except UnicodeDecodeError:
-                            continue
-                        except AttributeError:
-                            pass
-                        tag = tag.replace('/', '�') #XXX
-                        tags.append(tag)
-                        self.tags.setdefault(tag, []).append(basename)
+        for json_name in json_files:
+            with open(json_name, 'r') as jsonfile:
+                data = json.load(jsonfile)
+
+            tags = [tag.replace('/', '∕') for tag in data['tag_string'].split()] + ['rating:' + data['rating']]
+            name = '{}.{}'.format(data['id'], data['file_ext'])
+            self.paths[name] = json_name[:-4] + data['file_ext']
+            self.files[name] = tags
+            for tag in tags:
+                self.tags.setdefault(tag, []).append(name)
 
         print('[%d] Index done.' % (time() - start))
 
@@ -201,7 +194,7 @@ def main(args):
     for directory in directories:
         for (path, _, files) in os.walk(directory):
             filelist.extend(os.path.join(path, filename) for filename in files
-                            if filename.endswith('.tags'))
+                            if filename.endswith('.json'))
     print('[%d] Walk done.' % (time() - start))
 
     FUSE(Danbooru(filelist, os.path.dirname(mountpoint), use_symlinks),
@@ -211,7 +204,7 @@ def main(args):
 if __name__ == '__main__':
     if len(argv) < 3:
         print('USAGE: %s' % argv[0], '[-n|--no-symlinks]',
-              '<tags directory>', '<mountpoint>')
+              '<directory> [<directory> ...]', '<mountpoint>')
         exit(1)
 
     main(argv)