Add Youtube API connectivity

master
Dessa Simpson 2020-11-09 16:28:30 -07:00
parent c3079a198d
commit 5ab11f69f6
2 changed files with 28 additions and 2 deletions

View File

@ -1,5 +1,4 @@
import * as logging from './logging';
import { log, LogLevel } from "./logging"
import { LogLevel } from "./logging"
if (typeof process.env.PORT == 'undefined') {
console.log("Missing environment variable PORT");
@ -31,6 +30,12 @@ if (typeof process.env.SESSION_SECRET == 'undefined') {
}
export const sessionSecret: string = process.env.SESSION_SECRET!;
if (typeof process.env.YOUTUBE_SECRET == 'undefined') {
console.log("Missing environment variable YOUTUBE_SECRET");
process.exit(1);
}
export const youtubeSecret: string = process.env.YOUTUBE_SECRET!;
export const logLevel = (process.env.LOG_LEVEL ? LogLevel[process.env.LOG_LEVEL as keyof typeof LogLevel] : LogLevel.ERROR );
console.log("Running with logLevel = " + logLevel)

21
src/youtube.ts Normal file
View File

@ -0,0 +1,21 @@
import * as config from "./config";
import { log, LogLevel } from "./logging"
import fetch, { Response as FetchResponse } from "node-fetch";
export async function apiRequest(endpoint: string, parameters: URLSearchParams): Promise <any> {
log(LogLevel.DEBUG,`Call: youtube.apiRequest(${endpoint})`);
parameters.set('key',config.youtubeSecret);
var requestUrl = "https://www.googleapis.com/youtube/v3/" + endpoint + "?" + parameters.toString();
return fetch(requestUrl)
.then(async (res: FetchResponse) => {
if (res.status == 200) {
return res.json();
} else {
log(LogLevel.WARNING,"Failed Youtube API request:");
log(LogLevel.WARNING,"Request URL: " + requestUrl);
log(LogLevel.WARNING,"Response:");
log(LogLevel.WARNING,JSON.stringify(await res.json(),null,2));
return false;
}
})
}