有问题与Twitch API



我正试图得到聊天,然后把它变成每个时间段的一些chatters,然后把前10个时刻打印成一个文本文件,这样我就可以更容易地编辑,但是我似乎不能让它工作,这似乎是错误的客户端id,我不完全理解。

import requests
import logging
import json
import tkinter as tk
import tkinter.messagebox as messagebox
import functools
# Function to get the chat logs for a given video
def get_chat_logs(client_id, video_id):
# Make a GET request to the Twitch API to retrieve the chat logs for the given video
url = f"https://api.twitch.tv/v5/videos/{video_id}/comments"
headers = { "Client-ID": client_id }
response = requests.get(url, headers=headers)
# Print the URL and status code for debugging purposes
print(f"URL: {url}")
print(f"Status code: {response.status_code}")
# If the request was successful, return the chat logs
if response.status_code == 200:
return response.json()
# If the request was unsuccessful, display an error message and return an empty list
else:
messagebox.showerror("Error", "An error occurred while retrieving the chat logs. Please check your client ID and video ID and try again.")
return []

# Function to parse the chat logs and extract the chat volume for each moment in the video
def parse_chat_logs(chat_logs):
chat_volume_by_moment = {}
# Iterate through each chat log and extract the timestamp and number of messages
for chat_log in chat_logs:
timestamp = chat_log["content_offset_seconds"]
messages = chat_log["messages"]
# If this moment has not been seen before, initialize the chat volume to 0
if timestamp not in chat_volume_by_moment:
chat_volume_by_moment[timestamp] = 0
# Add the number of messages for this moment to the chat volume
chat_volume_by_moment[timestamp] += len(messages)
return chat_volume_by_moment
# Function to identify the top 10 moments with the highest chat volume
def find_top_moments(chat_volume_by_moment):
# Sort the moments by chat volume in descending order
sorted_moments = sorted(chat_volume_by_moment.items(), key=lambda x: x[1], reverse=True)
# Return the top 10 moments
return sorted_moments[:10]
# Function to add textboxes and a submit button to the UI
def add_ui_elements(main):
# Create the main window
window = tk.Tk()
window.title("Twitch Chat Logs")
# Create a label for the client ID textbox
client_id_label = tk.Label(text="Enter your client ID:")
client_id_label.pack()
# Create a textbox for the client ID
client_id_textbox = tk.Entry()
client_id_textbox.pack()
# Create a label for the video ID textbox
video_id_label = tk.Label(text="Enter the video ID:")
video_id_label.pack()
# Create a textbox for the video ID
video_id_textbox = tk.Entry()
video_id_textbox.pack()
# Create a submit button
submit_button = tk.Button(text="Submit")
submit_button.pack()
# Return the client ID and video ID textboxes and submit button
return client_id_textbox, video_id_textbox, submit_button, window
# Function to create a submit button
def create_submit_button(main):
# Create a submit button
submit_button = tk.Button(text="Submit")
submit_button.pack()
return submit_button
# Main function
def main(client_id_textbox, video_id_textbox, submit_button):
# Get the client ID and video ID from the UI
client_id = client_id_textbox.get()
video_id = video_id_textbox.get()
# Check if the user has entered a client ID and video ID
if not client_id or not video_id:
messagebox.showerror("Error", "Please enter a valid client ID and video ID.")
return
# If a client ID and video ID have been entered, make the API request
chat_logs = get_chat_logs(client_id, video_id)
# Parse the chat logs and extract the chat volume for each moment in the video
chat_volume_by_moment = parse_chat_logs(chat_logs)
# Find the top 10 moments with the highest chat volume
top_moments = find_top_moments(chat_volume_by_moment)
# Write the top moments to a file
with open("top_moments.txt", "w") as f:
for moment in top_moments:
f.write(f"{moment[0]}: {moment[1]}n")
messagebox.showinfo("Success", "The top moments have been written to top_moments.txt.")
# Add the UI elements and get the client ID and video ID textboxes and submit button
client_id_textbox, video_id_textbox, submit_button, window = add_ui_elements(main)
# Bind the main function to the submit button's "Button-1" event
submit_button.bind("<Button-1>", functools.partial(main, client_id_textbox, video_id_textbox))
# Run the main loop for the GUI
window.mainloop()
# Run the main function
if __name__ == "__main__":
client_id_textbox, video_id_textbox = add_ui_elements(main)
main(client_id_textbox, video_id_textbox, submit_button)

在过去的几个小时里,我尝试了很多东西,以确保代码是正确的,并且它正在进行API调用,但它不是。

您遇到的问题是,您正在尝试使用未记录且从未打算供第三方使用的端点https://api.twitch.tv/v5/videos/{video_id}/comments,因此它可以并且将在任何时候破坏/更改而没有警告。

正如您刚刚经历的那样,端点已被更改为不再支持第三方使用,例如:

唯一支持的方式来获得有关聊天的信息是连接到Twitch聊天,如文档https://dev.twitch.tv/docs/irc,并捕获聊天数据,因为它发生在实时。历史聊天数据,如视频,不支持第三方端点。

最新更新