Skip to content

Commit

Permalink
First Upload
Browse files Browse the repository at this point in the history
Everything is in the readme...
  • Loading branch information
Ezzud authored Jul 14, 2023
1 parent 5bd15a8 commit 18d8055
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
115 changes: 115 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Requirements
const ytch = require('yt-channel-info')
const axios = require('axios');
const quick = require('quick.db')
const db = new quick.table("info")
const config = require("./config.json")
const package_info = require("./package.json")

// Setting-up youtube payload
const payload = {
channelId: config.youtubeChannelID,
sortBy: 'newest',
channelIdType: 0
}


// Setting-up data tables
if(!db.get("lastVideo")) {
db.set("lastVideo", "0")
}
if(!db.get("sentVideos")) {
db.set("sentVideos", [])
}



console.log(`\x1b[32mSimple Youtube Notification v${package_info.version}\x1b[0m\n`)
console.log("\x1b[33mDeveloper:\x1b[0m github.com/Ezzud")
console.log("\x1b[33mSupport:\x1b[0m discord.ezzud.fr\n")


async function sendNewVideoWebhook(data) {
axios({
method: 'post',
url: config.webhook.url,
data: data
})
.then(function (response) {
console.log(`\x1b[32m[S]\x1b[0m New video successfully sent`)
})
.catch(function (error) {
if(error) console.log("\x1b[31m[E]\x1b[0m An error occured when sending the webhook, please check the informations on config.json")
});
}



async function checkForNewVideo() {
// Fetching channel videos
let error = false
let informations = await ytch.getChannelVideos(payload).catch((err) => {
error = true
console.log("\x1b[31m[E]\x1b[0m An error occured when trying to get recent youtube videos, please check your config.json!")
})
if(error) return;

// Checking for youtube error
if(informations.alertMessage) {
console.log("\x1b[31m[E]\x1b[0m Error while getting informations:\n" + informations.alertMessage)
return;
}


// Checking videos on channel
let videos = informations.items
if(!videos || !videos[0]) {
console.log("\x1b[31m[E]\x1b[0m An error occured when trying to get recent youtube videos, the youtube channel doesn't have any videos")
return;
}

// Getting the first and last registered videos
let firstVideo = videos[0]
let oldVideo = await db.get("lastVideo")


// Check if last video is the same
if(firstVideo.videoId !== oldVideo) {

// Replacing the old last video to the new one
await db.set("lastVideo", firstVideo.videoId)
if(db.get("sentVideos").find(x => x.id === firstVideo.videoId)) {
return;
}
await db.push("sentVideos", { "id":firstVideo.videoId})

// Creating webhook info
var webhook_data = {
"username":config.webhook.username,
"avatar_url":config.webhook.avatar_url,
"content": config.webhook.content.replaceAll("%VIDEO_URL%", `https://youtube.com/watch?v=${firstVideo.videoId}`).replaceAll("%VIDEO_TITLE%", firstVideo.title)
};
console.log(`\x1b[33m[I]\x1b[0m New video found, trying to send a webhook...`)
await sendNewVideoWebhook(webhook_data);



}
}

async function start() {
checkForNewVideo();
setInterval(async() => {
checkForNewVideo();
}, 60000)
}

start();








9 changes: 9 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"youtubeChannelID":"Youtube Channel ID",
"webhook": {
"url":"Discord Webhook URL",
"username":"Youtube",
"avatar_url":"https://img.freepik.com/icones-gratuites/youtube_318-194847.jpg",
"content":"@everyone\n🔔 **New Video** `%VIDEO_TITLE%` **Uploaded!**\n🔗 %VIDEO_URL%"
}
}
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name":"simple-youtube-notification-webhook",
"version":"1.0.1",
"author":"ezzud",
"dependencies": {
"axios": "^1.4.0",
"quick.db": "^7.1.3",
"yt-channel-info": "^3.0.0"
}
}
2 changes: 2 additions & 0 deletions start.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@ECHO OFF
npm install && cls && node app.js
1 change: 1 addition & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npm install && node app.js

0 comments on commit 18d8055

Please sign in to comment.