Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/website screenshotting #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"nanoid": "^3.2.0",
"node-fetch": "2",
"node-tesseract-ocr": "^2.2.1",
"puppeteer": "^19.4.0",
"sequelize": "^6.19.0",
"sqlite3": "^5.0.5",
"string-similarity": "^4.0.4"
Expand Down
24 changes: 23 additions & 1 deletion src/events/MessageScanner.event.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { GuildChannel, Message } from "discord.js";
import { Event } from "../handlers/EventHandler";
import puppeteer from "puppeteer";
// @ts-ignore
import Tesseract from "node-tesseract-ocr";
import fetch from "node-fetch";
import { client } from "../index";
import { BinLink } from "../types";
import AutoResponseManager from "../managers/AutoResponseManager";
import StatisticsManager from "../managers/StatisticsManager";

const tesseractConfig = {
lang: "eng",
oem: 2,
Expand Down Expand Up @@ -59,6 +59,28 @@ export default class ReadyEvent extends Event<"messageCreate"> {
text += " " + content;
}

// Links in text
const urlExpression = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b[-a-zA-Z0-9()@:%_\+.~#?&//=]*/gi;
const matches = msg.content.match(urlExpression);
if (matches) {
for (const match of matches) {
let browser;
try {
browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.setViewport({ width: 1440, height: 1080 });
await page.goto(match);
const screenshot = await page.screenshot({ type: 'jpeg', quality: 100 });

text += (" " + (await Tesseract.recognize(screenshot, tesseractConfig)));
} catch (err: any) {
console.log(`❌ Browser error: ${err.message}`);
} finally {
await browser?.close();
}
}
}

// Paste links
const bins = JSON.parse(
client.github.getFileFromRepo("bin_links.json")
Expand Down
Loading