【问题标题】:Checking if a list of YouTube videos still online with Python使用 Python 检查 YouTube 视频列表是否仍然在线
【发布时间】:2021-07-16 03:58:39
【问题描述】:

我正在尝试检查 4 个 YouTube 视频的列表是否仍然在线。我知道其中一个处于离线状态,有时我想看看是否对其他人采取了任何行动。通过网络搜索并使用一些编码(我是初学者)我做了这个:

import requests
from pprint import pprint

videos = ["id1","id2","id3","id4"]

for i in videos:
    id_of_video = i
    your_api_key = 'myapi' 
    url = 'https://www.googleapis.com/youtube/v3/videos?id={}&key={}&part=status'.format(id_of_video, your_api_key)
    url_get = requests.get(url)
    pprint(url_get.json())

代码运行良好,我收到类似这样的信息(我只复制了两个结果):

{'etag': 'somenumbersandcharactershere',
 'items': [{'etag': 'somenumbersandcharactershere',
            'id': 'videoid',
            'kind': 'youtube#video',
            'status': {'embeddable': True,
                       'license': 'youtube',
                       'madeForKids': False,
                       'privacyStatus': 'public',
                       'publicStatsViewable': True,
                       'uploadStatus': 'processed'}}],
 'kind': 'youtube#videoListResponse',
 'pageInfo': {'resultsPerPage': 1, 'totalResults': 1}}

{'etag': 'somenumbersandcharactershere',
 'items': [],
 'kind': 'youtube#videoListResponse',
 'pageInfo': {'resultsPerPage': 0, 'totalResults': 0}}

其中一个在线,另一个不在线。太好了。

但我想创建一个代码,不时为我检查它并说:“此视频 ID 不再可用”。但我不知道怎么做。

我现在的主要任务是为每个视频 ID 打印一条消息!所以,假设:“ID1 在线”和“ID2 离线”。

我考虑过创建一个 if 语句来检查“url_get.json()”标签的结果,但它只保存最后一个视频 ID...

如果我跑步

url_get.json()

我只得到列表中的最后一个 ID。

【问题讨论】:

  • 那么,你想print一个'message',只有ifyour_json['pageInfo']['totalResult']的值是== 0?
  • 您可以为此创建一个 cron 作业或创建一个机器人来为您执行此操作
  • @Grismar 是的。实际上,我想为每个视频 ID 打印一条消息!所以,假设:“ID1 在线”和“ID2 离线”。

标签: python python-3.x


【解决方案1】:

您可以为此使用字典。首先,我将值设置为None。然后,在遍历字典时,我将值设置为返回的 JSON。在程序结束时,我打印IDx is offline or online。不过,您可以对字典中的数据做任何您想做的事情。

import requests
from pprint import pprint

videos = {"id1": None, "id2": None, "id3": None, "id4": None}
for i in videos:
    id_of_video = i
    your_api_key = 'myapi' 
    url = 'https://www.googleapis.com/youtube/v3/videos?id={}&key={}&part=status'.format(id_of_video, your_api_key)
    url_get = requests.get(url)
    pprint(url_get.json())
    videos[i] = url_get.json()

for video in videos:
    if videos[video]["pageInfo"]["resultsPerPage"] == 0:
        print(f"{video.upper()} is offline")
    else:
        print(f"{video.upper()} is online")

【讨论】:

  • @interferemadly,如果这回答了您的问题,请点击复选标记
  • 试过了,但收到了这个错误:TypeError Traceback (most recent call last) <ipython-input-50-3e2290602fff> in <module> 12 13 for video in videos: ---> 14 if video[videos]["pageInfo"]["resultsPerPage"] == 0: 15 print(f"{video.upper()} is offline") 16 else: TypeError: string indices must be integers
  • @interferemadly 已修复 :)
  • 嘿伙计,我不知道我是否遗漏了什么——正如我所说,我是一个初学者。仍然出现错误:TypeError Traceback (most recent call last) <ipython-input-54-4c7fce7b6a05> in <module> 11 12 for video in videos: ---> 13 if videos[video]["pageInfo"]["resultsPerPage"] == 0: 14 print(f"{video.upper()} is offline") 15 else: TypeError: 'Response' object is not subscriptable
  • 现在应该修好了,忘了转成json了
【解决方案2】:

因此,您想编写一个定期 ping Youtube 的程序。实现它的最简单方法是

from time import sleep

delay : int = 20

while True:
# ping YT here
  sleep(delay)

这将每 20 秒运行一次。 这里有很多问题。

  • 它永远运行
  • 停止程序的唯一方法是单击 Ctrl+C
  • 也不会正好是 20 秒

但一开始它会成功的。 我发现了很好的问题(线程???)here at stackOverflow 答案建议使用一些更高级的“事件循环”概念。 如果有时间,绝对值得一试。

【讨论】:

  • 如果服务器出现故障或系统关闭,那么这将不起作用,程序需要重新运行。最好为这个工作创建一个 cron 工作
  • 谢谢,这是问题的一部分。但我的主要问题是:如何检查每个 ID 并为每个 ID 打印一条消息!所以,假设:“ID1 在线”和“ID2 离线”。
  • @CartesianCoordianateSystem,请在回答前完整阅读问题
猜你喜欢
  • 2021-10-19
  • 2015-10-08
  • 2023-03-13
  • 2016-05-21
  • 2016-11-18
  • 2012-11-10
  • 1970-01-01
  • 1970-01-01
  • 2015-08-10
相关资源
最近更新 更多