triangle-utils 1.4.4 → 1.4.6

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.
@@ -2,6 +2,7 @@ export declare class UtilsBee {
2
2
  private readonly bee;
3
3
  private readonly text_decoder;
4
4
  constructor(scraping_bee_api_key: string | undefined);
5
+ get_buffer(url: string, params?: Record<string, string | number | boolean>): Promise<Buffer<any> | undefined>;
5
6
  get(url: string, params?: Record<string, string | number | boolean>): Promise<string | undefined>;
6
7
  google_search(query: string, news?: boolean): Promise<Record<string, any>[] | undefined>;
7
8
  youtube_search(query: string, options?: {}): Promise<Record<string, any>[] | undefined>;
@@ -6,29 +6,35 @@ export class UtilsBee {
6
6
  this.bee = scraping_bee_api_key !== undefined ? new ScrapingBeeClient(scraping_bee_api_key) : undefined;
7
7
  this.text_decoder = new TextDecoder();
8
8
  }
9
- async get(url, params = {}) {
9
+ async get_buffer(url, params = {}) {
10
10
  if (this.bee === undefined) {
11
11
  return undefined;
12
12
  }
13
- for (let i = 0; i < 3; i++) {
14
- try {
15
- const response = await this.bee.htmlApi({
16
- url: url,
17
- params: params
18
- });
19
- const text = this.text_decoder.decode(response.data);
20
- return text;
13
+ try {
14
+ const response = await this.bee.htmlApi({
15
+ url: url,
16
+ params: params
17
+ });
18
+ if (response.data instanceof Buffer) {
19
+ return response.data;
21
20
  }
22
- catch (error) {
23
- if (error instanceof Error) {
24
- console.log(error.stack);
25
- }
26
- console.log("Failed to get from Scraping Bee.");
21
+ return undefined;
22
+ }
23
+ catch (error) {
24
+ if (error instanceof Error) {
25
+ console.log(error.stack);
27
26
  }
27
+ console.log("Failed to get from Scraping Bee.");
28
28
  }
29
- console.log("Failed to get from Scraping Bee, quitting.");
30
29
  return undefined;
31
30
  }
31
+ async get(url, params = {}) {
32
+ const buffer = await this.get_buffer(url, params);
33
+ if (buffer === undefined) {
34
+ return undefined;
35
+ }
36
+ return this.text_decoder.decode(buffer);
37
+ }
32
38
  async google_search(query, news = false) {
33
39
  if (this.bee === undefined) {
34
40
  return undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.4",
3
+ "version": "1.4.6",
4
4
  "main": "dist/src/index.js",
5
5
  "directories": {
6
6
  "test": "vitest"
package/src/UtilsBee.ts CHANGED
@@ -10,27 +10,35 @@ export class UtilsBee {
10
10
  this.text_decoder = new TextDecoder()
11
11
  }
12
12
 
13
- async get(url : string, params : Record<string, string | number | boolean> = {}) {
13
+ async get_buffer(url : string, params : Record<string, string | number | boolean> = {}) {
14
14
  if (this.bee === undefined) {
15
15
  return undefined
16
16
  }
17
- for (let i = 0; i < 3; i++) {
18
- try {
19
- const response = await this.bee.htmlApi({
20
- url : url,
21
- params : params
22
- })
23
- const text = this.text_decoder.decode(response.data)
24
- return text
25
- } catch (error) {
26
- if (error instanceof Error) {
27
- console.log(error.stack)
28
- }
29
- console.log("Failed to get from Scraping Bee.")
17
+ try {
18
+ const response = await this.bee.htmlApi({
19
+ url : url,
20
+ params : params
21
+ })
22
+ if (response.data instanceof Buffer) {
23
+ return response.data
24
+ }
25
+ return undefined
26
+ } catch (error) {
27
+ if (error instanceof Error) {
28
+ console.log(error.stack)
30
29
  }
30
+ console.log("Failed to get from Scraping Bee.")
31
31
  }
32
- console.log("Failed to get from Scraping Bee, quitting.")
33
32
  return undefined
33
+
34
+ }
35
+
36
+ async get(url : string, params : Record<string, string | number | boolean> = {}) {
37
+ const buffer = await this.get_buffer(url, params)
38
+ if (buffer === undefined) {
39
+ return undefined
40
+ }
41
+ return this.text_decoder.decode(buffer)
34
42
  }
35
43
 
36
44
  async google_search(query : string, news : boolean = false) : Promise<Record<string, any>[] | undefined> {