reddit-image-wall-getter/hash_thumbnailer_distributed/webproj/thumbnailer/management/commands/loadhashes.py

34 lines
1.3 KiB
Python

from pathlib import Path
from django.core.management.base import BaseCommand, no_translations
from webproj.thumbnailer.models import ErrorLog, Job, Log, PerformanceLog
class Command(BaseCommand):
def add_arguments(self, parser):
return
def handle(self, *args, **options):
# print(sorted(dir(self.style)))
self.stdout.write(self.style.HTTP_INFO(
'Truncating all relevant tables...'))
Job.truncate_table()
ErrorLog.truncate_table()
PerformanceLog.truncate_table()
self.stdout.write(self.style.SUCCESS('Tables trucated...'))
self.stdout.write(self.style.HTTP_INFO('Reading "hashes.txt"...'))
hashes = dict(map(lambda a: a.split('|', 1), Path(
'hashes.txt').read_text().splitlines()))
hashes_len = len(hashes)
self.stdout.write(self.style.HTTP_INFO('Adding entries to list...'))
jobs = list()
for seq, (hsh, file) in enumerate(hashes.items()):
jobs.append(Job(hsh=hsh, file=file))
if seq % 100000 == 0:
print(f'Added {seq} of {hashes_len}')
self.stdout.write(self.style.HTTP_INFO(
'Bulk-creating jobs into database...'))
Job.objects.bulk_create(jobs)
self.stdout.write(self.style.SUCCESS('Hashes loaded.'))