@livecord/notify
Advanced tools
Comparing version 1.0.7 to 1.0.8
@@ -7,8 +7,8 @@ const { Twitch } = require('../src/index'); | ||
}, | ||
channels: [ 'loirenn', 'clqu_' ], // Array of channels (required) | ||
interval: 1000 // check channels every 1000ms (1 second) (optional) (default: 60000 [60 seconds])]) | ||
interval: 1000, // check channels every 1000ms (1 second) (optional) (default: 60000 [60 seconds])]) | ||
useDatabase: true // use database to store videos (optional) (default: true) | ||
}); | ||
twitch.on('ready', (ready) => { | ||
console.log('Twitch connected at: ', ready); | ||
console.log('Twitch connected at: ', twitch.channels); | ||
}); | ||
@@ -15,0 +15,0 @@ |
const { YouTube } = require('../src/index'); | ||
const youtube = new YouTube({ | ||
channels: [], // Array of channels (required) | ||
interval: 1000, // check channels every 1000ms (1 second) (optional) (default: 60000 [60 seconds])]) | ||
@@ -8,5 +7,5 @@ useDatabase: true // use database to store videos (optional) (default: true) | ||
youtube.unsubscribe('UCaZGQt419Ptsdk_lh2mz9lQ'); // Subscribe to a channel | ||
youtube.on('ready', (ready) => { | ||
youtube.subscribe('UCaZGQt419Ptsdk_lh2mz9lQ'); // Subscribe to a channel | ||
console.log('Youtube connected at: ', ready); | ||
@@ -13,0 +12,0 @@ }); |
{ | ||
"name": "@livecord/notify", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "Notifier for Youtube and Twitch.", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -26,3 +26,2 @@ # [@livecord/notify](https://npmjs.com/package/@livecord/notify) | ||
}, | ||
channels: [ 'clqu_' ], // Array of channels (required) | ||
interval: 60000 // check channels every (optional) (default: 60000 [60 seconds]) | ||
@@ -32,2 +31,6 @@ }); | ||
twitch.on('ready', (ready) => { | ||
twitch.follow([ 'clqu_' ]); | ||
twitch.unfollow([ 'clqu_' ]); | ||
console.log('Twitch connected at: ', ready); | ||
@@ -61,3 +64,2 @@ }); | ||
const youtube = new YouTube({ | ||
channels: [ '' ], // Array of channel ids (required) | ||
interval: 60000, // check channels every 1000ms (1 second) (optional) (default: 60000 [60 seconds])]) | ||
@@ -67,9 +69,9 @@ useDatabase: true // use database to store videos (optional) (default: true) | ||
youtube.subscribe('UC00_j4mtyaMbWX62JWBMmWA'); // Subscribe to a another channel | ||
// For multiple: youtube.subscribe(['', '']); | ||
youtube.on('ready', (ready) => { | ||
youtube.subscribe('UC00_j4mtyaMbWX62JWBMmWA'); // Subscribe to a another channel | ||
// For multiple: youtube.subscribe(['', '']); | ||
youtube.unsubscribe('UC00_j4mtyaMbWX62JWBMmWA'); // Unsubscribe to any added channels | ||
// For multiple: youtube.unsubscribe(['', '']); | ||
youtube.unsubscribe('UC00_j4mtyaMbWX62JWBMmWA'); // Unsubscribe to any added channels | ||
// For multiple: youtube.unsubscribe(['', '']); | ||
youtube.on('ready', (ready) => { | ||
console.log('Youtube connected at: ', ready); | ||
@@ -76,0 +78,0 @@ }); |
const EventEmitter = require('events'); | ||
const WebSocket = require('ws'); | ||
const axios = require('axios'); | ||
const fs = require('fs'); | ||
module.exports = class LivecordTwitch extends EventEmitter { | ||
constructor({ client, channels, interval = 60000 }) { | ||
constructor({ client, interval = 60000, useDatabase = true }) { | ||
super(); | ||
@@ -16,4 +17,3 @@ | ||
if (interval && typeof interval !== 'number') throw new Error('LivecordTwitch: interval must be a number'); | ||
if (!channels) throw new Error('LivecordTwitch: You must provide a list of channels.'); | ||
if (!Array.isArray(channels)) throw new Error('LivecordTwitch: channels must be an array'); | ||
if (useDatabase && typeof useDatabase !== 'boolean') throw new Error('LivecordTwitch: useDatabase must be a boolean'); | ||
@@ -27,8 +27,19 @@ try { | ||
}; | ||
this.channels = channels; | ||
this.channels = []; | ||
this.lives = []; | ||
this.useDatabase = useDatabase; | ||
if (useDatabase) { | ||
if (fs.existsSync('./livecord-twitch.json')) { | ||
let db = JSON.parse(fs.readFileSync('livecord-twitch.json')); | ||
db?.channels?.forEach(channel => { | ||
if (!this.channels.includes(channel)) this.channels.push(channel); | ||
}); | ||
} else { | ||
fs.writeFileSync('livecord-twitch.json', JSON.stringify({ channels: [] }, 2, null)); | ||
} | ||
} | ||
this.ws.on('open', () => { | ||
this.emit('ready', Date.now()); | ||
setInterval(() => { | ||
channels.forEach(async channel => { | ||
this.channels.forEach(async channel => { | ||
const request = await axios.request({ | ||
@@ -64,2 +75,68 @@ method: "GET", | ||
}; | ||
follow(channel) { | ||
if (!channel) throw new Error('LivecordTwitch: channel is required'); | ||
if (typeof channel === 'string') { | ||
if (this.channels.includes(channel)) return; | ||
if (this.useDatabase) { | ||
fs.writeFileSync('livecord-twitch.json', JSON.stringify({ | ||
channels: [ | ||
...JSON.parse(fs.readFileSync('livecord-twitch.json'))?.channels, | ||
channel | ||
], | ||
}, 2, null)); | ||
return this.channels.push(channel); | ||
} else { | ||
return this.channels.push(channel); | ||
} | ||
} else if (Array.isArray(channel)) { | ||
channel.forEach(c => { | ||
if (this.channels.includes(c)) return; | ||
if (this.useDatabase) { | ||
fs.writeFileSync('livecord-twitch.json', JSON.stringify({ | ||
channels: [ | ||
...JSON.parse(fs.readFileSync('livecord-twitch.json'))?.channels, | ||
c | ||
], | ||
}, 2, null)); | ||
return this.channels.push(c); | ||
} else { | ||
return this.channels.push(c); | ||
} | ||
}) | ||
} else { | ||
throw new Error('LivecordTwitch: channel must be a string or array'); | ||
} | ||
} | ||
unfollow(channel) { | ||
if (!channel) throw new Error('LivecordTwitch: channel is required'); | ||
if (typeof channel === 'string') { | ||
if (!this.channels.includes(channel)) return; | ||
if (this.useDatabase) { | ||
this.channels = this.channels.filter(el => el !== channel); | ||
return fs.writeFileSync('livecord-twitch.json', JSON.stringify({ | ||
channels: [ | ||
...JSON.parse(fs.readFileSync('livecord-twitch.json'))?.channels?.filter(el => el !== channel) | ||
], | ||
}, 2, null)); | ||
} else { | ||
return this.channels = this.channels.filter(el => el !== channel); | ||
} | ||
} else if (Array.isArray(channel)) { | ||
channel.forEach(c => { | ||
if (!this.channels.includes(c)) return; | ||
if (this.useDatabase) { | ||
this.channels = this.channels.filter(el => el !== c); | ||
return fs.writeFileSync('livecord-twitch.json', JSON.stringify({ | ||
channels: [ | ||
...JSON.parse(fs.readFileSync('livecord-twitch.json'))?.channels?.filter(el => el !== c) | ||
], | ||
}, null, 2)); | ||
} else { | ||
return this.channels = this.channels.filter(el => el !== c); | ||
} | ||
}) | ||
} else { | ||
throw new Error('LivecordTwitch: channel must be a string or array'); | ||
} | ||
} | ||
}; | ||
@@ -66,0 +143,0 @@ |
@@ -8,3 +8,3 @@ const EventEmitter = require('events'); | ||
module.exports = class LivecordYoutube extends EventEmitter { | ||
constructor({ postedVideos = [], channels, interval = 60000, useDatabase = true }) { | ||
constructor({ postedVideos = [], interval = 60000, useDatabase = true }) { | ||
super(); | ||
@@ -16,4 +16,2 @@ | ||
if (useDatabase && typeof useDatabase !== 'boolean') throw new Error('LivecordYoutube: useDatabase must be a boolean'); | ||
if (!channels) throw new Error('LivecordYoutube: You must provide a list of channels.'); | ||
if (!Array.isArray(channels)) throw new Error('LivecordYoutube: channels must be an array'); | ||
@@ -27,3 +25,3 @@ try { | ||
}; | ||
this.channels = channels; | ||
this.channels = []; | ||
this.postedVideos = postedVideos; | ||
@@ -33,4 +31,4 @@ this.useDatabase = useDatabase; | ||
if (useDatabase) { | ||
if (fs.existsSync('./livecord.json')) { | ||
let db = JSON.parse(fs.readFileSync('livecord.json')); | ||
if (fs.existsSync('./livecord-youtube.json')) { | ||
let db = JSON.parse(fs.readFileSync('livecord-youtube.json')); | ||
db?.videos?.forEach(video => { | ||
@@ -43,3 +41,3 @@ if (!this.postedVideos.includes(video)) this.postedVideos.push(video); | ||
} else { | ||
fs.writeFileSync('livecord.json', JSON.stringify({ videos: [], channels: [] })); | ||
fs.writeFileSync('livecord-youtube.json', JSON.stringify({ videos: [], channels: [] }, null, 2)); | ||
} | ||
@@ -50,3 +48,3 @@ } | ||
setInterval(() => { | ||
channels.forEach(async channel => { | ||
this.channels.forEach(async channel => { | ||
parser.parseURL(`https://www.youtube.com/feeds/videos.xml?channel_id=${channel}`) | ||
@@ -57,11 +55,11 @@ .then(data => { | ||
if (postedVideos.includes($.id)) return; | ||
fs.writeFileSync('livecord.json', JSON.stringify({ | ||
fs.writeFileSync('livecord-youtube.json', JSON.stringify({ | ||
videos: [ | ||
...JSON.parse(fs.readFileSync('livecord.json'))?.videos, | ||
...JSON.parse(fs.readFileSync('livecord-youtube.json'))?.youtube?.videos, | ||
$.id | ||
], | ||
channels: [ | ||
...JSON.parse(fs.readFileSync('livecord.json'))?.channels, | ||
...JSON.parse(fs.readFileSync('livecord-youtube.json'))?.youtube?.channels | ||
] | ||
})); | ||
}, 2, null)); | ||
this.postedVideos.push($.id); | ||
@@ -88,11 +86,11 @@ return this.emit('upload', $); | ||
if (this.useDatabase) { | ||
fs.writeFileSync('livecord.json', JSON.stringify({ | ||
fs.writeFileSync('livecord-youtube.json', JSON.stringify({ | ||
videos: [ | ||
...JSON.parse(fs.readFileSync('livecord.json'))?.videos, | ||
...JSON.parse(fs.readFileSync('livecord-youtube.json'))?.youtube?.videos, | ||
], | ||
channels: [ | ||
...JSON.parse(fs.readFileSync('livecord.json'))?.channels, | ||
...JSON.parse(fs.readFileSync('livecord-youtube.json'))?.youtube?.channels, | ||
channel | ||
] | ||
})); | ||
}, 2, null)); | ||
this.channels.push(channel); | ||
@@ -108,11 +106,11 @@ return this.ws.emit('newChannel', channel); | ||
if (this.useDatabase) { | ||
fs.writeFileSync('livecord.json', JSON.stringify({ | ||
fs.writeFileSync('livecord-youtube.json', JSON.stringify({ | ||
videos: [ | ||
...JSON.parse(fs.readFileSync('livecord.json'))?.videos, | ||
...JSON.parse(fs.readFileSync('livecord-youtube.json'))?.youtube?.videos, | ||
], | ||
channels: [ | ||
...JSON.parse(fs.readFileSync('livecord.json'))?.channels, | ||
...JSON.parse(fs.readFileSync('livecord-youtube.json'))?.youtube?.channels, | ||
c | ||
] | ||
})); | ||
}, 2, null)); | ||
return this.channels.push(c); | ||
@@ -133,10 +131,10 @@ } else { | ||
this.channels = this.channels.filter(el => el !== channel); | ||
return fs.writeFileSync('livecord.json', JSON.stringify({ | ||
return fs.writeFileSync('livecord-youtube.json', JSON.stringify({ | ||
videos: [ | ||
...JSON.parse(fs.readFileSync('livecord.json'))?.videos, | ||
...JSON.parse(fs.readFileSync('livecord-youtube.json'))?.youtube?.videos, | ||
], | ||
channels: [ | ||
...JSON.parse(fs.readFileSync('livecord.json'))?.channels?.filter(el => el !== channel) | ||
...JSON.parse(fs.readFileSync('livecord-youtube.json'))?.youtube?.channels?.filter(el => el !== channel) | ||
] | ||
})); | ||
}, 2, null)); | ||
} else { | ||
@@ -150,8 +148,8 @@ return this.channels = this.channels.filter(el => el !== channel); | ||
this.channels = this.channels.filter(el => el !== c); | ||
return fs.writeFileSync('livecord.json', JSON.stringify({ | ||
return fs.writeFileSync('livecord-youtube.json', JSON.stringify({ | ||
videos: [ | ||
...JSON.parse(fs.readFileSync('livecord.json'))?.videos, | ||
...JSON.parse(fs.readFileSync('livecord-youtube.json'))?.youtube?.videos, | ||
], | ||
channels: [ | ||
...JSON.parse(fs.readFileSync('livecord.json'))?.channels?.filter(el => el !== c) | ||
...JSON.parse(fs.readFileSync('livecord-youtube.json'))?.youtube?.channels?.filter(el => el !== c) | ||
] | ||
@@ -158,0 +156,0 @@ })); |
18573
9
324
82
2