192 lines
6.9 KiB
Python
192 lines
6.9 KiB
Python
#!/usr/bin/env python3
|
|
import os
|
|
import requests
|
|
import sqlite3
|
|
import subprocess
|
|
|
|
CHANNEL_NAME = 'teampgp' # Replace with the streamer's username
|
|
|
|
def download_clips():
|
|
undownload_items = get_undownloaded_records()
|
|
|
|
print("Download Clips...")
|
|
|
|
# Updated unpack loop to include clip_by
|
|
for record_id, record_date, title, gamename, clip_by, downloaded in undownload_items:
|
|
print(f"Slug: {record_id} | Date: {record_date} | Game: {gamename} | By: {clip_by} | Title: {title}")
|
|
|
|
# Ensure output directory exists before running CLI tools
|
|
os.makedirs(f"save/clips/{record_id}", exist_ok=True)
|
|
|
|
# Uses the 'clipdownload' command
|
|
output = run_linux_command(f"TwitchDownloaderCLI clipdownload --id {record_id} -o save/clips/{record_id}/{record_id}.mp4")
|
|
|
|
if output["success"] is True:
|
|
print(f"Slug: {record_id} | Date: {record_date} | Game: {gamename} | By: {clip_by} | Title: {title} | was successful")
|
|
mark_as_downloaded(record_id)
|
|
else:
|
|
print(f"Slug: {record_id} | Date: {record_date} | Game: {gamename} | By: {clip_by} | Title: {title} | was Failed")
|
|
if not output["success"]: print(f"-> Clip Download Error: {output['stderr']}")
|
|
|
|
def run_linux_command(command: str):
|
|
"""Executes a Linux command, waits for completion, and returns output."""
|
|
try:
|
|
result = subprocess.run(
|
|
command, shell=True, check=True, capture_output=True, text=True
|
|
)
|
|
return {"success": True, "stdout": result.stdout, "stderr": result.stderr}
|
|
except subprocess.CalledProcessError as e:
|
|
return {"success": False, "stdout": e.stdout, "stderr": e.stderr}
|
|
|
|
def mark_as_downloaded(record_id: str):
|
|
"""Updates the downloaded status to True (1) for a specific clip slug."""
|
|
conn = sqlite3.connect("database.db")
|
|
cursor = conn.cursor()
|
|
cursor.execute(
|
|
"UPDATE clips SET downloaded = 1 WHERE id = ?",
|
|
(record_id,)
|
|
)
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
def get_undownloaded_records():
|
|
"""Retrieves all rows where downloaded status is False (0)."""
|
|
conn = sqlite3.connect("database.db")
|
|
cursor = conn.cursor()
|
|
# Added clip_by to the SELECT fields
|
|
cursor.execute(
|
|
"SELECT id, date, title, gamename, clip_by, downloaded, uploaded FROM clips WHERE downloaded = 0"
|
|
)
|
|
records = cursor.fetchall()
|
|
conn.close()
|
|
return records
|
|
|
|
def insert_record(record_id: str, record_date: str, title: str, gamename: str, clip_by: str):
|
|
"""Inserts a clip record including its text slug string and creator username."""
|
|
conn = sqlite3.connect("database.db")
|
|
cursor = conn.cursor()
|
|
|
|
# Added clip_by TEXT NOT NULL field to table definition
|
|
cursor.execute(
|
|
"""
|
|
CREATE TABLE IF NOT EXISTS clips (
|
|
id TEXT PRIMARY KEY,
|
|
date TEXT NOT NULL,
|
|
title TEXT NOT NULL,
|
|
gamename TEXT NOT NULL,
|
|
clip_by TEXT NOT NULL,
|
|
downloaded INTEGER NOT NULL,
|
|
uploaded INTEGER NOT NULL
|
|
)
|
|
"""
|
|
)
|
|
|
|
# Handled database migration gracefully if running script on an older DB missing the column
|
|
try:
|
|
cursor.execute(
|
|
"INSERT OR IGNORE INTO clips (id, date, title, gamename, clip_by, downloaded, uploaded) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
(str(record_id), str(record_date), title, gamename, clip_by, 0, 0),
|
|
)
|
|
except sqlite3.OperationalError as e:
|
|
if "has no column named clip_by" in str(e):
|
|
print("Upgrading database schema to support creator names...")
|
|
cursor.execute("ALTER TABLE clips ADD COLUMN clip_by TEXT DEFAULT 'Unknown'")
|
|
cursor.execute(
|
|
"INSERT OR IGNORE INTO clips (id, date, title, gamename, clip_by, downloaded, uploaded) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
(str(record_id), str(record_date), title, gamename, clip_by, 0, 0),
|
|
)
|
|
else:
|
|
raise e
|
|
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
def get_clip_slugs(channel_name):
|
|
session = requests.Session()
|
|
url = "https://twitch.tv"
|
|
|
|
session.headers = {
|
|
"Client-ID": "kimne78kx3ncx6brgo4mv6wki5h1ko",
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
|
"Content-Type": "text/plain"
|
|
}
|
|
|
|
# Query updated to fetch the curator's login (the user who clipped it)
|
|
query_string = """
|
|
query GetChannelClips($login: String!, $limit: Int!) {
|
|
user(login: $login) {
|
|
clips(first: $limit, criteria: { period: ALL_TIME, sort: VIEWS_DESC }) {
|
|
edges {
|
|
node {
|
|
slug
|
|
title
|
|
createdAt
|
|
game {
|
|
displayName
|
|
}
|
|
curator {
|
|
login
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
"""
|
|
|
|
payload = [{
|
|
"operationName": "GetChannelClips",
|
|
"query": query_string,
|
|
"variables": {
|
|
"login": channel_name.lower(),
|
|
"limit": 30
|
|
}
|
|
}]
|
|
|
|
try:
|
|
req = requests.Request('POST', url, json=payload)
|
|
prepped = session.prepare_request(req)
|
|
response = session.send(prepped)
|
|
response.raise_for_status()
|
|
|
|
data = response.json()
|
|
result = data[0] if isinstance(data, list) else data
|
|
|
|
if "errors" in result:
|
|
print(f"Twitch GraphQL Error: {result['errors']}")
|
|
return []
|
|
|
|
user_data = result['data']['user']
|
|
if not user_data:
|
|
print(f"Channel '{channel_name}' not found.")
|
|
return []
|
|
|
|
edges = user_data['clips']['edges']
|
|
clip_slugs = []
|
|
|
|
print(f"--- Top Clips for {channel_name} ---")
|
|
for edge in edges:
|
|
node = edge['node']
|
|
game_info = node.get('game')
|
|
game_name = game_info.get('displayName') if game_info else "Unknown/No Category"
|
|
|
|
# Safe extraction in case the curator account was deleted/missing
|
|
curator_info = node.get('curator')
|
|
clip_by = curator_info.get('login') if curator_info else "Unknown Creator"
|
|
|
|
print(f"Slug: {node['slug']} | Date: {node['createdAt']} | Game: {game_name} | By: {clip_by} | Title: {node['title']}")
|
|
|
|
# Passing clip_by to database writer block
|
|
insert_record(node['slug'], node['createdAt'], node['title'], game_name, clip_by)
|
|
clip_slugs.append(node['slug'])
|
|
|
|
return clip_slugs
|
|
|
|
except Exception as e:
|
|
print(f"An unexpected error occurred: {e}")
|
|
return []
|
|
|
|
if __name__ == "__main__":
|
|
get_clip_slugs(CHANNEL_NAME)
|
|
download_clips()
|