Skip to content
Merged
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
2 changes: 1 addition & 1 deletion packages/scraper-strategies/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@qnaplus/scraper-strategies",
"version": "3.1.3",
"version": "3.1.4",
"description": "Request stategies for @qnaplus/scraper",
"main": "dist/index.js",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions packages/scraper/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@qnaplus/scraper",
"version": "3.1.3",
"version": "3.1.4",
"description": "Utility package to scrape questions from the VEX Robotics Q&A.",
"main": "dist/index.js",
"repository": {
Expand All @@ -23,7 +23,7 @@
"@crawlee/core": "^3.16.0",
"cheerio": "^1.2.0",
"got-scraping": "^4.2.1",
"node-pdf-parser": "^1.0.7",
"pdfjs-dist": "^5.7.284",
"pino": "catalog:"
},
"devDependencies": {
Expand Down
23 changes: 20 additions & 3 deletions packages/scraper/src/modules/rules.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { parsepdf } from "node-pdf-parser";
import { getDefaultFetcherOptions } from "../clients";
import type { FetcherOptions } from "./fetchers";

Expand All @@ -9,6 +8,25 @@ export interface Rule {
summary: string;
}

async function extractPdfText(buffer: ArrayBuffer): Promise<string> {
const { getDocument } = await import("pdfjs-dist/legacy/build/pdf.mjs");
const doc = await getDocument({ data: new Uint8Array(buffer) }).promise;
const pages: string[] = [];
for (let i = 1; i <= doc.numPages; i++) {
const page = await doc.getPage(i);
const content = await page.getTextContent();
let pageText = "";
for (const item of content.items) {
if ("str" in item) {
pageText += item.str;
if (item.hasEOL) pageText += "\n";
}
}
pages.push(pageText);
}
return pages.join("\n");
}

export const extractRules = async (
url: string,
options?: FetcherOptions,
Expand All @@ -19,8 +37,7 @@ export const extractRules = async (
if (buffer === null) {
return null;
}
const content = await parsepdf(buffer);
const text = content.pages.join("\n");
const text = await extractPdfText(buffer);
const matches = Array.from(text.matchAll(RULE_TABLE_PATTERN));
const rules: Rule[] = [];
for (const match of matches) {
Expand Down
Loading