Skip to content

Commit

Permalink
add some typescript bindings for the youtube api
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 committed May 30, 2021
1 parent 692f739 commit f475c40
Showing 1 changed file with 81 additions and 4 deletions.
85 changes: 81 additions & 4 deletions server/services/youtube.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { URL } from "url";
import axios from "axios";
import axios, { AxiosResponse } from "axios";
import _ from "lodash";
import { RedisClient } from "redis";
import { ServiceAdapter, VideoRequest } from "../serviceadapter";
Expand All @@ -21,6 +21,84 @@ interface YoutubeChannelData {
customUrl?: string
}

interface YoutubeApiVideoListResponse {
kind: "youtube#videoListResponse";
etag: string;
nextPageToken: string;
prevPageToken: string;
pageInfo: YoutubeApiPageInfo;
items: YoutubeApiVideo[];
}

interface YoutubeApiPageInfo {
totalResults: number;
resultsPerPage: number;
}

interface YoutubeApiVideo {
kind: "youtube#video";
etag: string;
id: string;
snippet?: {
publishedAt: string;
channelId: string;
title: string;
description: string;
thumbnails: {
medium: YoutubeThumbnailInfo;
default: YoutubeThumbnailInfo;
};
channelTitle: string;
tags: string[];
categoryId: string;
liveBroadcastContent: string;
defaultLanguage: string;
localized: {
title: string;
description: string;
};
defaultAudioLanguage: string;
};
contentDetails?: {
duration: string;
dimension: string;
definition: string;
caption: string;
licensedContent: boolean;
regionRestriction: {
allowed?: (string)[] | null;
blocked?: (string)[] | null;
};
projection: string;
hasCustomThumbnail: boolean;
};
status?: {
uploadStatus: string;
failureReason: string;
rejectionReason: string;
privacyStatus: string;
publishAt: string;
license: string;
embeddable: boolean;
publicStatsViewable: boolean;
madeForKids: boolean;
selfDeclaredMadeForKids: boolean;
};
statistics?: {
viewCount: number;
likeCount: number;
dislikeCount: number;
favoriteCount: number;
commentCount: number;
};
}

interface YoutubeThumbnailInfo {
url: string;
width: number;
height: number;
}

export default class YouTubeAdapter extends ServiceAdapter {
apiKey: string
redisClient: RedisClient
Expand Down Expand Up @@ -282,7 +360,7 @@ export default class YouTubeAdapter extends ServiceAdapter {
const parts = this.getNeededParts(onlyProperties);
log.silly(`Requesting ${parts.length} parts for ${ids.length} videos`);
try {
const res = await this.api
const res: AxiosResponse<YoutubeApiVideoListResponse> = await this.api
.get("/videos", {
params: {
key: this.apiKey,
Expand All @@ -291,8 +369,7 @@ export default class YouTubeAdapter extends ServiceAdapter {
},
});
const results: Video[] = [];
for (let i = 0; i < res.data.items.length; i++) {
const item = res.data.items[i];
for (const item of res.data.items) {
const video: Video = {
service: this.serviceId,
id: item.id,
Expand Down

0 comments on commit f475c40

Please sign in to comment.