如何使用频道链接将数据从 YouTube API 提取到 Google 表格

2024-04-09

我在这里完全是新手。我尝试了很多脚本来执行以下操作。 (显然来自 stackoverflow 中的其他问题 - 我还不太擅长编写代码)

我的目标是,

  1. 从 Sheet 1 A2:A 的通道链接中读取通道 ID
  2. 将订阅者总数拉至 B2:B
  3. 将频道总观看次数拉至 C2:C
  4. 将视频上传总数拉至 D2:D

我已经有了 API 密钥,并尝试了我所能做的几乎所有事情来实现这一目标。但是,我无法让它发挥作用。

有人可以教育我如何完成这项工作吗?

我将非常感谢你的帮助。提前致谢


这是一个 Google Apps 脚本,在谷歌表格 https://docs.google.com/spreadsheets/d/1qo_t31xAH6jE76QZy2ha1UUJgsJxLGtUzdpAUqqWnnw/edit?usp=sharing:

// This Google Apps Script fills a Google Sheet with statistics associated to given YouTube channels.

// More precisely this script assumes that in columns A after the first row there are channel identifiers,
// which can be a channel ID (such as `UC0aMPje3ZACnQPKM_qzY0vw`) or any channel URL such as:
// - https://www.youtube.com/channel/UCK_cUZLMpibyRiIdp0uF-lQ
// - https://www.youtube.com/user/Fairphone
// - https://www.youtube.com/c/lemondefr

function fills_statistics_associated_to_given_youtube_channels() {
  const A = 1, B = 2, C = 3, D = 4;
  var sheet = SpreadsheetApp.getActiveSheet();
  const YOUTUBE_CHANNELS = YouTube.Channels;
  const CHANNEL_URL_PREFIX = "https://www.youtube.com/";
  // Consider an arbitrary number of channels written after the first row.
  for(var row = 2; row <= sheet.getLastRow(); row++)
  {
    const channelURL = sheet.getRange(row, A).getValue().toString().replace(CHANNEL_URL_PREFIX, "");
    // Retrieve the channel ID from the channel identifier provided.
    // If the channel URL provided is an username-based one, obtain the associated channel ID.
    var channelID;
    if(channelURL.startsWith("user/"))
      channelID = YOUTUBE_CHANNELS.list("id", {"forUsername": channelURL.replace("user/", "")}).items[0].id;
    // As YouTube Data API v3 Channels: list `forUsername` filter doesn't work for this kind of URL, proceed with a YouTube UI reverse-engineering approach.
    // This approach consists in obtaining JSON encoded JavaScript `ytInitialData` from the HTML code of the YouTube UI channel.
    else if(channelURL.startsWith("c/"))
    {
      // Note that the following reverse-engineering method isn't working currently because of escaped JSON syntax and I haven't found any clean way to parse it correctly.
      /*
      const channelHTML = UrlFetchApp.fetch(CHANNEL_URL_PREFIX + channelURL).getContentText();
      const ytInitialDataStr = channelHTML.split('">var ytInitialData = ', 2)[1].split(";</script>", 1)[0];
      const ytInitialDataJSON = JSON.parse(ytInitialDataStr);
      channelID = ytInitialDataJSON["responseContext"]["serviceTrackingParams"][0]["params"][6]["value"];
      continue;
      */

      // However by relying on a YouTube operational API instance doing this reverse-engineering method, it works fine. Nevertheless if YouTube servers detect the instance as suspicious, you have to host your own instance cf below. If you go this way, replace `yt.lemnoslife.com` to your instance hostname.
      // YouTube operational API source code is available at: https://github.com/Benjamin-Loison/YouTube-operational-API
      const channelStr = UrlFetchApp.fetch("https://yt.lemnoslife.com/channels?part=snippet&cId=" + channelURL.replace("c/", "")).getContentText();
      const channelJSON = JSON.parse(channelStr);
      channelID = channelJSON["items"][0]["id"];
    }
    // Not filtering with `if(channelURL.startsWith("channel/"))` in order to support channel ID too.
    else
      channelID = channelURL.replace("channel/", "");
    // For more details see YouTube Data API v3 Channels: list endpoint documentation: https://developers.google.com/youtube/v3/docs/channels/list
    // Note that the channel ID retrieval can't be optimized in terms of HTTPS requests.
    // However could optimize the other HTTPS requests to YouTube Data API v3 by implementing using `id` filter to provide up to 50 channel IDs as documented by `maxResults`.
    const statistics = YOUTUBE_CHANNELS.list("statistics", {"id": channelID}).items[0].statistics;
    sheet.getRange(row, B).setValue(statistics.subscriberCount);
    sheet.getRange(row, C).setValue(statistics.viewCount);
    sheet.getRange(row, D).setValue(statistics.videoCount);
  }
}

请注意,目前的情况channelURL.startsWith("c/")依靠我的开源 https://github.com/Benjamin-Loison/YouTube-operational-API YouTube 操作 API https://yt.lemnoslife.com.

