This repository has been archived by the owner on May 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (78 loc) · 2.57 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
const request = require('superagent');
let ads = [];
/**
* Return matching ad(s) from Xyfir Ads' public repository.
* @param {object} [options]
* @param {number} [options.count=1] - Number of ads to attempt to retrieve.
* @param {boolean} [options.xyfir] - Determines how to handle ads created by
* the Xyfir Network. Set `true` to only return Xyfir-created ads; `false` to
* return all non-Xyfir ads; and leave undefined for both.
* @param {Array.<string|number>} [options.blacklist] - Blacklist ads by slot
* (array) index if number or by name if string.
* @param {Array.<string>} [options.keywords] - Increase likelyhood of an ad
* being returned if it has keywords that match the provided keywords. If
* keywords are not provided, ads are randomly selected from those available.
* @return {Array.<object>}
*/
module.exports = async function(options = {}) {
if (!ads.length) await loadAds();
return ads
.slice()
.filter((ad, slot) => {
if (options.xyfir != undefined) {
if (options.xyfir && ad.creator != 'Xyfir')
return false;
if (!options.xyfir && ad.creator == 'Xyfir')
return false;
}
if (options.blacklist) {
for (let blacklisted of options.blacklist) {
if (typeof blacklisted == 'number' && slot == blacklisted)
return false;
if (typeof blacklisted == 'string' && ad.name == blacklisted)
return false;
}
}
if (options.keywords) {
const keywordString = options.keywords.join(' ');
ad.score = 0;
for (let keyword of ad.keywords) {
// Check for exact keyword match
if (options.keywords.indexOf(keyword) > -1)
ad.score += 3;
// Check for partial match
else if (keywordString.indexOf(keyword) > -1)
ad.score++;
}
}
return true;
})
.sort((a, b) => {
// Sort by score
if (options.keywords) {
if (a.score > b.score)
return -1;
if (a.score < b.score)
return 1;
// Random sort if scores are equal
return Math.round(Math.random()) == 0 ? -1 : 1;
}
// Random sort
else {
return Math.round(Math.random()) == 0 ? -1 : 1;
}
})
.slice(0, options.count || 1);
}
async function loadAds() {
try {
const result = await request.get(
'https://raw.githubusercontent.com/Xyfir/Ads/master/ads.json'
);
ads = JSON.parse(result.text);
}
catch (err) {
setTimeout(loadAds, 60 * 1000);
}
}
setInterval(loadAds, 86400 * 1000);