aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2022-07-05 00:23:58 +0530
committerBobby <[email protected]>2022-07-05 00:23:58 +0530
commit406a35de9d0d37d20123a0a3e961614142c2cf3c (patch)
tree31299b83e59f02ad08051c7e21655e1895b79106
parentde994764b87d2716f951941f26a1044aaca63ed7 (diff)
downloadthatcomputerscientist-archived.tar.xz
thatcomputerscientist-archived.zip
Updarte Screenshot API as a Public Servicearchived
-rw-r--r--README.md2
-rw-r--r--routes/api/index.js15
-rw-r--r--routes/api/private/admin.js (renamed from routes/api/admin.js)2
-rw-r--r--routes/api/private/user.js (renamed from routes/api/user.js)0
-rw-r--r--routes/api/public/screenshot.js46
-rw-r--r--routes/api/screenshot.js24
-rw-r--r--server.js6
7 files changed, 62 insertions, 33 deletions
diff --git a/README.md b/README.md
index 773a65b7..72ee72ac 100644
--- a/README.md
+++ b/README.md
@@ -15,7 +15,7 @@ If you would like to contribute to the website, please see the [contributing gui
Below is the screenshot of how the website looks like right now. This screenshot is being updated programatically by [That Computer Scientist's Screenshot API](https://api.thatcomputerscientist.com/screenshot), and will update as I push more changes to the repository.
-![Screenshot](https://api.thatcomputerscientist.com/screenshot?random=0.2128385904744179)
+![Screenshot](https://api.thatcomputerscientist.com/screenshot?random=0.4984434256068826)
## Cloning, Installing, and Running
diff --git a/routes/api/index.js b/routes/api/index.js
index 774828be..1b61ce45 100644
--- a/routes/api/index.js
+++ b/routes/api/index.js
@@ -1,10 +1,19 @@
const router = require("express").Router();
-const admin = require("./admin");
-const user = require("./user");
-const screenshot = require("./screenshot");
+const admin = require("./private/admin");
+const user = require("./private/user");
+const screenshot = require("./public/screenshot");
router.use("/admin", admin);
router.use("/user", user);
router.use("/screenshot", screenshot);
+router.get("/", (req, res) => {
+ res.status(200).json({
+ message: "Welcome to That Computer Scientist's Public API Service.",
+ apis: {
+ screenshot: "/screenshot",
+ }
+ });
+});
+
module.exports = router;
diff --git a/routes/api/admin.js b/routes/api/private/admin.js
index dd5d2744..8b7d7d5f 100644
--- a/routes/api/admin.js
+++ b/routes/api/private/admin.js
@@ -1,7 +1,7 @@
const router = require("express").Router();
const mysql = require("mysql2");
const bcrypt = require("bcryptjs");
-const validateAuthorization = require("../../functions/validate");
+const validateAuthorization = require("../../../functions/validate");
require("dotenv").config();
const connectionURL = process.env.DATABASE_URL;
diff --git a/routes/api/user.js b/routes/api/private/user.js
index 8874d2e5..8874d2e5 100644
--- a/routes/api/user.js
+++ b/routes/api/private/user.js
diff --git a/routes/api/public/screenshot.js b/routes/api/public/screenshot.js
new file mode 100644
index 00000000..6c8c1fe0
--- /dev/null
+++ b/routes/api/public/screenshot.js
@@ -0,0 +1,46 @@
+const router = require("express").Router();
+const puppeteer = require("puppeteer");
+const fs = require("fs");
+const yaml = require("yaml");
+const config = yaml.parse(fs.readFileSync("site.config.yml", "utf8"));
+
+router.get("/", async (req, res) => {
+ const width = parseInt(req.query.width) ? parseInt(req.query.width) : 1920;
+ const height = parseInt(req.query.height) ? parseInt(req.query.height) : 1080;
+ const url = req.query.url || config.url;
+ const format = req.query.format ? ['png', 'jpeg', 'webp'].includes(req.query.format) ? req.query.format : 'png' : 'png';
+ const fullpage = req.query.fullpage || true;
+
+ // Set screenshot options
+ const options = { type: format, fullPage: fullpage };
+
+ // Take a screenshot
+ const image = async (url, width, height, options) => {
+ const browser = await puppeteer.launch();
+ const page = await browser.newPage();
+ page.setViewport({ width, height });
+ // Go to the url
+ await page.goto(url, { waitUntil: "networkidle2" });
+ // Take a screenshot
+ const screenshot = await page.screenshot(options);
+ // Close the browser
+ await browser.close();
+ // Return the screenshot
+ return screenshot;
+ }
+
+ // Get the screenshot
+ image(url, width, height, options).then(screenshot => {
+ // Send the screenshot
+ res.setHeader("Content-Type", `image/${format}`);
+ res.send(screenshot);
+ }).catch(err => {
+ // Send an error
+ res.status(500).json({
+ message: "Error",
+ error: err
+ });
+ });
+});
+
+module.exports = router;
diff --git a/routes/api/screenshot.js b/routes/api/screenshot.js
deleted file mode 100644
index 06340981..00000000
--- a/routes/api/screenshot.js
+++ /dev/null
@@ -1,24 +0,0 @@
-const router = require("express").Router();
-const puppeteer = require("puppeteer");
-const fs = require("fs");
-const yaml = require("yaml");
-const config = yaml.parse(fs.readFileSync("site.config.yml", "utf8"));
-
-router.get("/", async (req, res) => {
- const browser = await puppeteer.launch();
- const page = await browser.newPage();
- page.setViewport({ width: 1920, height: 1080 });
- // Go to the url
- await page.goto(config.url, {
- waitUntil: "networkidle2",
- });
- // Take a screenshot
- const image = await page.screenshot({fullPage : true});
- await browser.close();
- // Send the image as a response
- res.setHeader("Content-Type", "image/png");
- res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
- res.send(image);
-});
-
-module.exports = router;
diff --git a/server.js b/server.js
index 90ea8129..fe805d1e 100644
--- a/server.js
+++ b/server.js
@@ -32,10 +32,8 @@ app.use(
})
);
-// Setup API subdomain
-app.use(
- subdomain("api", require("./routes/api"))
-);
+app.use(subdomain("screenshot.api", require("./routes/api/public/screenshot.js")));
+app.use(subdomain("api", require("./routes/api")));
// Set Template Engine
app.set("view engine", "ejs");