{"id":227,"date":"2021-01-31T00:17:21","date_gmt":"2021-01-30T15:17:21","guid":{"rendered":"http:\/\/drroot.page\/wp\/?p=227"},"modified":"2021-01-31T00:22:22","modified_gmt":"2021-01-30T15:22:22","slug":"%e3%80%902021%e5%b9%b41%e6%9c%88%e7%89%88%e3%80%91api%e7%84%a1%e3%81%97%e3%81%a7youtube-live%e3%81%ae%e3%82%b3%e3%83%a1%e3%83%b3%e3%83%88%e3%82%92%e5%8f%96%e5%be%97%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/drroot.page\/wp\/?p=227","title":{"rendered":"\u30102021\u5e741\u6708\u7248\u3011API\u7121\u3057\u3067Youtube Live\u306e\u30b3\u30e1\u30f3\u30c8\u3092\u53d6\u5f97\u3059\u308b"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/taizan-hokuto\/pytchat\">https:\/\/github.com\/taizan-hokuto\/pytchat<\/a><br \/>\n\u672c\u8a18\u4e8b\u3067\u66f8\u3044\u305f\u30b3\u30fc\u30c9\u306f\u3001\u307b\u307c\u4e0a\u8a18Python\u30ea\u30dd\u30b8\u30c8\u30ea\u306e\u52a3\u5316\u54c1\u3060\u3002<\/p>\n<p><!--more--><\/p>\n<h1>Youtube Live\u30c1\u30e3\u30c3\u30c8\u3092\u30d7\u30ed\u30b0\u30e9\u30e0\u304b\u3089\u53d6\u308d\u3046<\/h1>\n<h2>\u554f\u984c\u70b9<\/h2>\n<p>youtube\u306e\u914d\u4fe1\u306f\u90fd\u5ea6\u90fd\u5ea6\u4ed5\u69d8\u304c\u4ee3\u308f\u308a\u3001\u53bb\u5e74\u306e11\u6708\u304f\u3089\u3044\uff1f\u306b\u5b9f\u88c5\u3057\u305f\u30b3\u30fc\u30c9\u304c\u6700\u8fd1\u5168\u304f\u4f7f\u3044\u7269\u306b\u306a\u3089\u306a\u304f\u306a\u3063\u305f\u308a\u3057\u305f\u3002<br \/>\n\u3068\u3044\u3046\u308f\u3051\u3067\u30012021\u5e741\u6708\u672b\u73fe\u5728\u4f7f\u3048\u308b\u3001Youtube LiveChat\u53d6\u5f97\u30d7\u30ed\u30b0\u30e9\u30e0\u3060\u3002<br \/>\n\u74b0\u5883\u306fnode.js\u3002\u4e0a\u8a18\u306ePython\u30b3\u30fc\u30c9\u306f\u65e5\u672c\u4eba\u306e\u65b9\u304c\u66f8\u3044\u3066\u3044\u3066\u6700\u65b0\u7248\u306eYoutube\u306b\u3082\u5bfe\u5fdc\u3057\u3066\u304a\u308a\u3001\u975e\u5e38\u306b\u53c2\u8003\u306b\u306a\u3063\u305f\u3002<br \/>\nPython\u3092\u3042\u308b\u7a0b\u5ea6\u8aad\u3081\u308b\u306a\u3089\u3001\u3053\u3061\u3089\u3092\u53c2\u8003\u306b\u3057\u305f\u307b\u3046\u304c\u826f\u3044\u304b\u3082\u77e5\u308c\u306a\u3044\u2026\u672c\u8a18\u4e8b\u306eNode\u7248\u3068\u9055\u3044\u3001\u904e\u53bb\u306e\u30a2\u30fc\u30ab\u30a4\u30d6\u306e\u30c1\u30e3\u30c3\u30c8\u3082\u62fe\u3048\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u3002<\/p>\n<h2>\u6982\u8981<\/h2>\n<p>\u4e00\u6614\u524d\u306f\u3001GET\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u30af\u30a8\u30ea\u30d1\u30e9\u30e1\u30fc\u30bf\u306b\u300ccontinuation=XXXXXX&#8230;\u300d\u3092\u3064\u3051\u308c\u3070\u697d\u52dd\u3067\u53d6\u5f97\u51fa\u6765\u305f\u3002<br \/>\ncontinuation\u3068\u3044\u3046\u306e\u306f\u3001\u30c1\u30e3\u30c3\u30c8\u306e\u3069\u3053\u304b\u3089\u53d6\u5f97\u3059\u308b\u304b\u3068\u3044\u3046\u30d1\u30e9\u30e1\u30fc\u30bf\u3060\u3002<\/p>\n<p>\u2026\u306e\u3060\u304c\u3001\u73fe\u884c\u30d0\u30fc\u30b8\u30e7\u30f3\u306f<\/p>\n<ol>\n<li>\u914d\u4fe1\u30da\u30fc\u30b8\u306b\u98db\u3093\u3067\u30da\u30fc\u30b8\u306eHTML\u3092\u5f97\u308b<\/li>\n<li>HTML\u5185\u306eJavascript\u304b\u3089continuation\u7b49\u3092\u5f15\u3063\u3053\u629c\u304f<\/li>\n<li><a href=\"https:\/\/www.youtube.com\/youtubei\/v1\/live_chat\/get_live_chat\">https:\/\/www.youtube.com\/youtubei\/v1\/live_chat\/get_live_chat<\/a> \u306b<strong>POST\u3067<\/strong>\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u9001\u308a\u3064\u3051\u308b<\/li>\n<li>\u65b0\u7740\u306e\u30c1\u30e3\u30c3\u30c8\u7b49\u304c\u98db\u3093\u3067\u304f\u308b \u3053\u306e\u4e2d\u306b\u6b21\u56de\u306econtinuation\u304c\u542b\u307e\u308c\u3066\u3044\u308b<\/li>\n<\/ol>\n<p>\u3068\u3044\u3046\u3084\u3084\u3053\u3057\u3044\u624b\u9806\u3092\u8e0f\u307e\u306d\u3070\u306a\u3089\u3093\u3002<\/p>\n<h2>\u5b9f\u88c5<\/h2>\n<h3>\u74b0\u5883<\/h3>\n<p>typescript ^3.9 + node.js\u3067\u3084\u3063\u3066\u3044\u304f\u306e\u3067\u3001\u306a\u308b\u3079\u304f\u5358\u4f53\u3067\u4f7f\u3048\u308b\u3088\u3046\u5207\u308a\u5206\u3051\u306f\u3057\u305f\u304c\u3001\u305d\u306e\u8fba\u306e\u77e5\u8b58\u304c\u5fc5\u8981\u3067\u3042\u308b\u3002<br \/>\n\u307e\u305fnode\u95a2\u4fc2\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3068\u3057\u3066\u3001axios\u3068node-html-parser\u3092\u4e8b\u524d\u306bnpm i\u306a\u308ayarn add\u306a\u308a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<\/p>\n<h3>\u5b9f\u88c5<\/h3>\n<p>\u8a73\u7d30\u306f\u30b3\u30e1\u30f3\u30c8\u53c2\u7167\u3002<\/p>\n<pre><code class=\"language-javascript\">import axios from &quot;axios&quot;;\nimport { parse } from &quot;node-html-parser&quot;;\n\ninterface Options {\n  key: string;\n  continuation: string;\n  visitorData: string;\n  clientVersion: string;\n}\n\nexport class Live {\n  public channelId = &quot;&quot;;\n  public liveId = &quot;&quot;;\n\n  private options?: Options = undefined;\n\n  private readonly headers = {\n    &quot;user-agent&quot;:\n      &quot;Mozilla\/5.0 (Windows NT 6.3; Win64; x64) &quot; +\n      &quot;AppleWebKit\/537.36 (KHTML, like Gecko) &quot; +\n      &quot;Chrome\/86.0.4240.111 Safari\/537.36&quot;\n  };\n  private chatUri = &quot;https:\/\/www.youtube.com\/live_chat&quot;;\n  private apiEpUri =\n    &quot;https:\/\/www.youtube.com\/youtubei\/v1\/live_chat\/get_live_chat&quot;;\n  private pollingHandle?: NodeJS.Timer;\n\n  \/**\n   * \u30c1\u30e3\u30f3\u30cd\u30ebID\u304b\u3089\u6709\u52b9\u306a\u914d\u4fe1URL\u5f15\u3063\u3053\u629c\u3044\u3066\u304f\u308b\n   * @param channelId\n   *\/\n  public static async getLiveFromChannelId(channelId: string) {\n    \/\/ \u5358\u7d14\u306b\u30c1\u30e3\u30f3\u30cd\u30ebURL\u306e\u5f8c\u308d\u306b\/live\u4ed8\u3051\u308c\u3070\u30e9\u30a4\u30d6\u306eURL\u306b\u98db\u3079\u308b\n    \/\/ \u3064\u3044\u3067\u306b\u3088\u308a\u30d1\u30fc\u30b9\u3057\u3084\u3059\u3044embed\u30da\u30fc\u30b8\u3092\u5229\u7528\u3059\u308b\n    const uri = &quot;https:\/\/www.youtube.com\/embed\/live_stream&quot;;\n\n    const res = await axios\n      .get(uri, {\n        params: {\n          channel: channelId\n        }\n      })\n      .catch(err =&gt; err.response);\n\n    if (res.status !== 200) return null;\n    const root = parse(res.data).querySelectorAll(&quot;link&quot;);\n    let vid = null;\n    for (const v of root) {\n      if (v.getAttribute(&quot;rel&quot;) === &quot;canonical&quot;) {\n        const href = v.getAttribute(&quot;href&quot;);\n        if (href) {\n          const match = href.match(\/v=(.+)\/);\n          if (match &amp;&amp; match.length &gt;= 2) {\n            vid = match[1];\n          }\n        }\n      }\n    }\n    return vid;\n  }\n\n  \/**\n   * \u30b3\u30e1\u30f3\u30c8\u30dd\u30fc\u30ea\u30f3\u30b0\u958b\u59cb\n   * @param pollingIntervalMs\n   *\/\n  public begin(\n    pollingIntervalMs: number,\n    callbackLiveBegin: () =&gt; void,\n    callbackCommentsReceived: (comments: Comment[]) =&gt; void\n  ) {\n    this.Logging(&quot;begin&quot;);\n    if (this.liveId) {\n      callbackLiveBegin();\n      const closure = async () =&gt; {\n        \/\/ \u521d\u56de\u3060\u3051Continuation\u62fe\u3044\u306b\u884c\u304f\n        if (!this.options) {\n          this.options = await this.fetchFirstLive(this.liveId);\n        }\n\n        \/\/ \u521d\u56de\u30d1\u30fc\u30b9\u304c\u5931\u6557\u3057\u3066\u308b\u304b\u3082\u306a\u306e\u3067\u99ac\u9e7f\u3063\u307d\u3044\u3051\u30692\u5ea6\u5224\u5b9a\n        if (this.options) {\n          const item = await this.fetchChat(this.options);\n          if (\n            item &amp;&amp;\n            item.continuationContents &amp;&amp;\n            item.continuationContents.liveChatContinuation\n          ) {\n            const {\n              continuations,\n              actions\n            } = item.continuationContents.liveChatContinuation;\n\n            \/\/ \u65b0\u7740\u30b3\u30e1\u30f3\u30c8\u304c\u3042\u308b\u306a\u3089actions\u30a2\u30a4\u30c6\u30e0\u304c\u3042\u308b\n            if (actions) {\n              const comments = this.buildCommentObject(actions);\n              callbackCommentsReceived(comments);\n            }\n\n            \/\/ continuation\u3092\u66f4\u65b0\n            const icd = continuations[0].invalidationContinuationData;\n            this.options.continuation = icd.continuation;\n          } else {\n            this.Logging(&quot;fetchChat()\u304c\u30b4\u30df\u8fd4\u3057\u3066\u6765\u305f&quot;);\n          }\n        } else {\n          this.Logging(&quot;options \u53d6\u5f97\u5931\u6557&quot;);\n        }\n      };\n      closure();\n      this.pollingHandle = setInterval(closure, pollingIntervalMs);\n    } else {\n      throw new Error(&quot;liveId\u304b\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306e\u8a2d\u5b9a\u5fd8\u308c&quot;);\n    }\n  }\n\n  \/**\n   * \u30b3\u30e1\u30f3\u30c8\u30dd\u30fc\u30ea\u30f3\u30b0\u7d42\u4e86\n   *\/\n  public end() {\n    if (this.pollingHandle) {\n      clearInterval(this.pollingHandle);\n    }\n  }\n\n  \/**\n   * post\u3057\u3066\u30c1\u30e3\u30c3\u30c8\u30a2\u30a4\u30c6\u30e0\u3092\u53d6\u5f97\n   *\/\n  async fetchChat(options: Options) {\n    const params = {\n      continuation: options.continuation,\n      visitorData: options.visitorData,\n      clientVersion: options.clientVersion\n    };\n\n    const data = this.buildOptions(\n      params.continuation,\n      this.headers[&quot;user-agent&quot;],\n      params.clientVersion,\n      params.visitorData\n    );\n\n    const res = await axios\n      .post(this.apiEpUri, data, {\n        headers: this.headers,\n        params: {\n          key: options.key\n        }\n      })\n      .catch(e =&gt; e.response);\n\n    if (res.status === 200) {\n      return res.data;\n    } else {\n      return null;\n    }\n  }\n\n  \/**\n   * post\u3059\u308b\u70ba\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u7d44\u307f\u7acb\u3066\u308b\n   *\/\n  buildOptions(\n    continuation: string,\n    userAgent: string,\n    clientVersion: string,\n    visitorData: string\n  ) {\n    const ret = {\n      context: {\n        client: {\n          visitorData,\n          userAgent,\n          clientName: &quot;WEB&quot;,\n          clientVersion\n        }\n      },\n      continuation\n    };\n    return ret;\n  }\n\n  \/**\n   * \u914d\u4fe1\u304b\u3089Continuous Key\u3092\u629c\u304f \u3064\u3044\u3067\u306bAPI Key\u3082\u629c\u304f\n   * @param videoId \u914d\u4fe1ID\n   *\/\n  async fetchFirstLive(videoId: string): Promise&lt;Options | undefined&gt; {\n    const res = await axios.get(this.chatUri, {\n      headers: this.headers,\n      params: { v: videoId }\n    });\n\n    \/\/ html\u306e\u4e2d\u306bjs\u3068\u304b\u3082\u66f8\u3044\u3066\u3042\u3063\u3066\u3001\u898b\u305f\u611f\u3058Regex\u4e00\u767a\u3067\u629c\u3051\u308b\u2026\u305f\u3076\u3093\u3002\n    const html: string = res.data;\n    const matchedKey = html.match(\/&quot;INNERTUBE_API_KEY&quot;:&quot;(.+?)&quot;\/);\n    const matchedCtn = html.match(\/&quot;continuation&quot;:&quot;(.+?)&quot;\/);\n    const matchedVisitor = html.match(\/&quot;visitorData&quot;:&quot;(.+?)&quot;\/);\n    const matchedClient = html.match(\/&quot;clientVersion&quot;:&quot;(.+?)&quot;\/);\n\n    const matched = (obj: RegExpMatchArray | null) =&gt; {\n      if (obj &amp;&amp; obj.length &gt;= 2) {\n        return obj[1];\n      }\n      return undefined;\n    };\n\n    \/\/ \u306a\u304a2021\/01\/29\u73fe\u5728\u3001key\u306f\u2193\u306e\u3067\u56fa\u5b9a\u3063\u307d\u3044\n    \/\/ AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8\n    const ret = {\n      key: matched(matchedKey),\n      continuation: matched(matchedCtn),\n      visitorData: matched(matchedVisitor),\n      clientVersion: matched(matchedClient)\n    };\n\n    \/\/ \u4e00\u500b\u3067\u3082\u629c\u3051\u304c\u3042\u3063\u305f\u3089Undef\u8fd4\u3059\n    if (Object.values(ret).includes(undefined)) {\n      return undefined;\n    } else {\n      return ret as Options;\n    }\n  }\n\n  \/**\n   * actionItem\u30d7\u30ed\u30d1\u30c6\u30a3\u304b\u3089\u30b3\u30e1\u30f3\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u5207\u308a\u51fa\u3057\u3066\u8fd4\u3059\n   * @param actions data\u5185\u306eactions\u914d\u5217\n   *\/\n  private buildCommentObject(actions: any): Comment[] {\n    \/\/ actions:[{addChatItemAction:{item:{...}}}]\n    const ret: Array&lt;Comment&gt; = [];\n    for (const actionItem of actions) {\n      \/\/ addChatItemAction\u304c\u3042\u308c\u3070\u30b3\u30e1\u30f3\u30c8\u304b\u30b9\u30d1\u30c1\u30e3\n      if (&quot;addChatItemAction&quot; in actionItem) {\n        const item = actionItem.addChatItemAction.item;\n        \/\/ \u30b3\u30e1\u30f3\u30c8\u5171\u901a\u9805\u76ee(\u5b9f\u8cea\u30b9\u30d1\u30c1\u30e3\u306f\u901a\u5e38\u30b3\u30e1\u306e\u4e0a\u4f4d\u4e92\u63db)\n        let common = null;\n        let isSuperChat = false;\n\n        \/\/ \u901a\u5e38\u30b3\u30e1\u306e\u5834\u5408\n        if (&quot;liveChatTextMessageRenderer&quot; in item) {\n          common = item.liveChatTextMessageRenderer;\n        }\n        \/\/ \u30b9\u30d1\u30c1\u30e3\u306e\u5834\u5408\n        else if (&quot;liveChatPaidMessageRenderer&quot; in item) {\n          common = item.liveChatPaidMessageRenderer;\n          isSuperChat = true;\n        }\n\n        \/\/ \u5171\u901a\u30c7\u30fc\u30bf\u6574\u5f62\n        if (common) {\n          ret.push(\n            \/\/ \u81ea\u4f5c\u30b3\u30e1\u30f3\u30c8\u30d1\u30fc\u30b5\u3092\u547c\u3073\u51fa\u3059 \u4eba\u306b\u3088\u3063\u3066\u6271\u3044\u304c\u9055\u3046\u306e\u3067\u4efb\u305b\u308b\n            \/\/ Comment.fromMessageRenderer(common, isSuperChat, this.liveId)\n          );\n        }\n      }\n      \/\/ addLiveChatTickerItemAction\u304c\u3042\u308c\u3070\u30b9\u30fc\u30d1\u30fc\u30b9\u30c6\u30c3\u30ab\u30fc\n      else if (&quot;addLiveChatTickerItemAction&quot; in actionItem) {\n        \/\/ const item = actionItem.addLiveChatTickerItemAction.item\n        \/\/ STUB \u3088\u304f\u77e5\u3089\u3093 \u305d\u3082\u305d\u3082\u81ea\u5206\u306e\u30c1\u30e3\u30f3\u30cd\u30eb\u53ce\u76ca\u5316\u3057\u3066\u306d\u3047\n      }\n    }\n    return ret;\n  }\n\n  \/**\n   * logging\u30e9\u30c3\u30d1 \u30c7\u30d0\u30c3\u30b0\u7b49\u306b\u3069\u3046\u305e\n   * @param message\n   *\/\n  private Logging(message: string) {\n    \/\/ console.log(message)\n  }\n}\n<\/code><\/pre>\n<p>\u5f15\u3063\u3053\u629c\u3044\u3066\u304d\u305fJson\u304b\u3089\u30b3\u30e1\u30f3\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u3076\u3063\u3053\u629c\u304f\u306e\u306f\u3001\u5275\u597d\u30ea\u30ca\u3055\u3093\u306e\u5f93\u6765\u578b\u30af\u30ed\u30fc\u30e9\u3092\u6d41\u7528\u3059\u308b\u306e\u304c\u65e9\u3044\u305e\u3002<br \/>\nMessageRenderer\u3068\u3044\u3046\u30ad\u30fc\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3067\u304d\u308c\u3070\u3001\u5f93\u6765\u306e\u3084\u308a\u65b9\u304c\u305d\u306e\u307e\u307e\u4f7f\u3048\u308b\u3002<br \/>\n<a href=\"https:\/\/github.com\/LinaTsukusu\/youtube-chat\">https:\/\/github.com\/LinaTsukusu\/youtube-chat<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/github.com\/taizan-hokuto\/pytchat &hellip; <a href=\"https:\/\/drroot.page\/wp\/?p=227\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-227","post","type-post","status-publish","format-standard","hentry","category-it"],"_links":{"self":[{"href":"https:\/\/drroot.page\/wp\/index.php?rest_route=\/wp\/v2\/posts\/227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/drroot.page\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/drroot.page\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/drroot.page\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/drroot.page\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=227"}],"version-history":[{"count":0,"href":"https:\/\/drroot.page\/wp\/index.php?rest_route=\/wp\/v2\/posts\/227\/revisions"}],"wp:attachment":[{"href":"https:\/\/drroot.page\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/drroot.page\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/drroot.page\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}