steeldonutcollection/backend/management/commands/importvideos.py
2022-05-05 17:40:57 +02:00

48 lines
1.7 KiB
Python

from django.core.management.base import BaseCommand
from django.conf import settings
from datetime import timedelta, datetime
from json import loads
from backend.s3 import build_s3_from_settings
from backend.models import Video
class Command(BaseCommand):
help = 'Imports all videos from storage'
def handle(self, *args, **kwargs):
s3 = build_s3_from_settings()
if not s3.bucket_exists(settings.S3_BUCKET):
s3.make_bucket(settings.S3_BUCKET)
with open(settings.BASE_DIR / "sdc.json") as policyfile:
policy = policyfile.read()
s3.set_bucket_policy(settings.S3_BUCKET, policy)
objects = list(s3.list_objects(settings.S3_BUCKET))
names = [obj.object_name for obj in objects]
for obj in objects:
if obj.object_name.endswith(".info.json"):
base = obj.object_name.rsplit(".info.json")[0]
if (videoname := f"{base}.mp4") in names:
vjson = s3.get_object(
settings.S3_BUCKET, obj.object_name).read().decode()
vdata = loads(vjson)
try:
vobj = Video.objects.get(id=vdata["id"])
except Video.DoesNotExist:
vobj = Video()
vobj.title = vdata["title"]
vobj.published = datetime.strptime(vdata["upload_date"], "%Y%m%d")
vobj.description = vdata["description"]
vobj.path = videoname
vobj.thumbnail_path = f"{base}.webp"
vobj.original_json = vjson
vobj.save()