Files
python_scripts/helper_files/import_vod.py
T

116 lines
3.5 KiB
Python

#!/usr/bin/env python3
import os
import json
import asyncio
from pathlib import Path
import package.database as database
import package.twitch.video_info as video_info
DB = None
def format_to_hms(total_seconds):
# Calculate hours, minutes, and seconds
hours, remainder = divmod(int(total_seconds), 3600)
minutes, seconds = divmod(remainder, 60)
# Return formatted string: e.g., 4h27m12s
return f"{hours}h{minutes}m{seconds}s"
def read_json(file_path: str):
try:
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
# Extracting streamer info
streamer_name = data.get("streamer", {}).get("name")
# Extracting video core details
video_data = data.get("video", {})
video_title = video_data.get("title")
video_id = video_data.get("id")
video_created_at = video_data.get("created_at")
video_length_seconds = video_data.get("length")
video_views = video_data.get("viewCount")
video_game = video_data.get("game")
# Printing variables to verify
print(f"Streamer: {streamer_name}")
print(f"Title: {video_title}")
print(f"Video ID: {video_id}")
print(f"Created At: {video_created_at}")
print(f"Duration: {video_length_seconds} seconds")
print(f"Views: {video_views}")
print(f"Game: {video_game}")
video_length = format_to_hms(video_length_seconds)
game_id = asyncio.run(video_info.get_game_id_by_name(video_game))
DB.insert_video_record(
video_id,
video_title,
video_created_at,
int(video_views),
video_length,
"",
"",
game_id,
video_game,
"",
"",
False
)
id = video_id
source = Path(f"download/saveold/{id}")
destination = Path(f"download/videos/{id}")
if source.exists() and source.is_dir():
source.rename(destination)
print("====================================================")
print(f"🚀 VOD to moved: {video_title}")
print("====================================================")
except FileNotFoundError:
print(f"Error: The file at {file_path} was not found.")
except json.JSONDecodeError:
print(
"Error: Failed to parse JSON. Make sure trailing cut-off text is removed."
)
def main():
global DB
DB = database.Database()
# Set the path of the directory you want to walk through
# Use "." for the current folder, or a full path like "C:/Users/Name/Documents"
root_directory = "./download/saveold"
# os.walk yields a 3-item tuple for every single folder it finds
for current_folder, subfolders, files in os.walk(root_directory):
print(f"\n📂 Current Folder: {current_folder}")
# List all subfolders inside the current folder
if subfolders:
print(f" 📁 Subfolders found: {', '.join(subfolders)}")
# List all files inside the current folder
for file_name in files:
# Construct the full absolute or relative path to the file
full_file_path = os.path.join(current_folder, file_name)
print(f" 📄 File: {file_name} ({full_file_path})")
if file_name.endswith(".json"):
read_json(full_file_path)
if __name__ == "__main__":
main()