This repository was archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (59 loc) · 2.26 KB
/
index.js
File metadata and controls
70 lines (59 loc) · 2.26 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
const fetch = require('node-fetch')
const HTMLParser = require('node-html-parser')
const colors = require('colors')
const {
baseUrl,
fileUrl,
projects } = require('./projects.json')
const activeUsersParser = html => Number(
html
.querySelector(".project-file-list-item")
.querySelector(".project-file-downloads")
.text
.replace(',','')
)
const totalDownloadsParser = html => {
let n = 0;
html
.querySelector(".cf-details") // Right panel with addon data
.querySelectorAll("li") // Each row on that panel
.find(e => { // Find the "Total Downloads" row and extract the count
if (e.querySelector(".info-label").text === "Total Downloads") {
n = Number(e.querySelector(".info-data").text.replace(',',''))
return true
}
})
return n;
}
const parsePage = (url, parser) => fetch(url)
.then(res => res.text())
.then(HTMLParser.parse)
.then(parser)
const startTime = new Date()
const data = projects.map(project => {
const projectUrl = "" + baseUrl + project.id
const projectFilesUrl = projectUrl + fileUrl
const projectData = { name: project.name }
return Promise.all([
// Parsers go here and forward their results to the new projectData object
parsePage(projectUrl, totalDownloadsParser)
.then(result => projectData.totalDownloads = result),
parsePage(projectFilesUrl, activeUsersParser)
.then(result => projectData.recentDownloads = result)
]).then(() => projectData)
});
Promise.all(data).then(results => {
const totalDownloads = results.reduce((n, p) => n + p.totalDownloads, 0)
const recentDownloads = results.reduce((n, p) => n + p.recentDownloads, 0)
console.log(`You have ${String(totalDownloads).green} total downloads and ${String(recentDownloads).green} recent downloads`)
results.forEach(({name, recentDownloads: r, totalDownloads: t}) => {
// Output:
// Recount: 12345678 total, 10456 recent
console.log(
`${String(name).cyan}:`,
`${t} total, ${r} recent`
)
})
const executionTime = new Date() - startTime
console.log(`\nRan in ${String(executionTime).red}ms`)
});