首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Copilot和OpenAI Python脚本调试

Copilot和OpenAI Python脚本调试
EN

Stack Overflow用户
提问于 2022-09-07 06:01:54
回答 1查看 100关注 0票数 1

更新:

在当前的帖子中,我确实在函数中包含了ids=ids,但是当将其更改为id=ids时,我会得到相同的错误。也许通过查看完整的代码,它可能会使人们更加清楚为什么会发生这种情况。

下面是完整的代码:

代码语言:javascript
复制
#Write a script that writes a csv file with a list of the top most viewed YouTube videos.
#The script should take a YouTube API key as an argument and use it to query the YouTube Data API for the topmost viewed videos.
#The script should then write the video title, video view count, and video ID to a csv file.
#The script should also print the total number of views to a file.

import csv
import os
import argparse
from googleapiclient.discovery import build

# Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
# tab of
#   https://cloud.google.com/console
# Please ensure that you have enabled the YouTube Data API for your project.
DEVELOPER_KEY = "---hidden---"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

def youtube_search(options):
  youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    developerKey=DEVELOPER_KEY)

  # Call the search.list method to retrieve results matching the specified
  # query term.
  search_response = youtube.search().list(
    q=options.q,
    part="id,snippet",
    maxResults=options.max_results
  ).execute()

  ids = [item["id"]["videoId"] for item in search_response["items"]]

  search_response = youtube.videos().list(
    part="statistics",
    ids=ids
  ).execute()

  videos_with_view_counts = [[item["id"], item["statistics"]["viewCount"]] for item in search_response["items"]]

  return videos_with_view_counts

  # Add each result to the appropriate list, and then display the lists of
  # matching videos, channels, and playlists.
  for search_result in search_response.get("items", []):
    if search_result["id"]["kind"] == "youtube#video":
      videos.append(search_result)

  return videos

def main():
  parser = argparse.ArgumentParser(description='Search on YouTube')
  parser.add_argument("--q", help="Search term", default="Google")
  parser.add_argument("--max-results", help="Max results", default=25)
  args = parser.parse_args()

  try:
    os.remove('top_videos.csv')
  except OSError:
    pass

  with open('top_videos.csv', 'w') as csvfile:
    fieldnames = ['title', 'viewCount', 'videoId']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    for video in youtube_search(args):  # video is a dictionary
      print("Title: %s" % video['snippet']['title'])  # Print video title
      print("View Count: %s" % video['statistics']['viewCount'])  # video['statistics']['viewCount']
      print("Video ID: %s" % video['id']['videoId'])  # videoId
      print("\n")  # blank line

      writer.writerow({'title': video['snippet']['title'], 'viewCount': video['statistics']['viewCount'], 'videoId': video['id']['videoId']})

if __name__ == "__main__":
  main()

我得到的错误((您的代码+Copilot的代码)如下:

代码语言:javascript
复制
Traceback (most recent call last):
  File "c:\Users\brian\Documents\GitHub\scripts\top-youtube.py", line 75, in <module>
    main()
  File "c:\Users\brian\Documents\GitHub\scripts\top-youtube.py", line 66, in main
    for video in youtube_search(args):  # video is a dictionary
  File "c:\Users\brian\Documents\GitHub\scripts\top-youtube.py", line 31, in youtube_search
    ids = [item["id"]["videoId"] for item in search_response["items"]]
  File "c:\Users\brian\Documents\GitHub\scripts\top-youtube.py", line 31, in <listcomp>
    ids = [item["id"]["videoId"] for item in search_response["items"]]
KeyError: 'videoId'

我怀疑代码的其他部分是导致问题的原因,而不是函数本身。我不反对从脚本中删除任何必要的东西,让它搜索所有的YouTube,并返回一个列表,上面的1000个视频按按视图、喜好、subs顺序加权的升序排列。如果这确实是YouTube为内容创作者计算报酬的算法的话。我的想法是利用这些信息开始创建可以赚取被动收入的渠道,不管这些渠道有多小,然后通过使用Python和API将它们导入到我的交换帐户中,然后由Python交易机器人进行交易。

任何来自YouTube频道的东西总比没有好。我也知道这样一个事实:你可以从网站和社交媒体账户向你的YouTube视频/频道输送流量。不要指望从YouTube中发财,而是希望创造一种稳定的货币,然后再进行交易。预计YouTube不会在这里完成大部分的繁重工作,只是作为可能的许多来源之一。所以,我的想法是最初使用这个脚本来获得关于频道和视频、内容和成功率的想法。如果你知道更好的方法,请告诉我。

完整的脚本是这里

EN

回答 1

Stack Overflow用户

发布于 2022-09-07 08:37:08

代码语言:javascript
复制
2. Create a new project.

但是,当我登录到dev控制台时,创建一个新项目没有任何意义。

您似乎错过了Select a project左上角的Google开发者控制台按钮。

更新:

要使用从YouTube Data v3 搜索:列表端点检索到的ids,将它们传递给录影带:名单,以便获取它们的统计信息(特别是它们的viewCount),可以使用而不是使用代码片段:

代码语言:javascript
复制
  # Call the search.list method to retrieve results matching the specified
  # query term.
  search_response = youtube.search().list(
    q=options.q,
    part="id,snippet",
    maxResults=options.max_results
  ).execute()

  ids = [item["id"]["videoId"] for item in search_response["items"]]

  search_response = youtube.videos().list(
    part="statistics",
    ids=ids
  ).execute()

  videos_with_view_counts = [[item["id"], item["statistics"]["viewCount"]] for item in search_response["items"]]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73630747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档