update vod downloader to use the new database.py file
This commit is contained in:
+47
-88
@@ -2,55 +2,15 @@
|
||||
import requests
|
||||
|
||||
import sqlite3
|
||||
from datetime import date as datetime_date
|
||||
|
||||
import subprocess
|
||||
|
||||
from database import Database
|
||||
|
||||
import uploader
|
||||
from uploader import CategoryId
|
||||
|
||||
CHANNEL_NAME = 'teampgp' # Replace with the streamer's username
|
||||
|
||||
CONN = sqlite3.connect("database.db")
|
||||
CURSOR = CONN.cursor()
|
||||
|
||||
def create_database():
|
||||
# Creates table safely using multi-line string
|
||||
CURSOR.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS vods (
|
||||
id INTEGER PRIMARY KEY,
|
||||
date TEXT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
gamename TEXT NOT NULL,
|
||||
downloaded INTEGER NOT NULL,
|
||||
uploaded_yt INTEGER NOT NULL,
|
||||
chat_upload_yt INTEGER NOT NULL
|
||||
)
|
||||
"""
|
||||
)
|
||||
|
||||
CONN.commit()
|
||||
|
||||
def close_database():
|
||||
"""Commit before we close."""
|
||||
CONN.commit()
|
||||
CONN.close()
|
||||
|
||||
def download_vods():
|
||||
"""Find all undownload vods and download them."""
|
||||
undownload_vods = get_undownloaded_vods()
|
||||
|
||||
print("Download VODs...")
|
||||
|
||||
for record_id, record_date, title, game_name, downloaded in undownload_vods:
|
||||
print(f"ID: {record_id} | Date: {record_date} | Game: {game_name} | Title: {title}")
|
||||
output = run_linux_command(f"TwitchDownloaderCLI videodownload --id {record_id} -o save/vod/{record_id}/{record_id}.mp4")
|
||||
output2 = run_linux_command(f"TwitchDownloaderCLI chatdownload --id {record_id} -o save/vod/{record_id}/{record_id}_chat.json -E")
|
||||
if output["success"] is True:
|
||||
print(f"ID: {record_id} | Date: {record_date} | Game: {game_name} | Title: {title} | was successful")
|
||||
mark_as_downloaded(record_id)
|
||||
else:
|
||||
print(f"ID: {record_id} | Date: {record_date} | Game: {game_name} | Title: {title} | was Failed")
|
||||
|
||||
print("Finished Downloading VODs...")
|
||||
DB = Database("vods")
|
||||
|
||||
def run_linux_command(command: str):
|
||||
"""Executes a Linux command, waits for completion, and returns output."""
|
||||
@@ -67,52 +27,50 @@ def run_linux_command(command: str):
|
||||
# Handles errors if the Linux command returns a non-zero exit code
|
||||
return {"success": False, "stdout": e.stdout, "stderr": e.stderr}
|
||||
|
||||
def mark_as_downloaded(record_id: int):
|
||||
"""Updates the downloaded status to True (1) for a specific record ID."""
|
||||
|
||||
# Updates the row matching the specific ID
|
||||
CURSOR.execute(
|
||||
"UPDATE vods SET downloaded = 1 WHERE id = ?",
|
||||
(record_id,)
|
||||
)
|
||||
def download_vods():
|
||||
"""Find all undownload vods and download them."""
|
||||
undownloaded_vods = DB.get_undownloaded()
|
||||
|
||||
CONN.commit()
|
||||
print("Download VODs...")
|
||||
|
||||
def get_unuploaded_clips():
|
||||
"""Retrieves all clip rows remaining to be chat uploaded."""
|
||||
CURSOR.execute(
|
||||
"SELECT id, date, title, gamename, clip_by, view_count, downloaded, uploaded_yt, chat_uploaded_yt FROM vods WHERE downloaded = 1 AND chat_uploaded_yt = 0"
|
||||
)
|
||||
return CURSOR.fetchall()
|
||||
for record_id, record_date, title, game_name, downloaded, uploaded_yt, chat_upload_yt in undownloaded_vods:
|
||||
print(f"ID: {record_id} | Date: {record_date} | Game: {game_name} | Title: {title}")
|
||||
output = run_linux_command(f"TwitchDownloaderCLI videodownload --id {record_id} -o save/vods/{record_id}/{title}.mp4")
|
||||
output2 = run_linux_command(f"TwitchDownloaderCLI chatdownload --id {record_id} -o save/vods/{record_id}/{title}_chat.json -E")
|
||||
if output["success"] is True:
|
||||
print(f"ID: {record_id} | Date: {record_date} | Game: {game_name} | Title: {title} | was successful")
|
||||
DB.mark_as_downloaded(record_id)
|
||||
else:
|
||||
print(f"ID: {record_id} | Date: {record_date} | Game: {game_name} | Title: {title} | was Failed")
|
||||
|
||||
def get_unuploaded_clips():
|
||||
"""Retrieves all clip rows remaining to be uploaded."""
|
||||
CURSOR.execute(
|
||||
"SELECT id, date, title, gamename, clip_by, view_count, downloaded, uploaded_yt FROM vods WHERE downloaded = 1 AND uploaded_yt = 0"
|
||||
)
|
||||
return CURSOR.fetchall()
|
||||
print("Finished Downloading VODs...")
|
||||
|
||||
def get_undownloaded_vods():
|
||||
"""Retrieves all rows where downloaded status is False (0)."""
|
||||
|
||||
# Query filters by 0 because SQLite stores booleans as integers
|
||||
CURSOR.execute(
|
||||
"SELECT id, date, title, gamename, downloaded FROM vods WHERE downloaded = 0"
|
||||
)
|
||||
return CURSOR.fetchall()
|
||||
def upload_vods():
|
||||
"""Loops over the downloaded videos entries and uploaded them to youtube."""
|
||||
print("Uploading Clips...")
|
||||
|
||||
def insert_record(record_id: int, record_date: datetime_date, title: str, gamename: str):
|
||||
"""Inserts a record with ID, date, gamename, and title into a SQLite database."""
|
||||
# Connects to database file (creates it if missing)
|
||||
unuploaded_vods = DB.get_unuploaded()
|
||||
|
||||
# Inserts data using parameterized queries to prevent SQL injection
|
||||
CURSOR.execute(
|
||||
"INSERT OR IGNORE INTO vods (id, date, title, gamename, downloaded, uploaded_yt, chat_uploaded_yt) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
(record_id, str(record_date), title, gamename, False, False, False),
|
||||
)
|
||||
for record_id, record_date, title, game_name, downloaded, uploaded_yt, chat_upload_yt in unuploaded_vods:
|
||||
print(f"ID: {record_id} | Date: {record_date} | Game: {game_name} | Title: {title}")
|
||||
|
||||
# Saves changes and closes the connection
|
||||
CONN.commit()
|
||||
file_path = f"save/vods/{record_id}/{title}.mp4"
|
||||
title = title
|
||||
description = f"Game: {game_name}, on {record_date}, #Shorts"
|
||||
categoryId = CategoryId.GAMING
|
||||
privatcyStatus = 'private'
|
||||
tags = ['gaming', 'TeamPGP', f'{game_name}']
|
||||
|
||||
output = uploader.upload_video(file_path, title, description, categoryId, privatcyStatus, tags)
|
||||
|
||||
if output is True:
|
||||
print(f"ID: {record_id} | Was successfully uploaded.")
|
||||
DB.mark_as_uploaded(record_id)
|
||||
else:
|
||||
print(f"ID: {record_id} | Upload Process failed.")
|
||||
|
||||
def create_chat_vods():
|
||||
pass
|
||||
|
||||
def get_vod_ids_simplified(channel_name: str):
|
||||
session = requests.Session()
|
||||
@@ -190,7 +148,8 @@ def get_vod_ids_simplified(channel_name: str):
|
||||
print(f"ID: {node['id']} | Date: {node['publishedAt']} | Game: {game_name} | Title: {node['title']}")
|
||||
|
||||
# Pass game_name to your database logic
|
||||
insert_record(node['id'], node['publishedAt'], node['title'], game_name)
|
||||
DB.insert_vods_record(node['id'], node['publishedAt'], node['title'], game_name)
|
||||
#insert_record(node['id'], node['publishedAt'], node['title'], game_name)
|
||||
vod_ids.append(node['id'])
|
||||
|
||||
return vod_ids
|
||||
@@ -202,7 +161,7 @@ def get_vod_ids_simplified(channel_name: str):
|
||||
return []
|
||||
|
||||
if __name__ == "__main__":
|
||||
create_database()
|
||||
get_vod_ids_simplified(CHANNEL_NAME)
|
||||
download_vods()
|
||||
close_database()
|
||||
upload_vods()
|
||||
#close_database()
|
||||
Reference in New Issue
Block a user