triangle-utils 1.0.36 → 1.1.0
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/package.json +2 -1
- package/test.js +19 -6
- package/utils/Utils_Bee.js +26 -0
- package/utils/Utils_DynamoDB.js +9 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "triangle-utils",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"main": "Utils.js",
|
|
5
5
|
"author": "",
|
|
6
6
|
"license": "ISC",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"@aws-sdk/client-dynamodb": "^3.953.0",
|
|
12
12
|
"@aws-sdk/client-s3": "^3.953.0",
|
|
13
13
|
"@aws-sdk/client-s3vectors": "^3.953.0",
|
|
14
|
+
"@aws-sdk/client-secrets-manager": "^3.964.0",
|
|
14
15
|
"@aws-sdk/s3-request-presigner": "^3.953.0",
|
|
15
16
|
"nodemailer": "^7.0.11",
|
|
16
17
|
"scrapingbee": "^1.7.6",
|
package/test.js
CHANGED
|
@@ -1,11 +1,24 @@
|
|
|
1
1
|
import Utils from "./Utils.js";
|
|
2
|
+
import { SecretsManager } from "@aws-sdk/client-secrets-manager"
|
|
2
3
|
|
|
4
|
+
const secret_name = "api_keys"
|
|
3
5
|
|
|
4
|
-
const
|
|
6
|
+
const secrets_manager = new SecretsManager({ region: "us-east-1" })
|
|
5
7
|
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
})
|
|
8
|
+
const api_keys = await secrets_manager.getSecretValue({
|
|
9
|
+
SecretId: secret_name
|
|
10
|
+
}).then(response => JSON.parse(response.SecretString))
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
const config = {
|
|
13
|
+
google_email : "louishou@triangleanalytics.com",
|
|
14
|
+
alerts_email : "alerts@triangleanalytics.com",
|
|
15
|
+
region : "us-east-1",
|
|
16
|
+
...api_keys
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
config.fec_api_keys = config.fec_api_keys.split(",")
|
|
20
|
+
|
|
21
|
+
const utils = new Utils(config)
|
|
22
|
+
|
|
23
|
+
const videos = await utils.bee.youtube_search("Marie Gluesenkamp Perez")
|
|
24
|
+
console.log(videos)
|
package/utils/Utils_Bee.js
CHANGED
|
@@ -55,4 +55,30 @@ export default class Utils_Bee {
|
|
|
55
55
|
console.log("Failed to Google thrice, quitting.")
|
|
56
56
|
return undefined
|
|
57
57
|
}
|
|
58
|
+
|
|
59
|
+
async youtube_search(query) {
|
|
60
|
+
for (let i = 0; i < 3; i++) {
|
|
61
|
+
try {
|
|
62
|
+
const bee_url = "https://app.scrapingbee.com/api/v1/youtube/search?api_key=" + this.config.scraping_bee_api_key + "&search=" + encodeURI(query) + "&sort_by=upload_date"
|
|
63
|
+
console.log(bee_url)
|
|
64
|
+
const response = await fetch(bee_url)
|
|
65
|
+
if (!response.ok) {
|
|
66
|
+
console.log("Failed to YouTube Search:", response.status)
|
|
67
|
+
continue
|
|
68
|
+
}
|
|
69
|
+
const data = await response.json()
|
|
70
|
+
const results = data.results
|
|
71
|
+
|
|
72
|
+
if (results === undefined) {
|
|
73
|
+
console.log("Failed to YouTube Search.")
|
|
74
|
+
continue
|
|
75
|
+
}
|
|
76
|
+
return results
|
|
77
|
+
} catch {
|
|
78
|
+
console.log("Failed to YouTube Search.")
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
console.log("Failed to YouTube Search thrice, quitting.")
|
|
82
|
+
return undefined
|
|
83
|
+
}
|
|
58
84
|
}
|
package/utils/Utils_DynamoDB.js
CHANGED
|
@@ -38,7 +38,7 @@ function convert_input(input) {
|
|
|
38
38
|
return undefined
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
async function
|
|
41
|
+
async function compile_pages(request, f, compile = true) {
|
|
42
42
|
const items = []
|
|
43
43
|
let last_eval_key = undefined
|
|
44
44
|
while (true) {
|
|
@@ -49,7 +49,7 @@ async function paginate(request, f) {
|
|
|
49
49
|
)
|
|
50
50
|
const new_items = response.Items.map(item => convert_output({ M : item }))
|
|
51
51
|
items.push(...new_items)
|
|
52
|
-
if (response.LastEvaluatedKey === undefined) {
|
|
52
|
+
if (response.LastEvaluatedKey === undefined || !compile) {
|
|
53
53
|
return items
|
|
54
54
|
}
|
|
55
55
|
last_eval_key = response.LastEvaluatedKey
|
|
@@ -86,7 +86,7 @@ export default class Utils_DynamoDB {
|
|
|
86
86
|
Segment : i,
|
|
87
87
|
TotalSegments : concurrency
|
|
88
88
|
}).filter(([field, value]) => value !== undefined && (typeof value === typeof 3 || Object.keys(value).length !== 0)))
|
|
89
|
-
iterators.push(
|
|
89
|
+
iterators.push(compile_pages(request, (request) => this.dynamodb.scan(request)))
|
|
90
90
|
}
|
|
91
91
|
const segments = await Promise.all(iterators)
|
|
92
92
|
return segments.flat()
|
|
@@ -131,7 +131,9 @@ export default class Utils_DynamoDB {
|
|
|
131
131
|
|
|
132
132
|
|
|
133
133
|
|
|
134
|
-
async query(table, primary_key,
|
|
134
|
+
async query(table, primary_key, options = {}) {
|
|
135
|
+
const reverse = options.reverse !== undefined ? options.reverse : false
|
|
136
|
+
const compile = options.reverse !== undefined ? options.compile : true
|
|
135
137
|
if (Object.keys(primary_key).length !== 1) {
|
|
136
138
|
return undefined
|
|
137
139
|
}
|
|
@@ -146,7 +148,7 @@ export default class Utils_DynamoDB {
|
|
|
146
148
|
KeyConditionExpression: "#a = :a",
|
|
147
149
|
ScanIndexForward : !reverse
|
|
148
150
|
}
|
|
149
|
-
return await
|
|
151
|
+
return await compile_pages(request, (request) => this.dynamodb.query(request), compile)
|
|
150
152
|
}
|
|
151
153
|
|
|
152
154
|
async query_prefix(table, primary_key, secondary_key_prefix, reverse=false) {
|
|
@@ -166,7 +168,7 @@ export default class Utils_DynamoDB {
|
|
|
166
168
|
KeyConditionExpression: "#a = :a AND begins_with(#b, :b)",
|
|
167
169
|
ScanIndexForward : !reverse
|
|
168
170
|
}
|
|
169
|
-
return await
|
|
171
|
+
return await compile_pages(request, (request) => this.dynamodb.query(request))
|
|
170
172
|
}
|
|
171
173
|
|
|
172
174
|
async query_range(table, primary_key, secondary_key_range, reverse=false) {
|
|
@@ -187,7 +189,7 @@ export default class Utils_DynamoDB {
|
|
|
187
189
|
KeyConditionExpression: "#a = :a AND (#b BETWEEN :b1 AND :b2)",
|
|
188
190
|
ScanIndexForward : !reverse
|
|
189
191
|
}
|
|
190
|
-
return await
|
|
192
|
+
return await compile_pages(request, (request) => this.dynamodb.query(request))
|
|
191
193
|
}
|
|
192
194
|
|
|
193
195
|
async set(table, key, attributes) {
|