要从JavaScript或jQuery频道获取最新的YouTube和Twitch视频URL,你需要使用这些平台的API。YouTube和Twitch都提供了官方的API服务,允许开发者获取视频信息,包括视频URL。
search
方法来获取最新的视频列表。// 示例代码:获取YouTube最新视频URL
const apiKey = 'YOUR_API_KEY';
const url = `https://www.googleapis.com/youtube/v3/search?key=${apiKey}&part=snippet&order=date&maxResults=5`;
fetch(url)
.then(response => response.json())
.then(data => {
const videos = data.items.map(item => {
return {
title: item.snippet.title,
videoId: item.id.videoId,
url: `https://www.youtube.com/watch?v=${item.id.videoId}`
};
});
console.log(videos);
})
.catch(error => console.error('Error fetching YouTube videos:', error));
streams
方法来获取最新的直播视频。// 示例代码:获取Twitch最新直播视频URL
const clientId = 'YOUR_CLIENT_ID';
const clientSecret = 'YOUR_CLIENT_SECRET';
const tokenUrl = `https://id.twitch.tv/oauth2/token?client_id=${clientId}&client_secret=${clientSecret}&grant_type=client_credentials`;
fetch(tokenUrl)
.then(response => response.json())
.then(data => {
const accessToken = data.access_token;
const streamsUrl = `https://api.twitch.tv/helix/streams?first=5&access_token=${accessToken}`;
return fetch(streamsUrl);
})
.then(response => response.json())
.then(data => {
const streams = data.data.map(stream => {
return {
title: stream.title,
streamUrl: `https://www.twitch.tv/${stream.user_login}`
};
});
console.log(streams);
})
.catch(error => console.error('Error fetching Twitch streams:', error));
通过以上步骤和示例代码,你可以从JavaScript或jQuery频道获取最新的YouTube和Twitch视频URL。
领取专属 10元无门槛券
手把手带您无忧上云