triangle-utils 1.4.111 → 1.4.112

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.
@@ -4,7 +4,9 @@ export declare class UtilsBee {
4
4
  constructor(scraping_bee_api_key: string | undefined);
5
5
  get_buffer(url: string, params?: Record<string, string | number | boolean>): Promise<Error | Buffer<any> | undefined>;
6
6
  get(url: string, params?: Record<string, string | number | boolean>): Promise<string | Error | undefined>;
7
- google_search(query: string, news?: boolean): Promise<Record<string, any>[] | undefined>;
7
+ google_search(query: string, options?: {
8
+ search_type_id?: string;
9
+ }): Promise<Record<string, any>[] | undefined>;
8
10
  youtube_search(query: string, options?: {}): Promise<Record<string, any>[] | undefined>;
9
11
  youtube_get_subtitles(video_id: string): Promise<Record<string, any> | undefined>;
10
12
  }
@@ -39,42 +39,39 @@ export class UtilsBee {
39
39
  }
40
40
  return this.text_decoder.decode(buffer);
41
41
  }
42
- async google_search(query, news = false) {
42
+ async google_search(query, options = {}) {
43
43
  if (this.bee === undefined) {
44
44
  return undefined;
45
45
  }
46
- for (let i = 0; i < 3; i++) {
47
- try {
48
- const response = await this.bee.googleSearch({
49
- search: query,
50
- params: {
51
- search_type: news ? "news" : undefined
52
- }
53
- });
54
- if (response.status !== 200) {
55
- console.log("Failed to Google:", response.status);
56
- continue;
57
- }
58
- const data = JSON.parse(response.data.toString("utf-8"));
59
- if (data.news_results === undefined || data.organic_results === undefined) {
60
- continue;
61
- }
62
- const results = news ? data.news_results : data.organic_results;
63
- if (results === undefined) {
64
- console.log("Failed to Google.");
65
- continue;
66
- }
67
- if (!Array.isArray(results)) {
68
- console.log("Results are not an array.");
69
- return;
46
+ try {
47
+ const response = await this.bee.googleSearch({
48
+ search: query,
49
+ params: {
50
+ search_type: options.search_type_id
70
51
  }
71
- return results;
52
+ });
53
+ if (response.status !== 200) {
54
+ console.log("Failed to Google:", response.status);
55
+ return;
72
56
  }
73
- catch {
57
+ const data = JSON.parse(response.data.toString("utf-8"));
58
+ if (data.news_results === undefined || data.organic_results === undefined) {
59
+ return;
60
+ }
61
+ const results = options.search_type_id === "news" ? data.news_results : data.organic_results;
62
+ if (results === undefined) {
74
63
  console.log("Failed to Google.");
64
+ return;
65
+ }
66
+ if (!Array.isArray(results)) {
67
+ console.log("Results are not an array.");
68
+ return;
75
69
  }
70
+ return results;
71
+ }
72
+ catch (error) {
73
+ console.log("Failed to Google:", error.stack);
76
74
  }
77
- console.log("Failed to Google thrice, quitting.");
78
75
  return undefined;
79
76
  }
80
77
  async youtube_search(query, options = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.111",
3
+ "version": "1.4.112",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "directories": {
package/src/UtilsBee.ts CHANGED
@@ -45,44 +45,41 @@ export class UtilsBee {
45
45
  return this.text_decoder.decode(buffer)
46
46
  }
47
47
 
48
- async google_search(query : string, news : boolean = false) : Promise<Record<string, any>[] | undefined> {
48
+ async google_search(query : string, options : { search_type_id? : string } = {}) : Promise<Record<string, any>[] | undefined> {
49
49
  if (this.bee === undefined) {
50
50
  return undefined
51
51
  }
52
- for (let i = 0; i < 3; i++) {
53
- try {
54
- const response = await this.bee.googleSearch({
55
- search : query,
56
- params : {
57
- search_type : news ? "news" : undefined
58
- }
59
- })
60
- if (response.status !== 200) {
61
- console.log("Failed to Google:", response.status)
62
- continue
63
- }
64
- const data : Record<string, any> = JSON.parse(response.data.toString("utf-8"))
65
-
66
- if (data.news_results === undefined || data.organic_results === undefined) {
67
- continue
52
+ try {
53
+ const response = await this.bee.googleSearch({
54
+ search : query,
55
+ params : {
56
+ search_type : options.search_type_id
68
57
  }
69
- const results : Record<string, any>[] | undefined = news ? data.news_results : data.organic_results
58
+ })
59
+ if (response.status !== 200) {
60
+ console.log("Failed to Google:", response.status)
61
+ return
62
+ }
63
+ const data : Record<string, any> = JSON.parse(response.data.toString("utf-8"))
64
+
65
+ if (data.news_results === undefined || data.organic_results === undefined) {
66
+ return
67
+ }
68
+ const results : Record<string, any>[] | undefined = options.search_type_id === "news" ? data.news_results : data.organic_results
70
69
 
71
70
 
72
- if (results === undefined) {
73
- console.log("Failed to Google.")
74
- continue
75
- }
76
- if (!Array.isArray(results)) {
77
- console.log("Results are not an array.")
78
- return
79
- }
80
- return results
81
- } catch {
71
+ if (results === undefined) {
82
72
  console.log("Failed to Google.")
73
+ return
74
+ }
75
+ if (!Array.isArray(results)) {
76
+ console.log("Results are not an array.")
77
+ return
83
78
  }
79
+ return results
80
+ } catch (error : any) {
81
+ console.log("Failed to Google:", error.stack)
84
82
  }
85
- console.log("Failed to Google thrice, quitting.")
86
83
  return undefined
87
84
  }
88
85