unusualarchive/import.py
2023-05-09 22:12:39 +02:00

58 lines
1.8 KiB
Python

import os
import re
import sys
from datetime import datetime
sys.path.append('./')
os.environ['DJANGO_SETTINGS_MODULE'] = 'ragnarok.settings'
import django
django.setup()
from django.contrib.auth.models import User
from core.models import Video
folder_path = 'youtube_channel_archiver/UnusualVideos (@UnusualVideos)/'
def extract_version(filename):
# Define a regular expression pattern to match the version number
pattern = r'V(\d+)'
# Use the pattern to search for a match in the filename
match = re.search(pattern, filename)
if match:
# If a match is found, extract the version number from the matched string
version = match.group(1)
# Return the version number as an integer
return int(version)
else:
# If no match is found, return None
return None
for filename in os.listdir(folder_path):
if filename.endswith('.mp4'):
# Create a new Video object
video = Video()
id = extract_version(filename)
ret = Video.objects.filter(id=id)
if ret:
print("skipping V"+str(id))
continue
video.id = extract_version(filename)
video.name = "UNUSUAL MEMES COMPILATION"
video.created_at = datetime.now()
# Open the file and save it to the Video object
with open(os.path.join(folder_path, filename), 'rb') as f:
video.file.save(filename, f)
filename = filename[:-3] + "webp"
filepath = os.path.join(folder_path, filename)
if os.path.exists(filepath):
with open(filepath, 'rb') as f:
video.thumbnail.save(filename, f)
# Save the Video object to the database
video.save()
print('Done!')