import os import re import sys import fnmatch 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 = './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 def find_image_file(name_pattern, folder): pattern = re.compile('.*{} .*\.(jpg|png|webp)'.format(name_pattern), re.IGNORECASE) for root, dirs, files in os.walk(folder): for filename in files: if pattern.match(filename): return os.path.join(root, filename) return None files = os.listdir(folder_path) for filename in files: 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) index = [idx for idx, s in enumerate(files) if bool(re.search('V'+str(video.id)+'( |_)', s)) and '.webp' in s] if not index: continue index = index[0] with open(os.path.join(folder_path, files[index]), 'rb') as f: video.thumbnail.save(files[index], f) # Save the Video object to the database video.save() print('Done!')