-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
212 lines (199 loc) · 5.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const AWS = require("aws-sdk");
require("dotenv").config({ path: "./.env" });
var Validator = require("jsonschema").Validator;
// AWS configuration
AWS.config.update({
region: "eu-central-1",
endpoint: "http://dynamodb.eu-central-1.amazonaws.com",
accessKeyId: process.env.Access_Key_Id,
secretAccessKey: process.env.Secret_Access_Key,
sessionToken: process.env.Session_Token,
});
// Create the DynamoDB service object
var dynamodbClient = new AWS.DynamoDB.DocumentClient();
const table_name = "prod-lp-content-proxy-content";
const json_schema = {
$schema: "http://json-schema.org/draft-04/schema#",
type: "object",
properties: {
id: {
type: "string",
},
country: {
type: "string",
},
createdDate: {
type: "string",
},
data: {
type: "string",
},
language: {
type: "string",
},
path: {
type: "string",
},
platform: {
type: "string",
},
updatedDate: {
type: "string",
},
},
required: [
"id",
"country",
"createdDate",
"data",
"language",
"path",
"platform",
"updatedDate",
],
};
async function filtered_doc(filters, lastEvaluatedKey) {
var params = {
TableName: table_name,
ExpressionAttributeNames: { "#lang": "language" },
FilterExpression: " #lang = :l AND country = :c AND platform = :p ",
ExpressionAttributeValues: {
":l": filters.language,
":c": filters.country,
":p": filters.platform,
},
};
lastEvaluatedKey && (params.ExclusiveStartKey = lastEvaluatedKey);
dynamodbClient.scan(params, function (err, data) {
if (err) {
console.log("Error when Filtering \n", err);
} else {
// console.log("Success with filtering \n", data);
}
});
return dynamodbClient.scan(params).promise();
}
async function filterById(hashKey) {
return dynamodbClient
.query({
TableName: table_name,
KeyConditionExpression: "id = :hashKey",
ExpressionAttributeValues: {
":hashKey": hashKey,
},
})
.promise();
}
function put_item(item) {
var v = new Validator();
var instance = item;
let validation_output = v.validate(instance, json_schema);
console.log(v.validate(instance, json_schema));
if (typeof item.data === "string" && validation_output.errors.length == 0) {
var params = {
TableName: table_name,
Item: item,
};
dynamodbClient.put(params, function (err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success added updated document", data);
}
});
} else {
console.log(
" WRITE Operation Denied -- check Object format before and after changes "
);
}
}
async function UpdateTitleForIos() {
let ios_title =
"You can watch dazn on your Supported devices.\nWatch on any 2 devices at the same time. ";
const obj = await filterById("35628be0-9c6f-4d9c-8d8c-5cb17cab9923");
if (obj.Items.length > 0) {
let items_returned = obj.Items;
let item = items_returned[0];
item.data = JSON.parse(item.data);
item.data.devices.title = ios_title;
console.log(item);
// put_item(item);
} else {
console.log(" IOS - Items not found , please enter the correct hashID ");
}
}
async function UpdateTitleForAndroid() {
let android_title =
"Connect up to 6 Devices. Watch Sport in HD.\nWatch on any 2 devices at the same time.";
const obj = await filterById("69d4cc92-e061-4f2f-8442-702c5017dcd0");
if (obj.Items.length > 0) {
let items_returned = obj.Items;
let item = items_returned[0];
item.data = JSON.parse(item.data);
item.data.devices.title = android_title;
console.log(item);
item.data = JSON.stringify(item.data);
// put_item(item);
} else {
console.log(
" Android - Item not found , please enter the correct hasdID "
);
}
}
async function UpdatePricesAcrossAndroidTV() {
let androidtv_price_description =
"14,99€ per month after trial, cancel monthly.";
let filters = {
language: "en",
country: "de",
platform: "androidtv",
// description: "cancel",
};
let items_returned = [];
const obj = await filtered_doc(filters);
items_returned = obj.Items;
if (obj.LastEvaluatedKey) {
const temp_obj = await filtered_doc(filters, obj.LastEvaluatedKey);
temp_obj.Items.length > 0 &&
temp_obj.Items?.map((item) => {
items_returned.push(item);
});
}
console.log(
"Total number of items found for anddroid tv " + items_returned.length
);
console.log(items_returned);
// if description present this filter executes
filters.description &&
(items_returned = items_returned?.filter((item) => {
item.data = JSON.parse(item.data);
if (
Boolean(item.data.priceSection) &&
item.data.priceSection.description
) {
let desc_in_item = item.data.priceSection.description;
item.data = JSON.stringify(item.data);
return desc_in_item.includes(filters.description);
} else {
console.log("There is no priceSection in the Object");
return false;
}
}));
if (obj.Items.length > 0) {
items_returned?.map(async (item) => {
item.data = await JSON.parse(item.data);
item.data.priceSection = {
description: androidtv_price_description,
};
item.data = JSON.stringify(item.data);
// put_item(item);
});
} else {
console.log(
" AndroidTV - Items not found , please enter the correct filters "
);
}
}
UpdateTitleForAndroid();
UpdateTitleForIos();
UpdatePricesAcrossAndroidTV();