Files
2026-09-21 18:51:13 +00:00

71 lines
2.7 KiB
Python

#!/usr/bin/env python3
import os
import json
import sys
# Finds the absolute path to 'project_directory' and adds it to Python's search path
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../"))
sys.path.append(PROJECT_ROOT)
from package.config import DB as DB
from package.database import Chapter
def get_chapters(json_path):
"""Loads the JSON file and returns all chapters."""
with open(json_path, 'r', encoding='utf-8') as f:
data = json.load(f)
# The chapters are under video -> chapters
video = data.get('video', {})
chapters = video.get('chapters', [])
if not chapters:
print("No chapters found.")
return []
print(f"Found {len(chapters)} chapter(s) in video '{video.get('title')}':\n")
for i, chapter in enumerate(chapters, 1):
start_sec = chapter.get('startMilliseconds', 0) / 1000
length_sec = chapter.get('lengthMilliseconds', 0) / 1000
print(f"{i}. {chapter.get('description', 'No Title')}")
print(f" - Type: {chapter.get('type')}")
print(f" - Start: {start_sec}s ({chapter.get('startMilliseconds')}ms)")
print(f" - Length: {length_sec}s ({chapter.get('lengthMilliseconds')}ms)")
print(f" - Game: {chapter.get('gameDisplayName')} (ID: {chapter.get('gameId')})")
print()
return chapters
def main():
downloads = DB.get_downloaded()
for download in downloads:
json_path = f"download/clips/{download.id}/{download.id}_chat.json" if download.clip_is else f"download/videos/{download.id}/{download.id}_chat.json"
if os.path.exists(json_path):
chapters = get_chapters(json_path)
for chapter in chapters:
chapter_record = Chapter(
twitch_video_id=download.id,
start_milliseconds=chapter.get('startMilliseconds'),
length_milliseconds=chapter.get('lengthMilliseconds'),
description=chapter.get('description'),
type=chapter.get('type'),
game_display_name=chapter.get('gameDisplayName'),
game_id=chapter.get('gameId')
)
DB.insert(chapter_record)
else:
print(f"JSON file not found for video {download.id}: {json_path}")
if __name__ == "__main__":
main()
# Usage: python get_chapters.py your_file.json
#path = sys.argv[1] if len(sys.argv) > 1 else "/home/richard/python_scripts/download/videos/2873107835/2873107835_chat.json"
#try:
# get_chapters(path)
#except FileNotFoundError:
# print(f"Error: File not found -> {path}")
#except json.JSONDecodeError:
# print(f"Error: File is not valid JSON -> {path}")