|
| 1 | +/** |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +const {onCall, HttpsError} = require("firebase-functions/v2/https"); |
| 18 | +const {defineString, defineSecret} = require("firebase-functions/params"); |
| 19 | + |
| 20 | +const {google} = require("googleapis"); |
| 21 | + |
| 22 | +const youtubeKey = defineSecret("YOUTUBE_API_KEY"); |
| 23 | +const defaultChannelId = defineString("DEFAULT_CHANNEL_ID", { |
| 24 | + default: "UCP4bf6IHJJQehibu6ai__cg", |
| 25 | +}); |
| 26 | + |
| 27 | +exports.getChannelInfo = onCall( |
| 28 | + {secrets: [youtubeKey]}, |
| 29 | + |
| 30 | + async (request, response) => { |
| 31 | + const youtube = google.youtube({ |
| 32 | + version: "v3", |
| 33 | + auth: youtubeKey.value(), |
| 34 | + }); |
| 35 | + const channelId = request.data.channelId || defaultChannelId; |
| 36 | + |
| 37 | + const channelInfo = {id: channelId}; |
| 38 | + |
| 39 | + // Fetch channel information |
| 40 | + let channelData; |
| 41 | + try { |
| 42 | + // tell the client app we're starting |
| 43 | + response.sendChunk({status: "fetching channel info", channelInfo}); |
| 44 | + |
| 45 | + // https://developers.google.com/youtube/v3/docs/channels/list |
| 46 | + const {data} = await youtube.channels.list({ |
| 47 | + part: "snippet,statistics", |
| 48 | + id: channelId, |
| 49 | + maxResults: 1, |
| 50 | + }); |
| 51 | + channelData = data; |
| 52 | + } catch (error) { |
| 53 | + throw new HttpsError("internal", "Failed to fetch channel data."); |
| 54 | + } |
| 55 | + |
| 56 | + if (!channelData.items || channelData.items.length !== 1) { |
| 57 | + throw new HttpsError( |
| 58 | + "invalid-argument", |
| 59 | + `Channel with ID ${channelId} not found.`, |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + const channel = channelData.items[0]; |
| 64 | + (channelInfo.channelTitle = channel.snippet.title), |
| 65 | + (channelInfo.channelDescription = channel.snippet.description), |
| 66 | + (channelInfo.subscriberCount = channel.statistics.subscriberCount), |
| 67 | + // send latest data to the client app |
| 68 | + response.sendChunk({status: "found channel info", channelInfo}); |
| 69 | + |
| 70 | + // Fetch the channel's latest videos |
| 71 | + let videoData; |
| 72 | + try { |
| 73 | + // tell the client app we're starting to fetch videos |
| 74 | + response.sendChunk({status: "fetching latest videos", channelInfo}); |
| 75 | + // https://developers.google.com/youtube/v3/docs/search/list |
| 76 | + const {data} = await youtube.search.list({ |
| 77 | + part: "id, snippet", |
| 78 | + order: "date", |
| 79 | + channelId, |
| 80 | + maxResults: 3, |
| 81 | + }); |
| 82 | + videoData = data; |
| 83 | + } catch (error) { |
| 84 | + throw new HttpsError("internal", "Failed to fetch video data."); |
| 85 | + } |
| 86 | + const videos = (videoData.items || []).map((video) => ({ |
| 87 | + videoTitle: video.snippet.title, |
| 88 | + videoUrl: `https://www.youtube.com/watch?v=${video.id.videoId}`, |
| 89 | + videoDescription: video.snippet.description, |
| 90 | + })); |
| 91 | + |
| 92 | + channelInfo.recentVideos = videos; |
| 93 | + // send the latest data to the client app |
| 94 | + response.sendChunk({ |
| 95 | + status: `found ${videos.length} videos`, |
| 96 | + channelInfo, |
| 97 | + }); |
| 98 | + |
| 99 | + return channelInfo; |
| 100 | + }, |
| 101 | +); |
0 commit comments