-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathyoutube.ts
116 lines (101 loc) · 3.47 KB
/
youtube.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
namespace pxt.youtube {
export let apiKey: string = undefined;
function checkKey() {
if (!apiKey)
U.userError(`YouTube API key missing`);
}
export interface PlaylistResource {
items: Playlist[];
}
export interface Playlist {
id: string;
snippet: {
publishedAt: string;
channelId: string;
title: string;
description: string;
thumbnails?: Thumbnails;
}
}
export interface Thumbnail {
url: string;
width: number;
height: number;
}
export interface Thumbnails {
default?: Thumbnail;
medium?: Thumbnail;
high?: Thumbnail;
standard?: Thumbnail;
maxres?: Thumbnail;
}
function resolveThumbnail(thumbnails: Thumbnails) {
if (!thumbnails) return "";
const thumbnail = (thumbnails.medium || thumbnails.high || thumbnails.standard || thumbnails.default);
return thumbnail?.url || "";
}
function resolveDescription(d: string) {
// grab first paragraph.
return d.split(/\n\s+/, 1)[0].trim();
}
export function playlistItemToCodeCard(video: PlaylistItem): pxt.CodeCard {
return <pxt.CodeCard>{
"name": video.snippet.title.replace(/[^-]*-/, '').trim(),
"description": resolveDescription(video.snippet.description),
"youTubeId": video.snippet.resourceId.videoId,
"youTubePlaylistId": video.snippet.playlistId,
"imageUrl": resolveThumbnail(video.snippet.thumbnails)
}
}
export function playlistInfoAsync(playlistId: string) {
checkKey()
const url = `https://www.googleapis.com/youtube/v3/playlists?part=snippet&id=${playlistId}&key=${apiKey}`;
return pxt.Util.httpGetJsonAsync(url)
.then((res: PlaylistResource) => res.items[0]);
}
export interface PlaylistItem {
snippet: {
playlistId: string;
title: string;
description: string;
publishedAt: string;
thumbnails: Thumbnails;
position: number;
resourceId: {
videoId: string;
}
}
}
export interface PlaylistVideos {
nextPageToken: string;
prevPageToken: string;
items: PlaylistItem[];
}
export async function listPlaylistVideosAsync(playlistId: string): Promise<PlaylistItem[]> {
checkKey()
let items: PlaylistItem[] = []
let pageToken: string = undefined;
do {
let url = `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=${playlistId}&key=${apiKey}`;
if (pageToken)
url += `&pageToken=${pageToken}`;
const videos = await pxt.Util.httpGetJsonAsync(url)
items = items.concat(videos.items);
pageToken = videos.nextPageToken;
} while (pageToken);
if (pxt.options.debug)
pxt.debug(JSON.stringify(items, null, 2));
return items;
}
export function watchUrl(videoId?: string, playlistId?: string) {
let url: string = undefined;
if (videoId) {
url = `https://www.youtube.com/watch?v=${videoId}`;
if (playlistId)
url += `&list=${playlistId}`;
} else if (playlistId) {
url = `https://www.youtube.com/playlist?list=${playlistId}`;
}
return url;
}
}