如何获取频道最新视频上传的日期?

2024-06-16 16:52:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道GoogleDeveloper最新视频上传的日期。问题是我不知道如何抓住它。你知道吗

我正在尝试创建一个程序,每当GoogleDeveloper的频道上传一个视频时,一个连在我pi3上的实验板上的LED灯就会亮起来。我试着让程序检查他们最新视频的上传日期是否有变化。你知道吗

这是我目前掌握的密码

import os

import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors

scopes = ["https://www.googleapis.com/auth/youtube.readonly"]

def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "/home/pi/Documents/client_secret_952042969529-4pdbg60vq9f0lnmsl7cahah9mrrts1pa.apps.googleusercontent.com.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.playlistItems().list(
        part="snippet",
        maxResults=1,
        playlistId="UU_x5XG1OV2P6uZZ5FSM9Ttw"
    )
    response = request.execute()

    print(response)
    print(response.items);

if __name__ == "__main__":
    main()

我想如果我把响应.项目“我可以用这种方式获取日期,但它只是打印出来<built-in method items of dict object at 0x75a14ed0>


Tags: nameimportclientauthapi视频youtubemain
2条回答

第一步:获取该频道的上传播放列表id

通过带有contentDetails部分的https://developers.google.com/youtube/v3/docs/channels/list。你知道吗

第二步:在播放列表中获取视频

通过https://developers.google.com/youtube/v3/docs/playlistItems/listsnippet部分。你知道吗

获取第一个项目的视频id。你知道吗

第三步:获取视频信息,包括发布日期

通过带有snippet部分的https://developers.google.com/youtube/v3/docs/videos/list。你知道吗

我的答案不取决于代码,所以你得花点时间才能找到答案。但要知道这就是我的生产系统的工作原理。你知道吗

您要查找的video ID位于response.items[0].snippet.resourceId.videoId,而published date time位于response.items[0].snippet.publishedAt。您的代码还应该检查response.items是否为空(出于某些原因)。你知道吗


另一方面,重申我之前的一个answer,我建议您注意这样一个事实,查询频道的上传列表的PlaylistItems endpoint会生成一个items列表,该列表按上传日期排序(最新的优先)。但是items本身包含附加的publishedAtdatetime属性。你知道吗

publishedAt (datetime)

The date and time that the video was published. Note that this time might be different than the time that the video was uploaded. For example, if a video is uploaded as a private video and then made public at a later time, this property will specify the time that the video was made public.

如果您想获得某个频道的最新发布的视频,您应该查询Search endpoint,而是向它传递适当的参数:channelId=...maxResults=1order=date。你知道吗


更新:根据thisresponse.items[...].snippet.publishedAt应该访问为:

for item in response['items']:
    publishedAt = item['snippet']['publishedAt']
    # use publishedAt ...

相关问题 更多 >