python-3.x 使用Twitch API时遇到问题

w1e3prcc  于 2022-12-30  发布在  Python
关注(0)|答案(1)|浏览(136)

我试图得到聊天,然后把它变成每个时间段的聊天者数量,然后采取前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调用,但它没有。

igetnqfo

igetnqfo1#

您遇到的问题是,您试图使用一个端点https://api.twitch.tv/v5/videos/{video_id}/comments,这是没有记录的,从来没有打算为第三方使用,因此它可以而且将打破/更改在任何时候,没有警告。
正如您刚才所体验到的,该端点已更改为不再支持第三方使用,例如:
获取聊天信息的唯一支持方式是连接到Twitch Chat(如https://dev.twitch.tv/docs/irc所述),并真实的捕获聊天数据。历史聊天数据(如来自VoD的数据)不支持第三方终结点。

相关问题