From e32bce7249d5d4959f01bbeff8301e6300b5caff Mon Sep 17 00:00:00 2001 From: Saahil Date: Sat, 12 Oct 2024 23:30:33 -0400 Subject: [PATCH] update(project): finish up + ready for prod Signed-off-by: Saahil --- hackclub-spotify-bot/TODO.md | 6 ++-- hackclub-spotify-bot/src/index.js | 32 +++++++++++++++++++- hackclub-spotify-bot/src/public/hackclub.css | 9 ++++++ hackclub-spotify-bot/src/views/home.ejs | 21 +++++++++---- 4 files changed, 58 insertions(+), 10 deletions(-) diff --git a/hackclub-spotify-bot/TODO.md b/hackclub-spotify-bot/TODO.md index 7bf6c50..d3c0890 100644 --- a/hackclub-spotify-bot/TODO.md +++ b/hackclub-spotify-bot/TODO.md @@ -4,12 +4,12 @@ - - [x] oauth [docs](https://tools.slack.dev/node-slack-sdk/oauth) - - [ ] fix the [channel](https://app.slack.com/client/T0266FRGM/C07RE4N7S4B) - - - [x] Add ping for new-song event & send message ovs -- [ ] Spotify +- [x] Spotify - - [x] web api [docs](https://developer.spotify.com/documentation/web-api/) - - [x] oauth [docs](https://developer.spotify.com/documentation/general/guides/authorization-guide/) - - [x] refresh token [docs](https://developer.spotify.com/documentation/general/guides/authorization-guide/#refresh-an-access-token) - - [x] playlist tools (creation,modifcation,deletion) - [x] keydb (quick db or smthing) - [x] express -- [ ] transparency of added songs -- - [ ] export db -> into csv with properties (slack_id, url, added_at) +- [x] transparency of added songs +- - [x] export db -> into csv with properties (slack_id, url, added_at) diff --git a/hackclub-spotify-bot/src/index.js b/hackclub-spotify-bot/src/index.js index 98dca8d..be64aaf 100644 --- a/hackclub-spotify-bot/src/index.js +++ b/hackclub-spotify-bot/src/index.js @@ -17,6 +17,17 @@ const { QuickDB } = require("quick.db"); const db = new QuickDB({ filePath: "./data/songs.sqlite", }); + +function arrayToCsv(data){ + return data.map(row => + row + .map(String) // convert every value to String + .map(v => v.replaceAll('"', '""')) // escape double quotes + .map(v => `"${v}"`) // quote it + .join(',') // comma-separated + ).join('\r\n'); // rows starting on new lines +} + let cacheDb = {}; const app = express(); const userScopes = ["identity.avatar", "identity.basic", "identity.team"]; @@ -69,7 +80,7 @@ app.use( path: path.join(__dirname, "../data/sessions"), }), saveUninitialized: true, - cookie: { secure: "auto", maxAge: 1000 * 60 * 60 * 24 * 365 }, + cookie: { secure: "auto", maxAge: 1000 * 60 * 60 * 24 * 7 }, }), ); @@ -157,6 +168,18 @@ app.get("/home", async (req, res) => { s: req.query.s, }); }); +app.get('/download/db', async (req,res) => { + if(!req.session.info) return res.redirect("/login") + const allSongs = await db.all(); +const csvData = arrayToCsv([["slack_id", "url","song_id", "added_at"], +...allSongs.map(d => { + return [d.value.added_by, d.value.song_url, d.id, d.value.added_at] +}) +]) +res.setHeader('Content-Type', 'text/csv'); +res.setHeader('Content-Disposition', 'attachment; filename="songs.csv"'); +res.send(csvData); +}) app.post("/spotify/submitsong", async (req, res) => { if (!req.session.token) return res.redirect("/login"); if (!cacheDb[req.query.token]) return res.redirect(`/home?error=0`); @@ -230,3 +253,10 @@ app.listen(process.env.PORT || 3000, async () => { // if(!await db.has()) if (getCredentials() !== null) refreshToken(getCredentials().refresh_token); }); + +process.on('uncaughtException', function(err) { + console.log(err); +}); +process.on('unhandledRejection', function(err) { + console.log(err); +}); \ No newline at end of file diff --git a/hackclub-spotify-bot/src/public/hackclub.css b/hackclub-spotify-bot/src/public/hackclub.css index feb83cb..ca397f2 100644 --- a/hackclub-spotify-bot/src/public/hackclub.css +++ b/hackclub-spotify-bot/src/public/hackclub.css @@ -633,3 +633,12 @@ td { max-width: var(--size-narrow-plus); } } +/* custom additions */ +.error { + color: var(--red); + font-weight: bold; +} +.success { + color: var(--green); + font-weight: bold; +} \ No newline at end of file diff --git a/hackclub-spotify-bot/src/views/home.ejs b/hackclub-spotify-bot/src/views/home.ejs index e3d4601..54fba58 100644 --- a/hackclub-spotify-bot/src/views/home.ejs +++ b/hackclub-spotify-bot/src/views/home.ejs @@ -23,18 +23,21 @@

Submit Song

-
+
- +
<% if (s) { %> + +

Success!

-

Your song has been added to the playlist!

+

Your song has been added to the playlist!

+

refreshing page in 5s


<% } %> @@ -46,16 +49,22 @@ <% } %>
- +

-