请注意,此代码不支持句柄,即使可以支持他们 https://stackoverflow.com/a/74324720.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用频道链接将数据从 YouTube API 提取到 Google 表格 的相关文章

  • YouTube v3 CORS 不考虑返回的“Access-Control-Allow-Origin”

    我有一个客户端和一个服务器 我的工作流程如下 服务器使用 API v3 将片段上传到 youtube 并获取可恢复的 url Youtube v3 API for resumable uploads https developers goo
  • youtube api v3 按关键字搜索 javascript

    谷歌开发人员页面上给出的 按关键字搜索 的 javascript 示例不适合我 https developers google com youtube v3 code samples javascript https developers
  • YOUTUBE API:检索视频关键字

    最近 YouTube 决定仅向经过身份验证的开发者输出视频关键字 我注册了开发者密钥 我试图获取一个我将解析的 XML 通过请求 https gdata youtube com feeds api videos COwIYbYQUrQ ke
  • 如何将 YouTube API 集成到我的 iPhone 应用程序中?

    我想将 YouTube API 集成到我的应用程序中 我该怎么做 附注 我正在为 YouTube 频道制作一个应用程序 我尝试以webview 但这让一切变得更糟 因为用户可以看到 YouTube 控件 搜索等 以及有关 YouTube i
  • 没有 api 密钥的 YouTube 视频标题和持续时间

    如何在不使用 api 密钥的情况下获取 youtube 视频标题和时长 我检查过带有 API v3 且没有 API 密钥的 Youtube 视频标题 https stackoverflow com questions 30084140 yo
  • 如何禁用 YouTube Iframe 上的全屏?

    我里面有一个 div 容器和 Iframe 我指定宽度 200 和高度 200 当我点击全屏时 视频变得模糊且质量非常差 所以 我想看看是否可以在 YouTube iframe 上禁用全屏 I used controls 0在我的网址末尾
  • YouTube iFrame Player API 无法在 DOMWindow 上执行 postMessage

    我正在尝试使用以下命令将 youtube 加载到我的网页中YouTube iFrame Player API 并在加载时出现以下错误 Failed to execute postMessage on DOMWindow The target
  • YouTube API v3 的描述编码错误

    我已经成功创建了一个项目 通过 VB NET 以编程方式上传 YouTube 视频 并且它已经工作了几周直到今天 我在上传描述字段中包含德语变音符号的视频时遇到问题 当我尝试上传此类视频时 我收到以下信息WebException Syste
  • Youtube 退出全屏模式 TextView 可见性问题

    我正在全屏模式下播放视频 当我单击后退按钮时 我可以退出全屏模式 但无法显示我在全屏情况下隐藏的 TextView 要在全屏模式下隐藏 textView 我使用以下代码 Override public void onInitializati
  • Youtube as3 API 似乎不再起作用

    我正在使用 as3 Youtube 官方 API 我需要在 swf 文件中加载 API 播放器 我已经在几个项目上完成了 一切都很好 但几个小时后 我的所有项目现在都坏了 这是崩溃的代码片段 Security allowDomain www
  • 使用 Python 中的 YouTube Data API v3 将视频上传到 YouTube 并将其添加到播放列表

    我编写了一个脚本 在 python 中使用 YouTube Data API v3 将视频上传到 YouTube 并借助中给出的示例示例代码 https developers google com youtube v3 guides upl
  • onYouTubeIframeAPIReady() 未触发

    我已经浏览了很多问题和 youtube api 的东西 但我一生都无法弄清楚为什么 onYouTubeIframeAPIReady 不起作用 这是我的 iframe 还有我的脚本 function callYTapi var tag doc
  • 如果用户没有安装 YouTube 应用,如何在 Android 应用中播放 YouTube 视频?

    我正在尝试在 Android 应用程序中播放 YouTube 视频 我正在使用 YouTubePlayer API 它运行良好 但问题是我想在库中拥有这个播放器 如果没有安装 YouTube 应用程序 那么电影就不会显示 而且我还有另一个问
  • 从 XML 获取 viewCount [重复]

    这个问题在这里已经有答案了 我目前正在使用YouTube API https developers google com youtube 来自 Google 我正在尝试获取 viewCount 数组 我已经尝试过这个 但一点运气都没有 He
  • onYouTubeIframeAPIReady 函数未调用

    我想打电话onYouTubeIframeAPIReady函数 但这没有触发 我只得到frameID在控制台中 但其他函数没有被调用 document ready function var player var ytsrc video hol
  • 嵌入式 YouTube 播放器无法加载许可内容

    我已经查遍了 找不到这个问题的答案 所以我希望这里有人会知道 我正在建立一个网站 该网站将使用 Iframe API 嵌入 YouTube 播放器 除了观看未经许可的内容外 我还计划观看许可内容 即我从 YouTube 或 Google P
  • YouTube 在 iOS 上直播?

    这里的文档有点难以解析 我想知道是否有什么办法 将 YouTube 直播传输到 iOS 应用程序中 无需显着 任何 YouTube 品牌 Stream from作为 YouTube 直播的广播流的 iOS 设备 我最初的谷歌搜索得到了不同的
  • YouTube 分析 API 问题

    我是 YouTube Analytics API 的新手 有几个问题 为了检索 Analytics 报告 我必须为 ids 参数指定通道 ID 如何查找经过 OAuth 身份验证的用户的通道 ID 我在示例应用程序中看到 我可以调用 Dat
  • 在 Google Chrome 扩展程序中播放盈利的 YouTube 歌曲。我有什么选择吗?

    我在开发 Google Chrome 扩展程序时遇到了巨大的障碍 任何盈利的 YouTube 歌曲 例如带有广告 都不会播放 此处记录了这一点 https developers google com youtube flash api re
  • 使用GoogleAuthUtil时如何获取refreshToken

    我在用着GoogleAuthUtil in Google Play Services在安卓上 打电话后GoogleAuthUtil getToken context userName scope 我得到了这样的令牌 ya29 wQBWzta

随机推荐