跳转到主要内容

易于使用的库,用于批量操作访问YouTube数据API v3

项目描述

youtool - 批量访问YouTube数据API v3

Python库(以及未来的命令行界面),用于批量操作爬取YouTube数据API v3和其他相关任务。比替代方案更易于使用 - 您不需要花费时间学习YouTube API及其注意事项。使用此库,您可以获取

  • 从频道URL(抓取)或用户名(API)获取频道ID
  • 频道信息(标题、订阅者等)
  • 频道的播放列表列表
  • 播放列表的视频列表
  • 视频搜索(许多参数)
  • 视频信息(标题、描述、点赞、评论等)
  • 评论
  • 直播聊天,包括超级聊天(使用chat-downloader抓取)
  • 自动转录(使用yt-dlp抓取)

该库将自动

  • 尝试您提供的所有密钥
  • 使用支持的API端点的50个项目的批次
  • 需要时进行分页

安装

pip install youtool

您可能还需要一些额外功能

pip install youtool[livechat]
pip install youtool[transcription]

作为库使用

只需遵循下面的教程/示例,并检查YouTube方法的help()

注意:下面的示例将使用您API密钥配额的135个单位。

from pprint import pprint
from pathlib import Path

from youtool import YouTube

api_keys = ["key1", "key2", ...]  # Create one in Google Cloud Console
yt = YouTube(api_keys, disable_ipv6=True)  # Will try all keys

channel_id_1 = yt.channel_id_from_url("https://youtube.com/c/PythonicCafe/")
print(f"Pythonic Café's channel ID (got from URL): {channel_id_1}")
channel_id_2 = yt.channel_id_from_username("turicas")
print(f"Turicas' channel ID (got from username): {channel_id_2}")

print("Playlists found on Turicas' channel (the \"uploads\" playlist is not here):")
# WARNING: this method won't return the main channel playlist ("uploads").
# If you need it, get channel info using `channels_infos` and the `playlist_id` key (or use the hack in the next
# section), so you can pass it to `playlist_videos`.
for playlist in yt.channel_playlists(channel_id_2):
    # `playlist` is a `dict`
    print(f"Playlist: {playlist}")
    for video in yt.playlist_videos(playlist["id"]):
        # `video` is a `dict`, but this endpoint doesn't provide full video information (use `videos_infos` to get them)
        print(f"  Video: {video}")
    print("-" * 80)

# Hack: replace `UC` with `UU` on channel ID to get main playlist ID ("uploads"):
assert channel_id_1[:2] == "UC"
print("Last 3 uploads for Pythonic Café:")
for index, video in enumerate(yt.playlist_videos("UU" + channel_id_1[2:])):
    # `video` is a `dict`, but this endpoint doesn't provide full video information (use `videos_infos` to get them)
    print(f"  Video: {video}")
    if index == 2:  # First 3 results only
        break
print("-" * 80)

print("5 videos found on search:")
# `video_search` has many other parameters also!
# WARNING: each request made by this method will consume 100 units of your quota (out of 10k daily!)
for index, video in enumerate(yt.video_search(term="Álvaro Justen")):  # Will paginate automatically
    # `video` is a `dict`, but this endpoint doesn't provide full video information (use `videos_infos` to get them)
    print(f"  Video: {video}")
    if index == 4:  # First 5 results only
        break
print("-" * 80)

# The method below can be used to get information in batches (50 videos per request) - you can pass a list of video IDs
# (more than 50) and it'll get data in batches from the API.
last_video = list(yt.videos_infos([video["id"]]))[0]
print("Complete information for last video:")
pprint(last_video)
print("-" * 80)

print("Channel information (2 channels in one request):")
for channel in yt.channels_infos([channel_id_1, channel_id_2]):
    # `channel` is a `dict`
    print(channel)
print("-" * 80)

video_id = "b1FjmUzgFB0"
print(f"Comments for video {video_id}:")
for comment in yt.video_comments(video_id):
    # `comment` is a `dict`
    print(comment)
print("-" * 80)

live_video_id = "yyzIPQsa98A"
print(f"Live chat for video {live_video_id}:")
for chat_message in yt.video_livechat(live_video_id):
    # `chat_message` is a `dict`
    print(chat_message)  # It has the superchat information (`money_currency` and `money_amount` keys)
print("-" * 80)

download_path = Path("transcriptions")
if not download_path.exists():
    download_path.mkdir(parents=True)
print(f"Downloading Portuguese (pt) transcriptions for videos {video_id} and {live_video_id} - saving at {download_path.absolute()}")
for downloaded in yt.download_transcriptions([video_id, live_video_id], language_code="pt", path=download_path):
    vid, status, filename = downloaded["video_id"], downloaded["status"], downloaded["filename"]
    if status == "error":
        print(f"  {vid}: error downloading!")
    elif status == "skipped":
        print(f"  {vid}: skipped, file already exists ({filename}: {filename.stat().st_size / 1024:.1f} KiB)")
    elif status == "done":
        print(f"  {vid}: done ({filename}: {filename.stat().st_size / 1024:.1f} KiB)")
print("-" * 80)

# You can also download audio and video, just replace `download_transcriptions` with `download_audios` or
# `download_videos`. As simple as it is. :)

print("Categories in Brazilian YouTube:")
for category in yt.categories(region_code="BR"):
    # `category` is a `dict`
    print(category)
print("-" * 80)

print("Current most popular videos in Brazil:")
for video in yt.most_popular(region_code="BR"):  # Will paginate automatically
    # `video` is a `dict`, but this endpoint doesn't provide full video information (use `videos_infos` to get them)
    print(f"{video['id']} {video['title']}")
print("-" * 80)

print("Total quota used during this session:")
total_used = 0
for method, units_used in yt.used_quota.items():
    print(f"{method:20}: {units_used:05d} unit{'' if units_used == 1 else 's'}")
    total_used += units_used
print(f"TOTAL               : {total_used:05d} unit{'' if total_used == 1 else 's'}")

测试

要运行所有测试,执行

make test

未来改进

欢迎提交拉取请求! :)

  • 具有以下子命令的命令行界面
    • channel-id:从URL列表(或包含URL的CSV文件名)中获取频道ID,生成CSV输出(仅包含ID)
    • channel-info:从ID列表(或包含ID的CSV文件名)中获取频道信息,生成CSV输出(与channel字典相同的架构)
    • video-info:从ID列表或URL列表中获取视频信息(或包含URL/ID的CSV文件名),生成CSV输出(与video字典相同的架构)
    • video-search:从ID列表或URL列表中获取视频信息(或包含URL/ID的CSV文件名),生成CSV输出(简化的video字典架构或可选获取完整视频信息)
    • video-comments:从视频ID获取评论,生成CSV输出(与comment字典相同的架构)
    • video-livechat:从视频ID获取评论,生成CSV输出(与chat_message字典相同的架构)
    • video-transcriptions:根据语言代码、路径和视频ID/URL列表(或包含URL/ID的CSV文件名)下载视频字幕,将文件下载到目标位置并报告结果
  • 用dataclasses替换dict
  • 创建一个带有docs/reference的网站
  • 处理配额(例如,在使用密钥之前等待一段时间)

许可证

GNU Lesser General Public License (LGPL) version 3.

该项目是由Pythonic CaféNovelo Data合作开发的。

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定要选择哪一个,请了解有关安装包的更多信息。

源代码分布

youtool-0.2.0.tar.gz (19.7 kB 查看哈希值)

上传时间: 源代码

构建分布

youtool-0.2.0-py3-none-any.whl (17.4 kB 查看哈希值)

上传时间: Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面