Skip to content

Commit d4c15f6

Browse files
committed
SaveUrl removed
1 parent 213a4a8 commit d4c15f6

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-18
lines changed

src/App.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import BaseCommand from "./BaseCommand";
12
import FetchPreview from "./commands/FetchPreview";
2-
import SaveUrl from "./SaveUrl";
3+
// import SaveUrl from "./SaveUrl";
34

45
const asNumber = (n) => typeof n === "number" ? n : parseInt(n, 10);
56

@@ -58,7 +59,7 @@ export default class App {
5859

5960
const instance = event.botCheck
6061
? new FetchPreview(event)
61-
: new SaveUrl(event);
62+
: new BaseCommand(event);
6263

6364
try {
6465
const r = await instance.save(event);

src/BaseCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export interface ICommandInput {
7777
export default class BaseCommand {
7878

7979
private browser: Browser;
80-
private page: Page;
80+
protected page: Page;
8181

8282
constructor(private event) {
8383

src/TempFileService.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { file } from "tmp-promise";
2+
import fetch from "node-fetch";
23
import * as tmp from "tmp";
4+
import { createWriteStream, promises as fsp } from "fs";
35

46
tmp.setGracefulCleanup();
57

@@ -10,4 +12,19 @@ export default class TempFileService {
1012
return file({ mode: 0o644, prefix: "tmp-" , postfix: ext});
1113
}
1214

15+
16+
public static async fetch(inputUrl: string, filePath: string = null) {
17+
const rs = await fetch(inputUrl);
18+
if (rs.status >= 300) {
19+
// error...
20+
throw new Error(`Download ${inputUrl} failed \r\n${await rs.text()}`);
21+
}
22+
23+
await fsp.writeFile(filePath, rs.body);
24+
25+
console.log(`File ${inputUrl} downloaded to ${filePath}`);
26+
27+
return filePath;
28+
}
29+
1330
}

src/commands/FetchPreview.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import fetch from "node-fetch";
12
import { Page } from "puppeteer-core";
23
import BaseCommand from "../BaseCommand";
34
import BotChecker from "../BotChecker";
5+
import TempFileService from "../TempFileService";
46

57
export default class FetchPreview extends BaseCommand {
68

@@ -9,33 +11,47 @@ export default class FetchPreview extends BaseCommand {
911
const params = event;
1012

1113
const {
12-
botCheck,
1314
botUserAgent,
1415
url
1516
} = params;
1617

17-
if (botCheck) {
18-
const { canCrawl , content } = await BotChecker.check(url, botUserAgent);
19-
delete params.url;
20-
params.content = content;
21-
// disable all resources...
22-
params.html = true;
23-
if (!canCrawl) {
24-
console.log("Bot denied succeeded");
25-
} else {
26-
console.log("Bot check succeeded");
27-
}
18+
const { canCrawl , content } = await BotChecker.check(url, botUserAgent);
19+
delete params.url;
20+
params.content = content;
21+
// disable all resources...
22+
params.html = true;
23+
if (!canCrawl) {
24+
console.log("Bot denied succeeded");
25+
} else {
26+
console.log("Bot check succeeded");
2827
}
2928

3029
return await this.onSave(params);
3130
}
3231

33-
protected onBeforeRender(timeout: number, page: Page, stopTest: any) {
32+
protected onBeforeRender() {
3433
return Promise.resolve();
3534
}
3635

37-
protected async onRender({ html, pdf, video, output }) {
38-
return {};
36+
protected async onRender({ output }) {
37+
let document: any;
38+
let url = await this.page.evaluate(() => document.head.querySelector(`meta[property="og:image"]`)?.content);
39+
if (url) {
40+
url = await this.page.evaluate(() => document.head.querySelector(`img`)?.src);
41+
}
42+
43+
const file = await TempFileService.getTempFile(".jpg");
44+
await TempFileService.fetch(url, file.path);
45+
46+
await this.upload({ url: output , filePath: file.path});
47+
48+
return {
49+
statusCode: 200,
50+
body: JSON.stringify(url),
51+
headers: {
52+
"content-type": "application/json"
53+
}
54+
};
3955
}
4056

4157
}

0 commit comments

Comments
 (0)