-
Notifications
You must be signed in to change notification settings - Fork 0
/
ego-utils.js
131 lines (118 loc) · 4.74 KB
/
ego-utils.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
// Copyright 2022 Ulvican Kahya
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const axios = require('axios').default;
axios.defaults.timeout = 15000;
const EventEmitter = require('events');
const fs = require('fs').promises;
class EgoScraper {
constructor(){
this.respondedPage = 0;
this.egoEmitter = new EventEmitter();
this.options = require('./options.json');
}
saveAsLog = async (newResult) => {
try {
const logPath = './logs.json';
newResult.ranking = newResult.ranking.toString();
newResult.total = newResult.total.toString();
try {
await fs.access(logPath, fs.constants.R_OK);
const logFile = await fs.readFile(logPath, 'utf8');
let logs = JSON.parse(logFile);
if (logs[logs.length -1].date !== newResult.date || logs[logs.length -1].ranking !== newResult.ranking || logs[logs.length -1].total !== newResult.total ) {
logs.push(newResult);
fs.writeFile(logPath, JSON.stringify(logs, null, ' '));
}
return Promise.resolve(logs);
} catch (error){
console.log('### File doesn\'t exist\n### Creating new file:', logPath);
let logs = [];
logs.push(newResult);
fs.writeFile(logPath, JSON.stringify(logs, null, ' '));
return Promise.resolve(logs);
}
} catch (error) {
return Promise.reject(error);
}
};
getTotalPage = async () => {
try {
this.options.params.page = 1;
const response = await axios.request(this.options);
const totalPage = parseInt(response.data.numpages);
console.log('Total Page:', totalPage);
return Promise.resolve(totalPage);
} catch (error) {
return Promise.reject(error);
}
};
getPage = async (page) => {
try {
this.options.params.page = page;
const response = await axios.request(this.options);
this.respondedPage++;
let ranking;
const extensions = response.data['extensions'];
for (let i = 0; i < extensions.length; i++) {
const extension = extensions[i];
if (extension['name'] === 'EasyEffects Preset Selector') {
ranking = ((page - 1) * Number(this.options.params.n_per_page)) + i + 1;
}
}
console.log('Page responded:', response.config.params.page,
'page count:', this.respondedPage);
this.egoEmitter.emit('pageResponse', this.respondedPage);
return Promise.resolve({'ranking': ranking, 'extNumOnPage': extensions.length});
} catch (error) {
console.error('getPage Catched:', error);
this.egoEmitter.emit('error', error);
return Promise.reject(error);
}
};
getExtensions = async (totalPage) => {
let tasks = [];
console.log('sending ego request for pages below and adding them to tasks array');
for (let i = 1; i < totalPage + 1; i++) {
process.stdout.write(`${i}, `);
tasks.push(this.getPage(i));
}
process.stdout.write('\n');
return Promise.all(tasks)
.then(async (values) => {
console.log('All pages responded!');
const ranking = values.find(value => value.ranking).ranking;
let totalExtensionNum = 0;
values.forEach(val => {
totalExtensionNum += val.extNumOnPage;
});
let result = {'extensionNumber': totalExtensionNum, 'ranking': ranking, 'respondedPage': this.respondedPage, 'finished': 'yes'};
const now = new Date();
const year = now.getFullYear().toString();
const month = (now.getMonth() + 1).toString() > 9 ? now.getMonth() + 1 : '0' + (now.getMonth() + 1).toString();
const day = now.getDate().toString() > 9 ? now.getDate() : '0' + now.getDate().toString();
const date = year + '-' + month + '-' + day;
const newLogEntry = {date, ranking, 'total': totalExtensionNum};
console.log('newLogEntry:', newLogEntry);
try {
const logs = await this.saveAsLog(newLogEntry);
result['logs'] = logs;
console.log('RESULT:', result);
return Promise.resolve(result);
} catch (error) {
this.egoEmitter.emit('error', error);
return Promise.reject(error);
}
}).catch((error) => {
return Promise.reject(error);
});
};
}
module.exports = {EgoScraper};