Sfoglia il codice sorgente

changed check order, first date then checksum

Gamaliel Espinoza 8 anni fa
parent
commit
5227d95670
2 ha cambiato i file con 35 aggiunte e 29 eliminazioni
  1. 1 1
      tools/fourier/__init__.py
  2. 34 28
      tools/fourier/cli.py

+ 1 - 1
tools/fourier/__init__.py

@@ -1 +1 @@
-__version__ = '1.0.0b3'
+__version__ = '1.0.0b4'

+ 34 - 28
tools/fourier/cli.py

@@ -27,6 +27,17 @@ def insert_file(hsh, filename, timestamp, db=None):
         (hsh, filename, int(time.mktime(timestamp.timetuple())), )
     )
 
+def hash_file(filename):
+    ahash = md5()
+    with open(filename, 'rb') as fp:
+        while True:
+            data = fp.read(4096)
+            if data:
+                ahash.update(data)
+            else:
+                break
+    return ahash.hexdigest()
+
 def main():
     parser = ArgumentParser()
     subparsers = parser.add_subparsers(dest='entity')
@@ -67,35 +78,30 @@ def main():
         path = os.path.join('/var/fourier', device_id)
         for folder, folders, files in os.walk(path):
             for file in files:
-                if file.endswith('.mp3'):
-                    filename = os.path.join(folder, file)
-                    ahash = md5()
-                    with open(filename, 'rb') as fp:
-                        while True:
-                            data = fp.read(4096)
-                            if data:
-                                ahash.update(data)
-                            else:
-                                break
-                    thehash = ahash.hexdigest()
-                    exists = find_hash(thehash, db=conn)
-                    if not exists:
-                        dt = datetime.strptime(
-                            file[:19],
-                            '%Y-%m-%dT%H-%M-%S'
-                        )
-                        if args.from_date:
-                            if dt >= args.from_date:
-                                counter += 1
-                                insert_file(thehash, filename, dt, db=conn)
-                                print(dt)
-                        else:
-                            counter += 1
-                            insert_file(thehash, filename, dt, db=conn)
-                            print(dt)
+                if not file.endswith('.mp3'):
+                    continue
+
+                filename = os.path.join(folder, file)
+
+                dt = datetime.strptime(
+                    file[:19],
+                    '%Y-%m-%dT%H-%M-%S'
+                )
+                try:
+                    if args.from_date:
+                        do_insert = dt >= args.from_date  
                     else:
-                        already_indexed += 1
-                        print('already indexed: {}'.format(filename))
+                        do_insert = True
+
+                    if do_insert:
+                        thehash = hash_file(filename)
+                        insert_file(thehash, filename, dt, db=conn)
+                        counter += 1
+                        print(dt)
+
+                except sqlite3.IntegrityError:
+                    already_indexed += 1
+                    print('already indexed: {}'.format(filename))
 
         if not args.dry_run:
             conn.commit()