-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrank-checker.js
92 lines (74 loc) · 2.91 KB
/
rank-checker.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
const fs = require('fs');
const axios = require('axios');
const Rankfile = require('./rankfile');
const CredentialsStore = require('./credential-store');
const Serp = require('./serp');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
var keyFileContents = fs.readFileSync('./keys.crd','utf8');
var credentialStore = new CredentialsStore(keyFileContents);
var dataForSeoUsername = credentialStore.getCredential('dataForSeoUsername');
var dataforSeoPassword = credentialStore.getCredential('dataForSeoPassword');
var rankfile = new Rankfile(fs.readFileSync('./rankfile.txt','utf8'));
const csvWriter = createCsvWriter({
path: 'rank_results.csv',
header: [
{id: 'url', title: 'URL'},
{id: 'keyword', title: 'Keyword'},
{id: 'rank', title: 'Rank'}
]
});
function getRankSafely(serp, url) {
try {
return serp.getRankOfUrl(url);
} catch (error) {
return 100; // Set rank to 100 if an exception is thrown
}
}
let results = [];
getDataForSeoResults();
function getDataForSeoResults() {
let requests = [];
rankfile.entryToCheck.forEach((entry, entryIndex) => {
console.log(entry)
entry.keywords.forEach((keyword, keywordIndex) => {
const postRequest = {
method: 'post',
url: 'https://api.dataforseo.com/v3/serp/google/organic/live/regular',
auth: {
username: dataForSeoUsername,
password: dataforSeoPassword
},
data: [{
"keyword": encodeURIComponent(keyword),
"language_code": "en",
"location_code": 2840
}],
headers: {
'content-type': 'application/json'
}
};
let request = axios(postRequest).then(function (response) {
var result = response['data']['tasks'];
var serp = new Serp(result);
var rank = getRankSafely(serp,entry.url);
results.push({url: entry.url, keyword: keyword, rank: rank, entryIndex, keywordIndex});
//console.log(`Rank for ${entry.url}, ${keyword} is ${rank}`);
}).catch(function (error) {
console.log(error);
});
requests.push(request);
})
})
Promise.all(requests).then(() => {
results.sort((a, b) => a.entryIndex === b.entryIndex ? a.keywordIndex - b.keywordIndex : a.entryIndex - b.entryIndex);
// Remove index fields before writing to CSV
const csvResults = results.map(({ entryIndex, keywordIndex, ...rest }) => rest);
csvWriter.writeRecords(csvResults)
.then(() => {
console.log('CSV file was written successfully');
})
.catch((err) => {
console.log('Error writing CSV file', err);
});
});
}