import {db} from '../databases/databases'; import {Logger} from '../utils/logger'; import {Request, Response} from 'express'; import { config } from '../config'; const ONE_MINUTE = 1000 * 60; const styleHeader = `` const licenseHeader = `

The API and database follow CC BY-NC-SA 4.0 unless you have explicit permission.

Attribution Template

If you need to use the database or API in a way that violates this license, contact me with your reason and I may grant you access under a different license.

`; const tables = [{ name: "sponsorTimes", order: "timeSubmitted" }, { name: "userNames" }, { name: "categoryVotes" }, { name: "noSegments", }, { name: "warnings", order: "issueTime" }, { name: "vipUsers" }]; const links: string[] = tables.map((table) => `/database/${table.name}.csv`); const linksHTML: string = tables.map((table) => `

${table.name}.csv

`) .reduce((acc, url) => acc + url, ""); let lastUpdate = 0; export default function dumpDatabase(req: Request, res: Response, showPage: boolean) { if (!config.postgres) { res.status(404).send("Not supported on this instance"); return; } const now = Date.now(); const updateQueued = now - lastUpdate > ONE_MINUTE; res.status(200) if (showPage) { res.send(`${styleHeader}

SponsorBlock database dumps

${licenseHeader}

How this works

Send a request to https://sponsor.ajay.app/database.json, or visit this page to trigger the database dump to run. Then, you can download the csv files below, or use the links returned from the JSON request.

Links

${linksHTML}
${updateQueued ? `Update queued.` : ``} Last updated: ${lastUpdate ? new Date(lastUpdate).toUTCString() : `Unknown`}`); } else { res.send({ lastUpdated: lastUpdate, updateQueued, links }) } if (updateQueued) { lastUpdate = Date.now(); for (const table of tables) { db.prepare('run', `COPY (SELECT * FROM "${table.name}"${table.order ? ` ORDER BY "${table.order}"` : ``}) TO '/opt/exports/${table.name}.csv' WITH (FORMAT CSV, HEADER true);`); } } }