mirror of
https://github.com/toast-ts/Daggerbot-TS.git
synced 2024-11-17 12:21:00 -05:00
YouTube no longer has videos.xml
page
This commit is contained in:
parent
0bcd398d0e
commit
1558e53cdc
@ -1,5 +1,6 @@
|
|||||||
import {TextChannel} from 'discord.js';
|
import {TextChannel} from 'discord.js';
|
||||||
import TClient from '../client.js';
|
import TClient from '../client.js';
|
||||||
|
import TSClient from '../helpers/TSClient.js';
|
||||||
import Logger from '../helpers/Logger.js';
|
import Logger from '../helpers/Logger.js';
|
||||||
import CacheServer from './CacheServer.js';
|
import CacheServer from './CacheServer.js';
|
||||||
import MessageTool from '../helpers/MessageTool.js';
|
import MessageTool from '../helpers/MessageTool.js';
|
||||||
@ -8,27 +9,29 @@ export default async(client:TClient, YTChannelID:string, YTChannelName:string, D
|
|||||||
let Data: any;
|
let Data: any;
|
||||||
let cacheExpiry: number = 7200; // Invalidate cache after sitting in Redis for 2 hours
|
let cacheExpiry: number = 7200; // Invalidate cache after sitting in Redis for 2 hours
|
||||||
try {
|
try {
|
||||||
await fetch(`https://www.youtube.com/feeds/videos.xml?channel_id=${YTChannelID}`, {
|
await fetch(`https://youtube.googleapis.com/youtube/v3/activities?part=snippet&channelId=${YTChannelID}&maxResults=2&key=${(await TSClient.Token()).youtube}`, {
|
||||||
signal: AbortSignal.timeout(10000),
|
signal: AbortSignal.timeout(10000),
|
||||||
headers: {'User-Agent':'Daggerbot - Notification/undici'},
|
headers: {'User-Agent':'Daggerbot - Notification/undici'},
|
||||||
}).then(async xml=>(Data = new client.fxp.XMLParser({ignoreAttributes: false, transformAttributeName(attributeName){return attributeName.replaceAll('@_','')}}).parse(await xml.text()) as any));
|
}).then(async json=>Data = await json.json());
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Logger.forwardToConsole('log', 'YTModule', `Failed to fetch "${YTChannelName}" from YouTube`);
|
Logger.forwardToConsole('log', 'YTModule', `Failed to fetch "${YTChannelName}" from YouTube`);
|
||||||
}
|
}
|
||||||
if (!Data) return;
|
if (!Data) return;
|
||||||
|
const getVideoId = (index:number)=>Data.items[index].snippet.thumbnails.default.url.split('/')[4];
|
||||||
|
const videoUrl = `https://www.youtube.com/watch?v=${getVideoId(0)}`;
|
||||||
const cacheKey = `YTCache:${YTChannelID}`;
|
const cacheKey = `YTCache:${YTChannelID}`;
|
||||||
const cachedVideoId = await CacheServer.get(cacheKey);
|
const cachedVideoId = await CacheServer.get(cacheKey);
|
||||||
if (!cachedVideoId) {
|
if (!cachedVideoId) {
|
||||||
await CacheServer.set(cacheKey, Data.feed.entry[0]['yt:videoId']).then(async()=>await CacheServer.expiry(cacheKey, cacheExpiry));
|
await CacheServer.set(cacheKey, getVideoId(0)).then(async()=>await CacheServer.expiry(cacheKey, cacheExpiry));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Data.feed.entry[1]['yt:videoId'] === cachedVideoId) {
|
if (getVideoId(1) === cachedVideoId) {
|
||||||
await CacheServer.delete(cacheKey).then(async()=>{
|
await CacheServer.delete(cacheKey).then(async()=>{
|
||||||
await CacheServer.set(cacheKey, Data.feed.entry[0]['yt:videoId']).then(async()=>await CacheServer.expiry(cacheKey, cacheExpiry))
|
await CacheServer.set(cacheKey, getVideoId(0)).then(async()=>await CacheServer.expiry(cacheKey, cacheExpiry))
|
||||||
});
|
});
|
||||||
|
|
||||||
(client.channels.resolve(DiscordChannelID) as TextChannel).send({
|
(client.channels.resolve(DiscordChannelID) as TextChannel).send({
|
||||||
content: `${MessageTool.formatMention(DiscordRoleID, 'role')}\n**${YTChannelName}** just uploaded a video!\n${Data.feed.entry[0].link.href}`,
|
content: `${MessageTool.formatMention(DiscordRoleID, 'role')}\n**${YTChannelName}** just uploaded a video!\n${videoUrl}`,
|
||||||
allowedMentions: {parse:['roles']},
|
allowedMentions: {parse:['roles']},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user