-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoutube-rss.ts
More file actions
149 lines (136 loc) · 5.15 KB
/
Copy pathyoutube-rss.ts
File metadata and controls
149 lines (136 loc) · 5.15 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { createCache, MemoryCache, memoryStore } from "cache-manager";
import Parser from "rss-parser";
import { is } from "typia";
import { ParsedEntry } from "./parsed-entry";
export default class YouTubeRss {
private channelByAnyTermCache: MemoryCache;
private urlPattern = new RegExp(/(?:https?:\/\/)?(?:www\.|m\.)?(?:youtube\.com|youtu\.be)\/(?:channel\/|user\/|c\/)?@?([^/\s?]+).*$/);
private parser = new Parser({
timeout: 7e3, // 7 seconds
headers: { "User-Agent": "Axobot feedparser" },
});
constructor() {
this.channelByAnyTermCache = createCache(memoryStore({
max: 1_000,
ttl: 12 * 60 * 60 * 1000, // 12 hour
}));
}
async getDisplayName(channelId: string): Promise<string | undefined> {
if (channelId.length < 15 || channelId.length > 25) {
console.warn(`Invalid YouTube channel ID: ${channelId}`);
return undefined;
}
const result = await fetch(
"https://www.googleapis.com/youtube/v3/channels?" + new URLSearchParams({
key: process.env.YOUTUBE_API_KEY,
id: channelId,
part: "snippet",
maxResults: "1",
fields: "items(snippet(title))",
}).toString()
);
if (!result.ok) {
console.warn(`Failed to fetch YouTube channel name for ${channelId}: ${result.status}`);
console.debug(await result.text());
return undefined;
}
const json = await result.json();
if (!json.items || json.items.length === 0) {
console.warn(`No YouTube channel found for ${channelId}`);
return undefined;
}
return json.items[0].snippet.title;
}
async getLastPost(UrlOrChannelId: string): Promise<ParsedEntry | undefined> {
const channelId = await this.getChannelByAnyTerm(UrlOrChannelId);
if (!channelId) {
return undefined;
}
const feed = await this.getFeed(channelId);
if (!feed) {
return undefined;
}
const entry = feed.items[0];
return this.parseEntry(entry);
}
private async getChannelByAnyTerm(term: string): Promise<string | undefined> {
if (typeof term !== "string" || term.length < 3 || term.length > 100) {
console.warn("Invalid search term");
return undefined;
}
const urlMatch = this.urlPattern.exec(term);
if (urlMatch) {
term = urlMatch[1];
}
const cached = await this.channelByAnyTermCache.get<string>(term);
if (cached) {
return cached;
}
const channelId = (await this.isValidChannelId(term)) ? term : (await this.apiFindChannelByHandle(term));
if (channelId) {
this.channelByAnyTermCache.set(term, channelId);
return channelId;
}
return undefined;
}
private async isValidChannelId(channelId: string) {
if (!/^[a-zA-Z\d]{10,25}$/.test(channelId)) {
return false;
}
const url = `https://www.youtube.com/channel/${channelId}`;
const result = await fetch(url);
return result.ok;
}
private async apiFindChannelByHandle(term: string) {
const result = await fetch(
"https://www.googleapis.com/youtube/v3/channels?" + new URLSearchParams({
key: process.env.YOUTUBE_API_KEY,
forHandle: term,
part: "id",
type: "channel",
maxResults: "5",
}).toString()
);
if (!result.ok) {
console.warn(`Failed to fetch YouTube channel name: ${result.status}`);
console.debug(await result.text());
return undefined;
}
const json = await result.json();
if (!is<{items: {id: string}[]}>(json)) {
const sanitizedTerm = term.replace(/\n|\r/g, "").slice(0, 100);
console.warn(`Invalid YouTube API response for ${sanitizedTerm}`);
return undefined;
}
return json.items[0].id;
}
private async getFeed(channelId: string): Promise<Parser.Output<unknown> | null> {
let feed: Parser.Output<unknown>;
const url = `https://www.youtube.com/feeds/videos.xml?channel_id=${channelId}`;
try {
feed = await this.parser.parseURL(url);
} catch (err) {
const sanitizedUrl = url.replace(/\n|\r/g, "").slice(0, 100);
console.warn(`Error while fetching RSS feed from ${sanitizedUrl}: ${err}`);
return null;
}
if (!feed || feed.items.length === 0) {
return null;
}
return feed;
}
private parseEntry(entry: Parser.Item): ParsedEntry {
return {
url: entry.link!,
title: entry.title!,
pubDate: entry.isoDate!,
entryId: entry.id!,
author: entry.author!,
channel: entry.author!,
image: entry.mediaThumbnail?.url || null,
imageAlt: null,
postText: entry.contentSnippet || null,
postDescription: null,
};
}
}