-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetDifficultyVolume.js
185 lines (163 loc) · 5.01 KB
/
GetDifficultyVolume.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
// API stuff
////
/* Uncomment this and add in authorizations/variables
// DO NOT PASTE THIS SECTION ANYWHERE OR COMMIT TO REPO
let KWEAuth = 'Bearer KWEtoken'
let DFSEOAuth= 'Basic DFStoken'
let VolumeFieldName = 'Volume'
let DifficultyFieldName = 'Difficulty'
// DO NOT PASTE THIS SECTION ANYWHERE OR COMMIT TO REPO END
*/
////
// Version May 2, 2023
// This function will look at the query to see which entries are missing the respective 'field'
function GenerateArray(queryResult, field) {
let keywordList = [];
for (let record of queryResult.records) {
if (!record.getCellValueAsString(field)) {
console.log(
`${record.getCellValueAsString('Keyword')} has no ${field} value`,
);
if (record.getCellValueAsString('Keyword') != '') {
keywordList.push(record.getCellValueAsString('Keyword'));
}
}
}
return keywordList;
}
async function KeywordsEverywhere(KeywordList) {
if (KeywordList > 100) {
console.log(
"Keyword list is too long for Keyword Everywhere's API, will only get the first 100. Run again to get the rest",
);
}
let trimmedKeywordList = KeywordList.slice(0, 99);
console.log(trimmedKeywordList, 'trimed');
let myHeaders = new Headers();
myHeaders.append('Authorization', KWEAuth);
myHeaders.append('Accept', 'application/json');
myHeaders.append('Content-Type', 'application/x-www-form-urlencoded');
let urlencoded = new URLSearchParams();
urlencoded.append('dataSource', 'gkp');
for (const keyword of trimmedKeywordList) {
urlencoded.append('kw[]', keyword);
}
console.log(Object.fromEntries(urlencoded));
console;
const requestOptions = {
method: 'POST',
headers: myHeaders,
body: urlencoded,
redirect: 'follow',
};
console.log(urlencoded);
let response = await fetch(
'https://api.keywordseverywhere.com/v1/get_keyword_data',
requestOptions,
);
let responseJSON = await response.json();
return responseJSON.data;
}
async function DataForSEO(KeywordList) {
let myHeaders = new Headers();
myHeaders.append('Authorization', DFSEOAuth);
myHeaders.append('Content-Type', 'application/json');
let raw = JSON.stringify([
{
keywords: KeywordList,
location_code: 2840,
language_code: 'en',
},
]);
let requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow',
};
let response = await fetch(
'https://api.dataforseo.com/v3/dataforseo_labs/google/bulk_keyword_difficulty/live',
requestOptions,
);
let responseJSON = await response.json();
console.log(responseJSON);
const {
tasks: [
{
result: [{ items }],
},
],
} = responseJSON;
return items;
}
// Change this name to use a different table
let table = base.getTable('Keywords');
let queryResult = await table.selectRecordsAsync({
fields: ['Keyword', VolumeFieldName, DifficultyFieldName],
});
let difficultyLookup = await GenerateArray(queryResult, DifficultyFieldName);
let volumeLookup = await GenerateArray(queryResult, VolumeFieldName);
console.log(volumeLookup);
/// update fields with difficulty
if (difficultyLookup.length !== 0) {
try {
let DFSresults = [];
DFSresults = await DataForSEO(difficultyLookup);
for (const item of DFSresults) {
const keyword = item.keyword?.toLowerCase();
if (!keyword) {
console.log('No keyword found in item:', item);
continue;
}
const match = queryResult.records.find(
(rec) => rec.getCellValue('Keyword')?.toLowerCase() === keyword,
);
if (!match) {
console.log('No match found for keyword:', keyword);
continue;
}
const updates = {
[DifficultyFieldName]: item.keyword_difficulty ?? 0,
};
await table.updateRecordAsync(match.id, updates);
console.log('Match found for keyword:', keyword);
}
} catch (error) {
console.log(
'An error occurred while fetching or processing the data:',
error,
);
}
} else {
console.log('Difficulty data already filled in, moving on to the next step');
}
/// update volume
//console.log(KWEResults);
if (volumeLookup.length !== 0) {
try {
let keywordResults = [];
keywordResults = await KeywordsEverywhere(volumeLookup);
for (const result of keywordResults) {
const matchingRecord = queryResult.records.find((record) => {
const keyword = record.getCellValue('Keyword')?.toLowerCase();
return keyword === result.keyword?.toLowerCase();
});
if (matchingRecord) {
const volume = result.vol ?? 0;
const updates = { [VolumeFieldName]: volume };
await table.updateRecordAsync(matchingRecord.id, updates);
console.log(`Updated record ${matchingRecord.id}`);
} else {
console.log(`No matching record for ${result.keyword}`);
}
}
// Other code can be run here
} catch (error) {
console.log(
'An error occurred while fetching or processing the data:',
error,
);
}
} else {
console.log('Volume data already filled in');
}