-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-data.js
More file actions
103 lines (90 loc) · 2.46 KB
/
fetch-data.js
File metadata and controls
103 lines (90 loc) · 2.46 KB
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
// HTTPS module
const https = require("https");
// Variables for API data fetch
// TODO: GitHub repository sync
let pluginDownloads = 0, projects = 32, averageStars = 0;
// Download URLs
let downloadURL = new Map();
// Page URLs
let pageURL = new Map();
// Module export functions
module.exports = {
// Returns amount of plugin downloads
getPluginDownloads: function() {
return pluginDownloads;
},
// Returns the amount of projects
getProjects: function() {
return projects;
},
// Returns average stars in reviews
getAverageStars: function() {
return averageStars;
},
// Returns download URL of plugin specified by the ID
getDownloadURL: function(pluginId) {
return downloadURL.get(pluginId);
},
// Returns project page URL of plugin specified by the ID
getPageURL: function(pluginId) {
return pageURL.get(pluginId);
}
};
// Request options for Spiget API
const requestOptions = {
host: "api.spiget.org",
port: 443,
path: "/v2/authors/387302/resources",
method: "GET"
}
// Handles response data
function handleResponse(response) {
// Encoding
response.setEncoding("utf8");
// Data chunks
let data = "";
// On data receive
response.on("data", function(chunk) {
data += chunk;
});
// On response data chunk read end
response.on("end", function() {
processData(data);
});
}
// Handles errors
function handleError(error) {
console.log(error);
}
// Processes finalized data
function processData(data) {
// Reset
averageStars = 0;
pluginDownloads = 0;
// JSON object
const json = JSON.parse(data);
// For each plugin
json.forEach(element => {
// Add downloads and average stars
pluginDownloads += element.downloads;
averageStars += element.rating.average;
// Set the download and page URL
downloadURL.set(element.id, "https://spigotmc.org/" + element.file.url);
pageURL.set(element.id, "https://spigotmc.org/resources/" + element.id);
});
// Calculate and round average stars
averageStars = Math.round(averageStars / json.length);
}
// Requests data from Spiget API
function request() {
// Create request
let request = https.request(requestOptions, handleResponse);
// Add handler
request.on("error", handleError);
// End
request.end();
}
// Run the request
request();
// Run every 10 minutes
setInterval(request, 600000);