Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion config.sample.json → config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"https://www.youtube.com/channel/UCNuYih-JMtLxOlsjeIos4ZA"
],
"youtubeKey": "your http://console.developer.google.com youtube v3 api key"
}
}
65 changes: 41 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
const config = require("./config.json"),
Discord = require("discord.js"),
Parser = require("rss-parser"),
const Parser = require("rss-parser"),
parser = new Parser(),
Youtube = require("simple-youtube-api"),
youtube = new Youtube(config.youtubeKey);
Youtube = require("simple-youtube-api");

class list {
constructor(client, token) {
this.token = token.youtubekey;
this.channel = token.channel;
this.dischannel = token.discordchannel;
this.msg = token.discordmsg;
this.client = client;
this.client.on("ready", () => {
console.log("🎉[yt update notification now ready]🎉");
this.apiPost();
});
}

async apiPost() {

if(!this.client) return console.log("make sure check out your code and install client");
const youtube = new Youtube(this.token);

const startAt = Date.now();
const lastVideos = {};

const client = new Discord.Client();
client.login(config.token).catch(console.log);
const client = this.client;

client.on("ready", () => {
console.log(`[!] Ready to listen ${config.youtubers.length} youtubers!`);
//console.log(`[!] Ready to listen ${this.channel.length} youtubers!`);
check();
setInterval(check, 20*1000);
setInterval(check, 30);
});

/**
Expand All @@ -34,15 +48,15 @@ function formatDate(date) {
* @returns The last video of the youtuber
*/
async function getLastVideo(youtubeChannelName, rssURL){
console.log(`[${youtubeChannelName}] | Getting videos...`);
//console.log(`[${youtubeChannelName}] | Getting videos...`);
let content = await parser.parseURL(rssURL);
console.log(`[${youtubeChannelName}] | ${content.items.length} videos found`);
// console.log(`[${youtubeChannelName}] | ${content.items.length} videos found`);
let tLastVideos = content.items.sort((a, b) => {
let aPubDate = new Date(a.pubDate || 0).getTime();
let bPubDate = new Date(b.pubDate || 0).getTime();
return bPubDate - aPubDate;
});
console.log(`[${youtubeChannelName}] | The last video is "${tLastVideos[0] ? tLastVideos[0].title : "err"}"`);
// console.log(`[${youtubeChannelName}] | The last video is "${tLastVideos[0] ? tLastVideos[0].title : "err"}"`);
return tLastVideos[0];
}

Expand All @@ -53,15 +67,15 @@ async function getLastVideo(youtubeChannelName, rssURL){
* @returns The video || null
*/
async function checkVideos(youtubeChannelName, rssURL){
console.log(`[${youtubeChannelName}] | Get the last video..`);
// console.log(`[${youtubeChannelName}] | Get the last video..`);
let lastVideo = await getLastVideo(youtubeChannelName, rssURL);
// If there isn't any video in the youtube channel, return
if(!lastVideo) return console.log("[ERR] | No video found for "+lastVideo);
// if(!lastVideo) return console.log("[ERR] | No video found for "+lastVideo);
// If the date of the last uploaded video is older than the date of the bot starts, return
if(new Date(lastVideo.pubDate).getTime() < startAt) return console.log(`[${youtubeChannelName}] | Last video was uploaded before the bot starts`);
if(new Date(lastVideo.pubDate).getTime() < startAt) return; //console.log(`[${youtubeChannelName}] | Last video was uploaded before the bot starts`);
let lastSavedVideo = lastVideos[youtubeChannelName];
// If the last video is the same as the last saved, return
if(lastSavedVideo && (lastSavedVideo.id === lastVideo.id)) return console.log(`[${youtubeChannelName}] | Last video is the same as the last saved`);
if(lastSavedVideo && (lastSavedVideo.id === lastVideo.id)) //console.log(`[${youtubeChannelName}] | Last video is the same as the last saved`);
return lastVideo;
}

Expand Down Expand Up @@ -107,23 +121,26 @@ async function getYoutubeChannelInfos(name){
* Check for new videos
*/
async function check(){
console.log("Checking...");
config.youtubers.forEach(async (youtuber) => {
// console.log("Checking...");
this.channel.forEach(async (youtuber) => {
console.log(`[${youtuber.length >= 10 ? youtuber.slice(0, 10)+"..." : youtuber}] | Start checking...`);
let channelInfos = await getYoutubeChannelInfos(youtuber);
if(!channelInfos) return console.log("[ERR] | Invalid youtuber provided: "+youtuber);
if(!channelInfos) return ;//console.log("[ERR] | Invalid youtuber provided: "+youtuber);
let video = await checkVideos(channelInfos.raw.snippet.title, "https://www.youtube.com/feeds/videos.xml?channel_id="+channelInfos.id);
if(!video) return console.log(`[${channelInfos.raw.snippet.title}] | No notification`);
let channel = client.channels.get(config.channel);
if(!channel) return console.log("[ERR] | Channel not found");
if(!video) return;//console.log(`[${channelInfos.raw.snippet.title}] | No notification`);
let channel = client.channels.get(this.dischannel);
if(!channel) return;//console.log("[ERR] | Channel not found");
channel.send(
config.message
this.msg
.replace("{videoURL}", video.link)
.replace("{videoAuthorName}", video.author)
.replace("{videoTitle}", video.title)
.replace("{videoPubDate}", formatDate(new Date(video.pubDate)))
);
console.log("Notification sent !");
console.log("Notification sent!");
lastVideos[channelInfos.raw.snippet.title] = video;
});
}
}
}
module.exports = list;
18 changes: 5 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
{
"name": "discordyoutubenotifier",
"version": "1.0.0",
"name": "ytnoti",
"version": "1.0.0-dev",
"description": "Be notified when one of your favorite youtubers posts a video! ",
"main": "index.js",
"scripts": {
"scripts": {
"start": "node index.js",
"test": "node index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Androz2091/DiscordYoutubeNotifier.git"
},
"author": "Androz2091",
"author": "namekinggamerboy",
"license": "ISC",
"bugs": {
"url": "https://github.com/Androz2091/DiscordYoutubeNotifier/issues"
},
"homepage": "https://github.com/Androz2091/DiscordYoutubeNotifier#readme",
"dependencies": {
"discord.js": "^11.5.1",
"rss-parser": "^3.7.2",
"simple-youtube-api": "^5.2.1"
}
Expand Down
57 changes: 57 additions & 0 deletions shrinkwrap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
dependencies:
rss-parser: 3.8.0
simple-youtube-api: 5.2.1
packages:
/entities/1.1.2:
dev: false
resolution:
integrity: sha512-f2LZMYl1Fzu7YSBKg+RoROelpOaNrcGmE9AZubeDfrCEia483oW4MI4VyFd5VNHIgQ/7qm1I0wUHK1eJnn2y2w==
/iso8601-duration/1.2.0:
dev: false
resolution:
integrity: sha512-ErTBd++b17E8nmWII1K1uZtBgD1E8RjyvwmxlCjPHNqHMD7gmcMHOw0E8Ro/6+QT4PhHRSnnMo7bxa1vFPkwhg==
/node-fetch/2.6.0:
dev: false
engines:
node: 4.x || >=6.0.0
resolution:
integrity: sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==
/rss-parser/3.8.0:
dependencies:
entities: 1.1.2
xml2js: 0.4.23
dev: false
resolution:
integrity: sha512-EvjM0r3IymXnk+SCUawLhdH36D2WQCwpEfIyvu4hpPswYuMSk/jlCLOcg24OvXwVdcJcEocAlTZVjGNjOOSJXg==
/sax/1.2.4:
dev: false
resolution:
integrity: sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==
/simple-youtube-api/5.2.1:
dependencies:
iso8601-duration: 1.2.0
node-fetch: 2.6.0
dev: false
resolution:
integrity: sha512-vmndP9Bkh35tifn2OwY+th2imSsfYtmDqczgdOW5yEARFzvSoR8VSQFsivJnctfV5QHQUL6VrOpNdbmDRLh9Bg==
/xml2js/0.4.23:
dependencies:
sax: 1.2.4
xmlbuilder: 11.0.1
dev: false
engines:
node: '>=4.0.0'
resolution:
integrity: sha512-ySPiMjM0+pLDftHgXY4By0uswI3SPKLDw/i3UXbnO8M/p28zqexCUoPmQFrYD+/1BzhGJSs2i1ERWKJAtiLrug==
/xmlbuilder/11.0.1:
dev: false
engines:
node: '>=4.0'
resolution:
integrity: sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==
registry: 'https://registry.npmjs.org/'
shrinkwrapMinorVersion: 9
shrinkwrapVersion: 3
specifiers:
rss-parser: ^3.7.2
simple-youtube-api: ^5.2.1