Add file hashing to the export.

This commit is contained in:
RecursiveGreen 2019-06-10 15:48:55 -04:00
parent c7811ddd11
commit 490e37a2f5

View file

@ -7,6 +7,7 @@ by the new database later.
import argparse import argparse
from decimal import Decimal, getcontext from decimal import Decimal, getcontext
import hashlib
import json import json
import mimetypes import mimetypes
import os import os
@ -38,6 +39,19 @@ def detect_mime(path):
return mimetype return mimetype
def hash_file(path):
'''
Run a music file through a hashing algorithm (SHA3_256) and return the
hexidecimal digest.
'''
try:
with open(path, 'rb') as file:
filehash = hashlib.sha3_256(file.read()).hexdigest()
except OSError:
filehash = None
return filehash
def adapt_decimal(number): def adapt_decimal(number):
'''Sqlite3 adapter for Decimal types''' '''Sqlite3 adapter for Decimal types'''
return str(number) return str(number)
@ -142,6 +156,7 @@ def import_sqlite3(db_file):
store = {'path': scrub(song[7]), store = {'path': scrub(song[7]),
'mime': detect_mime(scrub(song[7])), 'mime': detect_mime(scrub(song[7])),
'filesize': os.stat(scrub(song[7])).st_size, 'filesize': os.stat(scrub(song[7])).st_size,
'filehash': hash_file(scrub(song[7])),
'length': song[6]} 'length': song[6]}
songs.append({'album': scrub(song[2]), songs.append({'album': scrub(song[2]),
'artists': song_artists, 'artists': song_artists,