-
Notifications
You must be signed in to change notification settings - Fork 0
/
popularities.js
48 lines (41 loc) · 1.07 KB
/
popularities.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
// Copyright 2024 Joseph P Medley
'use strict';
const fs = require('fs');
const { downloadPopularities } = require('./updateData.js');
const { getFile } = require('./utils.js');
class popularities {
constructor(category, options = {}) {
this._sourcePath = 'popularities.json';
if (options.source) { this._sourcePath = options.source }
this._popularities = this._loadPopularities(category);
}
_loadPopularities(filter) {
if (!fs.existsSync(this._sourcePath)) {
downloadPopularities();
}
const source = getFile(this._sourcePath);
let someData = source.split(',');
someData.shift();
someData.pop();
if (!filter) { return someData; }
return someData.filter(f => {
return f.includes(filter);
});
}
get rawData() {
return this._popularities;
}
getRating(key) {
let val = this._popularities.find(f => {
return f.includes(key);
});
if (val) {
val = val.split(":")[1];
val = val.trim();
} else {
val = 0;
}
return Number(val);
}
}
module.exports.Popularities = popularities;