youtube_crawler.js
5.98 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* YouTube 热门视频爬虫
* 支持 API 和 yt-dlp 降级方案
*/
const { execSync } = require('child_process');
/**
* 使用 YouTube Data API v3 获取热门视频
*/
async function getTrendingVideosAPI({ regionCode = 'US', maxResults = 10, videoCategoryId = null, apiKey }) {
if (!apiKey) {
throw new Error('API key is required');
}
const url = 'https://www.googleapis.com/youtube/v3/videos';
const params = new URLSearchParams({
part: 'snippet,statistics',
chart: 'mostPopular',
regionCode,
maxResults: Math.min(Math.max(1, maxResults), 50).toString(),
key: apiKey
});
if (videoCategoryId) {
params.append('videoCategoryId', videoCategoryId);
}
const response = await fetch(`${url}?${params}`);
if (!response.ok) {
const error = await response.json().catch(() => ({}));
throw new Error(`API error ${response.status}: ${error.error?.message || response.statusText}`);
}
const data = await response.json();
return data.items.map(item => ({
videoId: item.id,
url: `https://youtube.com/watch?v=${item.id}`,
title: item.snippet.title,
channelTitle: item.snippet.channelTitle,
viewCount: parseInt(item.statistics.viewCount, 10) || 0,
thumbnail: item.snippet.thumbnails?.high?.url
|| item.snippet.thumbnails?.medium?.url
|| item.snippet.thumbnails?.default?.url,
publishedAt: item.snippet.publishedAt
}));
}
/**
* 使用 yt-dlp 获取热门视频 (降级方案)
*/
async function getTrendingVideosYTDLP({ regionCode = 'US', maxResults = 10 }) {
const url = regionCode === 'US'
? 'https://www.youtube.com/feed/trending'
: `https://www.youtube.com/feed/trending?gl=${regionCode}`;
const cmd = `yt-dlp --flat-playlist --playlist-end ${maxResults} -J "${url}" 2>/dev/null`;
try {
const output = execSync(cmd, { encoding: 'utf-8', timeout: 30000 });
const data = JSON.parse(output);
if (!data.entries || !Array.isArray(data.entries)) {
throw new Error('Invalid yt-dlp response format');
}
return data.entries.map(entry => ({
videoId: entry.id,
url: entry.webpage_url || `https://youtube.com/watch?v=${entry.id}`,
title: entry.title || 'Unknown',
channelTitle: entry.uploader || entry.channel || 'Unknown',
viewCount: entry.view_count || 0,
thumbnail: entry.thumbnail || null,
publishedAt: entry.upload_date
? `${entry.upload_date.slice(0, 4)}-${entry.upload_date.slice(4, 6)}-${entry.upload_date.slice(6, 8)}T00:00:00Z`
: null
}));
} catch (error) {
throw new Error(`yt-dlp failed: ${error.message}`);
}
}
/**
* 验证 regionCode 格式
*/
function validateRegionCode(code) {
if (!code || typeof code !== 'string') return false;
return /^[A-Z]{2}$/.test(code.toUpperCase());
}
/**
* 生成 Mock 数据(用于测试和演示)
*/
function getMockTrendingVideos({ regionCode = 'US', maxResults = 10 } = {}) {
const baseVideos = [
{
videoId: 'mock_001',
url: 'https://youtube.com/watch?v=mock_001',
title: 'Mock Trending Video: Global Hits 2024',
channelTitle: 'Mock Music Channel',
viewCount: 12500000,
thumbnail: 'https://img.youtube.com/vi/mock_001/hqdefault.jpg',
publishedAt: '2024-03-15T10:00:00Z'
},
{
videoId: 'mock_002',
url: 'https://youtube.com/watch?v=mock_002',
title: 'Mock Gaming: Speedrun World Record',
channelTitle: 'Mock Gaming',
viewCount: 3200000,
thumbnail: 'https://img.youtube.com/vi/mock_002/hqdefault.jpg',
publishedAt: '2024-03-14T08:30:00Z'
},
{
videoId: 'mock_003',
url: 'https://youtube.com/watch?v=mock_003',
title: 'Mock Tech Review: Latest Smartphone',
channelTitle: 'Mock Tech',
viewCount: 1800000,
thumbnail: 'https://img.youtube.com/vi/mock_003/hqdefault.jpg',
publishedAt: '2024-03-13T14:00:00Z'
},
{
videoId: 'mock_004',
url: 'https://youtube.com/watch?v=mock_004',
title: 'Mock Comedy: Stand-up Special',
channelTitle: 'Mock Comedy Central',
viewCount: 5400000,
thumbnail: 'https://img.youtube.com/vi/mock_004/hqdefault.jpg',
publishedAt: '2024-03-12T20:00:00Z'
},
{
videoId: 'mock_005',
url: 'https://youtube.com/watch?v=mock_005',
title: 'Mock Education: How Quantum Computers Work',
channelTitle: 'Mock Education',
viewCount: 890000,
thumbnail: 'https://img.youtube.com/vi/mock_005/hqdefault.jpg',
publishedAt: '2024-03-11T09:00:00Z'
}
];
return baseVideos.slice(0, maxResults).map(v => ({
...v,
_mockRegion: regionCode.toUpperCase()
}));
}
/**
* 统一入口 - 获取 YouTube 热门视频
*/
async function getTrendingVideos(options = {}) {
const {
regionCode = 'US',
maxResults = 10,
videoCategoryId = null,
useApi = true,
apiKey = process.env.YOUTUBE_API_KEY,
mock = false
} = options;
// 输入验证
if (!validateRegionCode(regionCode)) {
throw new Error(`Invalid regionCode: ${regionCode}. Must be 2-letter ISO code.`);
}
if (maxResults < 1 || maxResults > 50) {
throw new Error(`Invalid maxResults: ${maxResults}. Must be between 1 and 50.`);
}
const normalizedRegion = regionCode.toUpperCase();
// Mock 模式(用于测试和开发演示)
if (mock || process.env.YOUTUBE_CRAWLER_MOCK === '1') {
return getMockTrendingVideos({ regionCode: normalizedRegion, maxResults });
}
// 优先尝试 API
if (useApi && apiKey) {
try {
return await getTrendingVideosAPI({
regionCode: normalizedRegion,
maxResults,
videoCategoryId,
apiKey
});
} catch (error) {
console.warn(`[youtube-crawler] API failed: ${error.message}`);
console.warn('[youtube-crawler] Falling back to yt-dlp...');
}
}
// 降级到 yt-dlp
return await getTrendingVideosYTDLP({ regionCode: normalizedRegion, maxResults });
}
module.exports = {
getTrendingVideos,
getTrendingVideosAPI,
getTrendingVideosYTDLP,
getMockTrendingVideos,
validateRegionCode
};