@wargas/crawler 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.test.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { expect, test } from 'bun:test'
2
+ import { Crawler } from "./index";
3
+
4
+ test(`get page title`, async function() {
5
+
6
+ const c = Crawler.factory();
7
+
8
+ await c.client.get("https://google.com")
9
+
10
+ expect(c.document.title).toBe(`Google`)
11
+
12
+ })
package/index.ts CHANGED
@@ -2,10 +2,13 @@ import type { Got } from "got";
2
2
  import got from "got";
3
3
  import { CookieJar } from "tough-cookie";
4
4
  import FileCookieStore from "tough-cookie-file-store";
5
+ import { parseHTML } from "linkedom"
5
6
 
6
7
  export class Crawler {
7
8
  client!: Got
8
9
  cookieJar!: CookieJar
10
+ html = ``
11
+ document = parseHTML(``).document
9
12
 
10
13
  static factory() {
11
14
  const instance = new Crawler()
@@ -13,7 +16,21 @@ export class Crawler {
13
16
  instance.cookieJar = new CookieJar(new FileCookieStore(`cookies.json`))
14
17
 
15
18
  instance.client = got.extend({
16
- cookieJar: instance.cookieJar
19
+ hooks: {
20
+ afterResponse: [
21
+ res => {
22
+ if(typeof res.body == `string`) {
23
+ instance.html = res.body
24
+ instance.document = parseHTML(res.body).document
25
+ }
26
+
27
+ return res
28
+ }
29
+ ]
30
+ }
31
+ // followRedirect:false,
32
+ // cookieJar: instance.cookieJar,
33
+
17
34
  })
18
35
 
19
36
  return instance;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wargas/crawler",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "module": "index.ts",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -16,6 +16,7 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "got": "^15.0.5",
19
+ "linkedom": "^0.18.12",
19
20
  "tough-cookie": "^6.0.1",
20
21
  "tough-cookie-file-store": "^3.3.0"
21
22
  }
package/tsconfig.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  // Environment setup & latest features
4
- "lib": ["ESNext"],
4
+ "lib": ["ESNext", "DOM"],
5
5
  "target": "ESNext",
6
6
  "module": "Preserve",
7
7
  "moduleDetection": "force",