chat edits

This commit is contained in:
t0is 2025-02-21 15:24:11 +01:00
parent 109514f14f
commit 94a4084236

70
main.py
View File

@ -116,33 +116,29 @@ def search_transcription(result, keywords):
break # Stop checking further keywords for this segment break # Stop checking further keywords for this segment
return matches return matches
def scrape_chat_log(vod_id, output_filename): def scrape_chat_log(vod_id, output_filename):
"""
Uses TwitchDownloaderCLI to download the chat log for a given VOD.
The chat log is saved in JSON format to output_filename.
"""
if os.path.exists(output_filename): if os.path.exists(output_filename):
print(f"{output_filename} already exists. Skipping chat log scrape.") print(f"{output_filename} already exists. Skipping chat log scrape.")
return return
headers = {
"Client-ID": TWITCH_CLIENT_ID, # Build the TwitchDownloaderCLI command.
"Accept": "application/vnd.twitchtv.v5+json" # The command downloads the chat log in JSON format for the specified VOD.
} command = [
base_url = f"https://api.twitch.tv/v5/videos/{vod_id}/comments" "TwitchDownloaderCLI", "chatdownload",
comments = [] "--id", vod_id,
cursor = None "--output", output_filename
while True: ]
params = {}
if cursor: try:
params["cursor"] = cursor subprocess.run(command, check=True)
response = requests.get(base_url, headers=headers, params=params) print(f"Chat log saved to {output_filename}")
if response.status_code != 200: except subprocess.CalledProcessError as e:
print(f"Error fetching chat comments for VOD {vod_id}: {response.text}") print(f"Error downloading chat log for VOD {vod_id}: {e}")
break
data = response.json()
comments.extend(data.get("comments", []))
cursor = data.get("_next")
if not cursor:
break
with open(output_filename, "w", encoding="utf-8") as f:
json.dump(comments, f, ensure_ascii=False, indent=4)
print(f"Chat log saved to {output_filename}")
def create_clip_from_vod(video_file, match_start, vod_id): def create_clip_from_vod(video_file, match_start, vod_id):
clip_start = max(match_start - 15, 0) clip_start = max(match_start - 15, 0)
@ -164,19 +160,30 @@ def create_clip_from_vod(video_file, match_start, vod_id):
return clip_filename return clip_filename
def find_comments_by_keywords(chat_log, keywords): def find_comments_by_keywords(chat_log, keywords):
"""
Searches the chat log for any comments containing one of the given keywords.
The chat log can be either:
- a raw list of comment objects, or
- an object with a "comments" key containing the list.
Each comment is expected to have:
- a "message" key with the comment text (as a string)
- an "offset" key (or fallback to "content_offset_seconds") for the timestamp.
Returns a list of matching comment objects.
"""
matching_comments = [] matching_comments = []
# Ensure chat_log is a list of dictionaries. # If the chat log is wrapped in an object, extract the list.
if isinstance(chat_log, dict) and "comments" in chat_log:
chat_log = chat_log["comments"]
for comment in chat_log: for comment in chat_log:
if not isinstance(comment, dict): if not isinstance(comment, dict):
continue continue
message = comment.get("message", {}) # Get the message text; TwitchDownloaderCLI outputs it as a string in "message"
if not isinstance(message, dict): message_text = comment['message']['body'].lower()
continue
text = message.get("body", "").lower()
for keyword in keywords: for keyword in keywords:
if keyword.lower() in text: if keyword.lower() in message_text:
matching_comments.append(comment) matching_comments.append(comment)
break break # No need to check further keywords for this comment.
return matching_comments return matching_comments
def create_clip_from_comment_timestamp(video_file, comment_timestamp, vod_id): def create_clip_from_comment_timestamp(video_file, comment_timestamp, vod_id):
@ -267,7 +274,8 @@ def main():
comment_matches = find_comments_by_keywords(chat_log, SEARCH_KEYWORDS) comment_matches = find_comments_by_keywords(chat_log, SEARCH_KEYWORDS)
if comment_matches: if comment_matches:
for comment in comment_matches: for comment in comment_matches:
timestamp = comment.get("content_offset_seconds") # Try to get the timestamp from the "offset" field (or fallback to "content_offset_seconds")
timestamp = comment["content_offset_seconds"]
print(f"Found a matching comment at {timestamp} seconds.") print(f"Found a matching comment at {timestamp} seconds.")
create_clip_from_comment_timestamp(video_filename, timestamp, vod_id) create_clip_from_comment_timestamp(video_filename, timestamp, vod_id)
else: else: