2024-11-07 20:57:33 +01:00
|
|
|
import { InternalOptions } from "../scraper/scrapeURL";
|
|
|
|
|
import { ScrapeOptions } from "../controllers/v1/types";
|
2024-08-13 20:51:43 +02:00
|
|
|
import { WebCrawler } from "../scraper/WebScraper/crawler";
|
|
|
|
|
import { redisConnection } from "../services/queue-service";
|
2024-11-07 20:57:33 +01:00
|
|
|
import { logger } from "./logger";
|
2024-08-13 20:51:43 +02:00
|
|
|
|
|
|
|
|
export type StoredCrawl = {
|
2024-10-17 19:40:18 +02:00
|
|
|
originUrl?: string;
|
2024-08-13 20:51:43 +02:00
|
|
|
crawlerOptions: any;
|
2024-11-07 20:57:33 +01:00
|
|
|
scrapeOptions: Omit<ScrapeOptions, "timeout">;
|
|
|
|
|
internalOptions: InternalOptions;
|
2024-08-13 20:51:43 +02:00
|
|
|
team_id: string;
|
2024-11-07 20:57:33 +01:00
|
|
|
plan?: string;
|
2024-08-13 20:51:43 +02:00
|
|
|
robots?: string;
|
|
|
|
|
cancelled?: boolean;
|
2024-08-15 18:55:18 +02:00
|
|
|
createdAt: number;
|
2024-08-13 20:51:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export async function saveCrawl(id: string, crawl: StoredCrawl) {
|
|
|
|
|
await redisConnection.set("crawl:" + id, JSON.stringify(crawl));
|
|
|
|
|
await redisConnection.expire("crawl:" + id, 24 * 60 * 60, "NX");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getCrawl(id: string): Promise<StoredCrawl | null> {
|
|
|
|
|
const x = await redisConnection.get("crawl:" + id);
|
|
|
|
|
|
|
|
|
|
if (x === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return JSON.parse(x);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-16 22:47:56 +02:00
|
|
|
export async function getCrawlExpiry(id: string): Promise<Date> {
|
|
|
|
|
const d = new Date();
|
2024-08-16 22:52:48 +02:00
|
|
|
const ttl = await redisConnection.pttl("crawl:" + id);
|
|
|
|
|
d.setMilliseconds(d.getMilliseconds() + ttl);
|
|
|
|
|
d.setMilliseconds(0);
|
2024-08-16 22:47:56 +02:00
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 20:51:43 +02:00
|
|
|
export async function addCrawlJob(id: string, job_id: string) {
|
|
|
|
|
await redisConnection.sadd("crawl:" + id + ":jobs", job_id);
|
|
|
|
|
await redisConnection.expire("crawl:" + id + ":jobs", 24 * 60 * 60, "NX");
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 20:34:19 +02:00
|
|
|
export async function addCrawlJobs(id: string, job_ids: string[]) {
|
|
|
|
|
await redisConnection.sadd("crawl:" + id + ":jobs", ...job_ids);
|
|
|
|
|
await redisConnection.expire("crawl:" + id + ":jobs", 24 * 60 * 60, "NX");
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 22:10:17 +02:00
|
|
|
export async function addCrawlJobDone(id: string, job_id: string) {
|
|
|
|
|
await redisConnection.sadd("crawl:" + id + ":jobs_done", job_id);
|
2024-08-16 22:47:56 +02:00
|
|
|
await redisConnection.lpush("crawl:" + id + ":jobs_done_ordered", job_id);
|
2024-08-13 22:10:17 +02:00
|
|
|
await redisConnection.expire("crawl:" + id + ":jobs_done", 24 * 60 * 60, "NX");
|
2024-08-16 22:47:56 +02:00
|
|
|
await redisConnection.expire("crawl:" + id + ":jobs_done_ordered", 24 * 60 * 60, "NX");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getDoneJobsOrderedLength(id: string): Promise<number> {
|
|
|
|
|
return await redisConnection.llen("crawl:" + id + ":jobs_done_ordered");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getDoneJobsOrdered(id: string, start = 0, end = -1): Promise<string[]> {
|
|
|
|
|
return await redisConnection.lrange("crawl:" + id + ":jobs_done_ordered", start, end);
|
2024-08-13 22:10:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function isCrawlFinished(id: string) {
|
|
|
|
|
return (await redisConnection.scard("crawl:" + id + ":jobs_done")) === (await redisConnection.scard("crawl:" + id + ":jobs"));
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-17 01:04:14 +02:00
|
|
|
export async function isCrawlFinishedLocked(id: string) {
|
|
|
|
|
return (await redisConnection.exists("crawl:" + id + ":finish"));
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-16 18:48:52 +02:00
|
|
|
export async function finishCrawl(id: string) {
|
|
|
|
|
if (await isCrawlFinished(id)) {
|
|
|
|
|
const set = await redisConnection.setnx("crawl:" + id + ":finish", "yes");
|
|
|
|
|
if (set === 1) {
|
|
|
|
|
await redisConnection.expire("crawl:" + id + ":finish", 24 * 60 * 60);
|
|
|
|
|
}
|
|
|
|
|
return set === 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 20:51:43 +02:00
|
|
|
export async function getCrawlJobs(id: string): Promise<string[]> {
|
|
|
|
|
return await redisConnection.smembers("crawl:" + id + ":jobs");
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-26 21:00:27 +02:00
|
|
|
export async function getThrottledJobs(teamId: string): Promise<string[]> {
|
|
|
|
|
return await redisConnection.zrangebyscore("concurrency-limiter:" + teamId + ":throttled", Date.now(), Infinity);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 16:22:06 +01:00
|
|
|
export function normalizeURL(url: string): string {
|
|
|
|
|
const urlO = new URL(url);
|
|
|
|
|
urlO.search = "";
|
|
|
|
|
urlO.hash = "";
|
|
|
|
|
return urlO.href;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 20:29:17 +01:00
|
|
|
export function generateURLPermutations(url: string | URL): URL[] {
|
|
|
|
|
const urlO = new URL(url);
|
|
|
|
|
|
|
|
|
|
// Construct two versions, one with www., one without
|
|
|
|
|
const urlWithWWW = new URL(urlO);
|
|
|
|
|
const urlWithoutWWW = new URL(urlO);
|
|
|
|
|
if (urlO.hostname.startsWith("www.")) {
|
|
|
|
|
urlWithoutWWW.hostname = urlWithWWW.hostname.slice(4);
|
|
|
|
|
} else {
|
|
|
|
|
urlWithWWW.hostname = "www." + urlWithoutWWW.hostname;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let permutations = [urlWithWWW, urlWithoutWWW];
|
|
|
|
|
|
|
|
|
|
// Construct more versions for http/https
|
|
|
|
|
permutations = permutations.flatMap(urlO => {
|
|
|
|
|
if (!["http:", "https:"].includes(urlO.protocol)) {
|
|
|
|
|
return [urlO];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const urlWithHTTP = new URL(urlO);
|
|
|
|
|
const urlWithHTTPS = new URL(urlO);
|
|
|
|
|
urlWithHTTP.protocol = "http:";
|
|
|
|
|
urlWithHTTPS.protocol = "https:";
|
|
|
|
|
|
|
|
|
|
return [urlWithHTTP, urlWithHTTPS];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return permutations;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 20:51:43 +02:00
|
|
|
export async function lockURL(id: string, sc: StoredCrawl, url: string): Promise<boolean> {
|
|
|
|
|
if (typeof sc.crawlerOptions?.limit === "number") {
|
|
|
|
|
if (await redisConnection.scard("crawl:" + id + ":visited") >= sc.crawlerOptions.limit) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-01 19:00:33 +02:00
|
|
|
|
2024-11-08 16:22:06 +01:00
|
|
|
url = normalizeURL(url);
|
|
|
|
|
|
|
|
|
|
let res: boolean;
|
2024-11-08 16:25:11 +01:00
|
|
|
if (!sc.crawlerOptions.deduplicateSimilarURLs) {
|
2024-11-08 16:22:06 +01:00
|
|
|
res = (await redisConnection.sadd("crawl:" + id + ":visited", url)) !== 0
|
|
|
|
|
} else {
|
2024-11-11 20:29:17 +01:00
|
|
|
const permutations = generateURLPermutations(url);
|
2024-11-08 16:22:06 +01:00
|
|
|
res = (await redisConnection.sadd("crawl:" + id + ":visited", ...permutations.map(x => x.href))) === permutations.length;
|
2024-10-01 19:00:33 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-13 20:51:43 +02:00
|
|
|
await redisConnection.expire("crawl:" + id + ":visited", 24 * 60 * 60, "NX");
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 20:34:19 +02:00
|
|
|
/// NOTE: does not check limit. only use if limit is checked beforehand e.g. with sitemap
|
|
|
|
|
export async function lockURLs(id: string, urls: string[]): Promise<boolean> {
|
2024-10-01 19:00:33 +02:00
|
|
|
urls = urls.map(url => {
|
|
|
|
|
try {
|
|
|
|
|
const urlO = new URL(url);
|
|
|
|
|
urlO.search = "";
|
|
|
|
|
urlO.hash = "";
|
|
|
|
|
return urlO.href;
|
|
|
|
|
} catch (error) {
|
2024-11-07 20:57:33 +01:00
|
|
|
logger.warn("Failed to normalize URL " + JSON.stringify(url) + ": " + error);
|
2024-10-01 19:00:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return url;
|
|
|
|
|
});
|
|
|
|
|
|
2024-08-14 20:34:19 +02:00
|
|
|
const res = (await redisConnection.sadd("crawl:" + id + ":visited", ...urls)) !== 0
|
|
|
|
|
await redisConnection.expire("crawl:" + id + ":visited", 24 * 60 * 60, "NX");
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 20:51:43 +02:00
|
|
|
export function crawlToCrawler(id: string, sc: StoredCrawl): WebCrawler {
|
|
|
|
|
const crawler = new WebCrawler({
|
|
|
|
|
jobId: id,
|
2024-11-07 20:57:33 +01:00
|
|
|
initialUrl: sc.originUrl!,
|
2024-08-13 20:51:43 +02:00
|
|
|
includes: sc.crawlerOptions?.includes ?? [],
|
|
|
|
|
excludes: sc.crawlerOptions?.excludes ?? [],
|
|
|
|
|
maxCrawledLinks: sc.crawlerOptions?.maxCrawledLinks ?? 1000,
|
|
|
|
|
maxCrawledDepth: sc.crawlerOptions?.maxDepth ?? 10,
|
|
|
|
|
limit: sc.crawlerOptions?.limit ?? 10000,
|
|
|
|
|
generateImgAltText: sc.crawlerOptions?.generateImgAltText ?? false,
|
|
|
|
|
allowBackwardCrawling: sc.crawlerOptions?.allowBackwardCrawling ?? false,
|
|
|
|
|
allowExternalContentLinks: sc.crawlerOptions?.allowExternalContentLinks ?? false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (sc.robots !== undefined) {
|
|
|
|
|
try {
|
|
|
|
|
crawler.importRobotsTxt(sc.robots);
|
|
|
|
|
} catch (_) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return crawler;
|
|
|
|
|
}
|