[READ-ONLY] Mirror of https://github.com/probablykasper/yt-email-notifier. macOS menubar app for YouTube upload notification emails
email menubar notifications tray youtube
0

Configure Feed

Select the types of activity you want to include in your feed.

save/load settings from file

Kasper (Aug 26, 2020, 1:18 AM +0200) 681966a8 70634c1c

+66 -47
+1
.gitignore
··· 1 1 dist 2 + appfiles
+1 -1
package.json
··· 7 7 "scripts": { 8 8 "lint": "eslint --ext .js --ignore-path .eslintignore .", 9 9 "lint-fix": "eslint --ext .js --ignore-path .eslintignore . --fix", 10 - "start": "node main.js", 10 + "start": "APP_ENV=dev node main.js", 11 11 "compile": "pkg . --target macos --output dist/youtube-email-notifier", 12 12 "build": "pkg . --target macos --output dist/youtube-email-notifier && embler", 13 13 "pack": "embler"
+15
src/paths.js
··· 1 + const fs = require('fs') 2 + const path = require('path') 3 + 4 + const paths = {} 5 + module.exports = paths 6 + 7 + if (process.env.APP_ENV === 'dev') { 8 + paths.dataDir = path.join(__dirname, '../appfiles/space.kasper.youtube-email-notifier') 9 + paths.settings = path.join(__dirname, '../appfiles/space.kasper.youtube-email-notifier/settings.json') 10 + } else { 11 + paths.dataDir = path.join(__dirname, '~/Library/Application Support/space.kasper.youtube-email-notifier') 12 + paths.settings = path.resolve(paths.dataDir, 'settings.json') 13 + } 14 + 15 + if (!fs.existsSync(paths.dataDir)) fs.mkdirSync(paths.dataDir, { recursive: true })
+49 -46
src/server.js
··· 1 1 const http = require('http') 2 + const fs = require('fs') 2 3 const connect = require('connect') 3 4 const serveStatic = require('serve-static') 4 5 const WebSocket = require('ws') 5 6 const fetch = require('node-fetch') 7 + const paths = require('./paths.js') 6 8 7 9 const app = connect() 8 10 app.use(serveStatic('src/web')) 9 11 let server 10 12 11 - let store = { 12 - apiKey: '', 13 - instances: [ 14 - { 15 - email: 'test@example.com', 16 - lastSyncedAt: 1597870637076, 17 - minutesBetweenRefreshes: 60, 18 - channels: [], 19 - }, 20 - { 21 - email: 'test2@example.com', 22 - lastSyncedAt: 1597870637076, 23 - minutesBetweenRefreshes: 60*5, 24 - channels: [ 25 - { 26 - icon: '', 27 - id: '', 28 - uploadsPlaylistId: '', 29 - name: 'Monstercat', 30 - }, 31 - { 32 - icon: '', 33 - id: '', 34 - uploadsPlaylistId: '', 35 - name: 'Bass Nation', 36 - }, 37 - { 38 - icon: '', 39 - id: '', 40 - uploadsPlaylistId: '', 41 - name: 'Valiant', 42 - }, 43 - ], 44 - }, 45 - ], 13 + let store 14 + if (fs.existsSync(paths.settings)) { 15 + store = JSON.parse(fs.readFileSync(paths.settings, 'utf8')) 16 + } else { 17 + store = { 18 + apiKey: '', 19 + instances: [ 20 + { 21 + email: 'test@example.com', 22 + lastSyncedAt: 1597870637076, 23 + minutesBetweenRefreshes: 60, 24 + channels: [], 25 + }, 26 + { 27 + email: 'test2@example.com', 28 + lastSyncedAt: 1597870637076, 29 + minutesBetweenRefreshes: 60*5, 30 + channels: [ 31 + { 32 + icon: '', 33 + id: '', 34 + uploadsPlaylistId: '', 35 + name: 'Monstercat', 36 + }, 37 + { 38 + icon: '', 39 + id: '', 40 + uploadsPlaylistId: '', 41 + name: 'Bass Nation', 42 + }, 43 + { 44 + icon: '', 45 + id: '', 46 + uploadsPlaylistId: '', 47 + name: 'Valiant', 48 + }, 49 + ], 50 + }, 51 + ], 52 + } 46 53 } 47 54 48 55 async function fetchYT(options) { ··· 64 71 const wss = new WebSocket.Server({ noServer: true }) 65 72 let connection = null 66 73 67 - function sendAll(type, data) { 68 - wss.clients.forEach(function each(ws) { 69 - if (ws.readyState === WebSocket.OPEN) { 70 - ws.send(JSON.stringify({ type, data })) 71 - } 72 - }) 73 - } 74 - 75 74 wss.on('connection', async (ws) => { 76 75 function send(type, data) { 77 76 ws.send(JSON.stringify({ type, data })) 77 + } 78 + function storeUpdate() { 79 + fs.writeFileSync(paths.settings, JSON.stringify(store, null, ' ')) 80 + send('newStore', store) 78 81 } 79 82 80 83 if (connection) { ··· 105 108 try { 106 109 if (type === 'setApiKey') { 107 110 store.apiKey = data.key 108 - sendAll('newStore', store) 111 + storeUpdate() 109 112 110 113 } else if (type === 'newEmail') { 111 114 if (!data.channels) data.channels = [] 112 115 if (!data.lastSyncedAt) data.lastSyncedAt = new Date().getTime() 113 116 store.instances.push(data) 114 - sendAll('newStore', store) 117 + storeUpdate() 115 118 116 119 } else if (type === 'addChannel') { 117 120 const segments = data.channel.split('/') ··· 142 145 uploadsPlaylistId: channelObject.contentDetails.relatedPlaylists.uploads, 143 146 addedAt: new Date().getTime(), 144 147 }) 145 - sendAll('newStore', store) 148 + storeUpdate() 146 149 } 147 150 } catch(err) { 148 151 console.log(':::err:::')