-
Notifications
You must be signed in to change notification settings - Fork 42
/
stat.ts
88 lines (71 loc) · 2.14 KB
/
stat.ts
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
/**
* ./src/commands/tools/stat.ts
* penguins-eggs v.10.0.0 / ecmascript 2020
* author: Piero Proietti
* email: piero.proietti@gmail.com
* license: MIT
*/
import { Command, Flags } from '@oclif/core'
import axios from 'axios'
import https from 'node:https'
import Utils from '../../classes/utils.js'
const agent = new https.Agent({
rejectUnauthorized: false
})
import yaml from 'js-yaml'
/**
*
*/
export default class ToolsStat extends Command {
static description = 'get statistics from sourceforge'
static examples = ['eggs tools stat', 'eggs tools stat --month', 'eggs tools stat --year']
static flags = {
help: Flags.help({ char: 'h' }),
month: Flags.boolean({ char: 'm', description: 'current month' }),
year: Flags.boolean({ char: 'y', description: 'current year' })
}
/**
*
*/
async run(): Promise<void> {
const { args, flags } = await this.parse(ToolsStat)
Utils.titles(this.id + ' ' + this.argv)
const yesterday = new Date()
yesterday.setDate(yesterday.getDate() - 1)
// current date
// adjust 0 before single digit date
const day = ('0' + yesterday.getDate()).slice(-2)
// current month
const month = ('0' + (yesterday.getMonth() + 1)).slice(-2)
// current year
const year = yesterday.getFullYear()
const end = year + '-' + month + '-' + day
let start = year + '-' + month + '-' + day
if (flags.month) {
start = year + '-' + month + '-01'
}
if (flags.year) {
start = year + '-01-01'
}
this.log('start: ' + start + ', end: ' + end + '\n')
await this.show(start, end, 'DEBS')
console.log()
await this.show(start, end, 'ISOS')
}
/**
*
* @param start
* @param end
* @param type
*/
async show(start: string, end: string, type = 'DEBS') {
let url = `https://sourceforge.net/projects/penguins-eggs/files/${type}/stats/json`
const request = '?start_date=' + start + '&end_date=' + end
url += request
const res = await axios.get(url, { httpsAgent: agent })
console.log(type)
for (const country of res.data.countries) {
console.log('- ' + country[0] + ': ' + country[1])
}
}
}