update
This commit is contained in:
8
src/pages/admin.jsx
Normal file
8
src/pages/admin.jsx
Normal file
@@ -0,0 +1,8 @@
|
||||
import dynamic from "next/dynamic"
|
||||
const App = dynamic(() => import("@/admin/App"), { ssr: false })
|
||||
|
||||
const AdminPage = () => {
|
||||
return <App />;
|
||||
};
|
||||
|
||||
export default AdminPage;
|
||||
14
src/pages/api/admin/audio_files.js
Normal file
14
src/pages/api/admin/audio_files.js
Normal file
@@ -0,0 +1,14 @@
|
||||
const fs = require('fs');
|
||||
|
||||
export default async function handler(req, res) {
|
||||
if (req.method === 'GET') {
|
||||
const files = fs.readdirSync('./public/files/episodes', {withFileTypes: true})
|
||||
.filter(item => !item.isDirectory())
|
||||
.map(item => item.name);
|
||||
files.sort();
|
||||
files.reverse();
|
||||
|
||||
res.setHeader('Content-Range', files.length);
|
||||
res.status(200).json(files.map((f, i) => { return { id: i, filename: f } }));
|
||||
}
|
||||
}
|
||||
30
src/pages/api/admin/episodes/[id].js
Normal file
30
src/pages/api/admin/episodes/[id].js
Normal file
@@ -0,0 +1,30 @@
|
||||
import db from '@/db';
|
||||
|
||||
const COLS = {};
|
||||
const COLS_PREFIXED = {};
|
||||
const COLS_LIST = ['id', 'number', 'content', 'summary', 'slug', 'season', 'episode', 'duration', 'filename', 'title', 'episode_type', 'buzzsprout_id', 'buzzsprout_url', 'pub_date', 'youtube_url', 'transcript_filename', 'audio_url'];
|
||||
COLS_LIST.forEach((k) => COLS[k] = k)
|
||||
COLS_LIST.forEach((k) => COLS_PREFIXED[k] = `$${k}`)
|
||||
|
||||
export default async function handler(req, res) {
|
||||
const sessionId = req.cookies?.session;
|
||||
if (!sessionId) { res.status(404).json({}); return; }
|
||||
const sessionRes = await db.get('select email from sessions join users on users.id = sessions.user_id where session_id=?;', sessionId);
|
||||
if (!sessionRes || sessionRes?.email != process.env.ADMIN_EMAIL) { res.status(404).json({}); return; }
|
||||
const { id } = req.query;
|
||||
if (req.method === 'GET') {
|
||||
const episode = await db.get('select * from episodes where id = ?', id);
|
||||
res.status(200).json(episode)
|
||||
} else if (req.method === 'PUT') {
|
||||
const changes = req.body;
|
||||
const changesForSQL = {};
|
||||
Object.keys(changes).forEach((k) => changesForSQL[COLS_PREFIXED[k]] = changes[k]);
|
||||
const { id } = await db.get(`update episodes set ${Object.keys(changes).map((k) => `${COLS[k]} = ${COLS_PREFIXED[k]}`).join(', ')} where id = $id returning id;`, changesForSQL);
|
||||
const episode = await db.get('select * from episodes where id = ?', id);
|
||||
res.status(200).json(episode)
|
||||
} else if (req.method = 'DELETE') {
|
||||
const episode = await db.get('select * from episodes where id = ?', id);
|
||||
await db.run('delete from episodes where id = ?', id);
|
||||
res.status(200).json(episode);
|
||||
}
|
||||
}
|
||||
42
src/pages/api/admin/episodes/index.js
Normal file
42
src/pages/api/admin/episodes/index.js
Normal file
@@ -0,0 +1,42 @@
|
||||
import db from '@/db';
|
||||
|
||||
const SORT_MAP = {
|
||||
'ASC': 'asc',
|
||||
'DESC': 'desc'
|
||||
};
|
||||
|
||||
const COLUMN_MAP = {
|
||||
'id': 'id',
|
||||
'number': 'number',
|
||||
'episode': 'episode'
|
||||
};
|
||||
|
||||
const COLS_LIST = ['number', 'content', 'summary', 'slug', 'season', 'episode', 'duration', 'filename', 'title', 'episode_type', 'buzzsprout_id', 'buzzsprout_url', 'pub_date', 'youtube_url', 'transcript_filename', 'audio_url'];
|
||||
|
||||
export default async function handler(req, res) {
|
||||
const sessionId = req.cookies?.session;
|
||||
if (!sessionId) { res.status(404).json({}); return; }
|
||||
const sessionRes = await db.get('select email from sessions join users on users.id = sessions.user_id where session_id=?;', sessionId);
|
||||
if (!sessionRes || sessionRes?.email != process.env.ADMIN_EMAIL) { res.status(404).json({}); return; }
|
||||
if (req.method === 'GET') {
|
||||
const { sort, range, filter } = req.query;
|
||||
const [sortColumn, sortDirection] = sort ? JSON.parse(sort) : [false, false];
|
||||
const [rangeStart, rangeEnd] = range ? JSON.parse(range) : [false, false];
|
||||
let rows;
|
||||
if (sort && range) {
|
||||
rows = await db.all(`select * from episodes order by ${COLUMN_MAP[sortColumn]} ${SORT_MAP[JSON.parse(sort)[1]]} limit ? offset ?;`, rangeEnd - rangeStart, rangeStart);
|
||||
} else if (filter) {
|
||||
const filterParsed = JSON.parse(filter);
|
||||
rows = await db.all(`select * from episodes where id in (${filterParsed['id'].map(x => '?').join(',')})`, filterParsed['id']);
|
||||
}
|
||||
const { count } = await db.get('select count(id) as count from episodes;');
|
||||
|
||||
res.setHeader('Content-Range', count);
|
||||
res.status(200).json(rows)
|
||||
} else if (req.method === 'POST') {
|
||||
await db.run(`insert into episodes (${COLS_LIST.join(', ')}) values (${COLS_LIST.map(() => '?').join(', ')});`,
|
||||
COLS_LIST.map((c) => req.body[c]));
|
||||
const episode = await db.get('select * from episodes where number = ? and title = ? and slug = ?', req.body['number'], req.body['title'], req.body['slug']);
|
||||
res.status(200).json(episode);
|
||||
}
|
||||
}
|
||||
14
src/pages/api/admin/subscriptions.js
Normal file
14
src/pages/api/admin/subscriptions.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import db from '@/db';
|
||||
|
||||
export default async function handler(req, res) {
|
||||
const sessionId = req.cookies?.session;
|
||||
if (!sessionId) { res.status(404).json({}); return; }
|
||||
const sessionRes = await db.get('select email from sessions join users on users.id = sessions.user_id where session_id=?;', sessionId);
|
||||
if (!sessionRes || sessionRes?.email != process.env.ADMIN_EMAIL) { res.status(404).json({}); return; }
|
||||
if (req.method === 'GET') {
|
||||
const rows = await db.all('select id, user_id, uuid, started_date from subscriptions;');
|
||||
|
||||
res.setHeader('Content-Range', rows.length);
|
||||
res.status(200).json(rows)
|
||||
}
|
||||
}
|
||||
15
src/pages/api/admin/transcript_files.js
Normal file
15
src/pages/api/admin/transcript_files.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const fs = require('fs');
|
||||
|
||||
export default async function handler(req, res) {
|
||||
if (req.method === 'GET') {
|
||||
const filesOrig = fs.readdirSync('./src/data', {withFileTypes: true})
|
||||
.filter(item => !item.isDirectory())
|
||||
.map(item => item.name);
|
||||
const files = filesOrig.filter(f => f.includes('.srt'));
|
||||
files.sort();
|
||||
files.reverse();
|
||||
|
||||
res.setHeader('Content-Range', files.length);
|
||||
res.status(200).json(files.map((f, i) => { return { id: i, filename: f } }));
|
||||
}
|
||||
}
|
||||
16
src/pages/api/admin/users.js
Normal file
16
src/pages/api/admin/users.js
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
|
||||
import db from '@/db';
|
||||
|
||||
export default async function handler(req, res) {
|
||||
const sessionId = req.cookies?.session;
|
||||
if (!sessionId) { res.status(404).json({}); return; }
|
||||
const sessionRes = await db.get('select email from sessions join users on users.id = sessions.user_id where session_id=?;', sessionId);
|
||||
if (!sessionRes || sessionRes?.email != process.env.ADMIN_EMAIL) { res.status(404).json({}); return; }
|
||||
if (req.method === 'GET') {
|
||||
const rows = await db.all('select id, email from users;');
|
||||
|
||||
res.setHeader('Content-Range', rows.length);
|
||||
res.status(200).json(rows)
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,16 @@ import path from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
import db from '@/db';
|
||||
import {
|
||||
ROOT,
|
||||
REACTORS_ACCOUNT,
|
||||
accountUnsubscribeURL,
|
||||
accountFeedURL,
|
||||
podcastPage,
|
||||
episodeFile
|
||||
} from '@/paths';
|
||||
|
||||
import { Podcast } from 'podcast';
|
||||
import mp3Duration from 'mp3-duration';
|
||||
|
||||
import { getEpisodes } from '@/data/episodes';
|
||||
|
||||
@@ -13,31 +20,24 @@ async function syncEpisodes() {
|
||||
let newEpisodes = false;
|
||||
|
||||
const dbUpdates = episodes.map(async ({ title, published, description, content, slug, audio: { src, length }, num, id, youtube }) => {
|
||||
const filename = `0${num}-mixed.mp3`; // TODO auto-add the 0 prefix
|
||||
const filepath = path.join(process.cwd(), 'public', 'files', 'episodes', filename);
|
||||
if (fs.existsSync(filepath)) {
|
||||
const existsInDb = await db.get('select id from episodes where number=?', num);
|
||||
if (!existsInDb) {
|
||||
newEpisodes = true;
|
||||
console.log('adding to db');
|
||||
const duration = Math.round(await mp3Duration(filepath));
|
||||
await db.run('insert into episodes (number, content, summary, slug, season, episode, filename, duration, title, episode_type, buzzsprout_id, buzzsprout_url, pub_date, youtube_url) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);',
|
||||
num,
|
||||
content,
|
||||
description,
|
||||
slug,
|
||||
1,
|
||||
num,
|
||||
filename,
|
||||
duration,
|
||||
title,
|
||||
'full',
|
||||
id,
|
||||
src,
|
||||
published,
|
||||
youtube);
|
||||
console.log('added to db', num);
|
||||
}
|
||||
if (!existsInDb) {
|
||||
newEpisodes = true;
|
||||
console.log('adding to db');
|
||||
await db.run('insert into episodes (number, content, summary, slug, season, episode, audio_url, title, episode_type, buzzsprout_id, buzzsprout_url, pub_date, youtube_url) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);',
|
||||
num,
|
||||
content,
|
||||
description,
|
||||
slug,
|
||||
1,
|
||||
num,
|
||||
title,
|
||||
'full',
|
||||
id,
|
||||
src,
|
||||
published,
|
||||
youtube);
|
||||
console.log('added to db', num);
|
||||
}
|
||||
})
|
||||
// if (newEpisodes) {
|
||||
@@ -49,22 +49,36 @@ async function syncEpisodes() {
|
||||
|
||||
export default async function handler(req, res) {
|
||||
if (req.method === 'GET') {
|
||||
// await syncEpisodes();
|
||||
await syncEpisodes();
|
||||
const { uuid: uuidRaw } = req.query;
|
||||
const uuid = uuidRaw.split('.rss')[0];
|
||||
const subExists = await db.get('select id from subscriptions where uuid=?', uuid);
|
||||
if (subExists) {
|
||||
const now = new Date();
|
||||
const dbEpisodesRaw = await db.all('select * from episodes order by number desc;');
|
||||
const dbEpisodes = dbEpisodesRaw.filter(e => new Date(e.pub_date) <= now);
|
||||
const { last_build_date } = await db.get('select last_build_date from feed;');
|
||||
const lastEpisode = dbEpisodes[0];
|
||||
let lastBuilt = new Date(last_build_date);
|
||||
if (lastBuilt < new Date(lastEpisode.pub_date)) {
|
||||
console.log('rebuild!');
|
||||
await db.run('update feed set last_build_date = ?;', now.toISOString());
|
||||
lastBuilt = now;
|
||||
}
|
||||
const feed = new Podcast({
|
||||
title: 'The React Show (Reactors)',
|
||||
description: "Premium subscription to The React Show. Discussions about React, JavaScript, and web development by React experts with a focus on diving deep into learning React and discussing what it's like to work within the React industry.",
|
||||
feedUrl: `https://www.thereactshow.com/api/${uuidRaw}`,
|
||||
siteUrl: 'https://www.thereactshow.com',
|
||||
title: 'The React Show Premium: The Reactors',
|
||||
description: `<p>Premium subscription to The React Show: thank you so much for your support!</p>
|
||||
<p>Manage your subscription here: <a href="${REACTORS_ACCOUNT}">${REACTORS_ACCOUNT}</a></p>
|
||||
<p>Unsubscribe here: <a href="${accountUnsubscribeURL(uuid)}">${accountUnsubscribeURL(uuid)}</a></p>
|
||||
<p>Discussions about React, JavaScript, and web development by React experts with a focus on diving deep into learning React and discussing what it's like to work within the React industry.</p>`,
|
||||
feedUrl: accountFeedURL(uuid),
|
||||
siteUrl: ROOT,
|
||||
imageUrl: 'https://storage.buzzsprout.com/variants/d1tds1rufs5340fyq9mpyzo491qp/5cfec01b44f3e29fae1fb88ade93fc4aecd05b192fbfbc2c2f1daa412b7c1921.jpg',
|
||||
author: 'The React Show',
|
||||
copyright: '© 2023 Owl Creek',
|
||||
language: 'en',
|
||||
categories: ['Technology','Education','Business'],
|
||||
pubDate: 'May 20, 2012 04:00:00 GMT',
|
||||
pubDate:lastBuilt,
|
||||
ttl: 60,
|
||||
itunesAuthor: 'The React Show',
|
||||
itunesOwner: { name: 'The React Show' },
|
||||
@@ -81,30 +95,25 @@ export default async function handler(req, res) {
|
||||
itunesImage: 'https://storage.buzzsprout.com/variants/d1tds1rufs5340fyq9mpyzo491qp/5cfec01b44f3e29fae1fb88ade93fc4aecd05b192fbfbc2c2f1daa412b7c1921.jpg'
|
||||
});
|
||||
|
||||
const dbEpisodes = await db.all('select * from episodes order by number desc;');
|
||||
dbEpisodes.forEach(({ title, pub_date, summary: description, content, slug, duration, filename, number, episode_type }) => {
|
||||
const filepath = path.join(process.cwd(), 'public', 'files', 'episodes', filename);
|
||||
if (fs.existsSync(filepath)) {
|
||||
feed.addItem({
|
||||
title,
|
||||
description: content,
|
||||
content,
|
||||
url: `https://www.thereactshow.com/podcast/${slug}`,
|
||||
date: pub_date,
|
||||
itunesTitle: title,
|
||||
itunesExplicit: false,
|
||||
itunesSummary: description,
|
||||
itunesDuration: duration,
|
||||
itunesAuthor: 'The React Show',
|
||||
itunesSeason: 1,
|
||||
itunesEpisode: number,
|
||||
itunesEpisodeType: episode_type,
|
||||
enclosure : {
|
||||
url: `https://www.thereactshow.com/files/episodes/${filename}`,
|
||||
file: filepath
|
||||
},
|
||||
});
|
||||
}
|
||||
dbEpisodes.forEach(({ title, pub_date, summary: description, content, slug, duration, audio_url, number, episode_type }) => {
|
||||
feed.addItem({
|
||||
title,
|
||||
description: content,
|
||||
content,
|
||||
url: podcastPage(slug),
|
||||
date: pub_date,
|
||||
itunesTitle: title,
|
||||
itunesExplicit: false,
|
||||
itunesSummary: description,
|
||||
itunesDuration: duration,
|
||||
itunesAuthor: 'The React Show',
|
||||
itunesSeason: 1,
|
||||
itunesEpisode: number,
|
||||
itunesEpisodeType: episode_type,
|
||||
enclosure : {
|
||||
url: audio_url || ''
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
const xml = feed.buildXml();
|
||||
|
||||
Reference in New Issue
Block a user