如何使用频道链接将数据从 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 表格 的相关文章

随机推荐