Add /accounts/:username/ratings endpoint (#1756)
[oweals/peertube.git] / scripts / generate-code-contributors.ts
1 import { doRequest } from '../server/helpers/requests'
2 import { readFileSync } from 'fs-extra'
3
4 run()
5   .then(() => process.exit(0))
6   .catch(err => {
7     console.error(err)
8     process.exit(-1)
9   })
10
11 async function run () {
12
13   {
14     const contributors = await fetchGithub('https://api.github.com/repos/chocobozzz/peertube/contributors')
15
16     console.log('# Code\n')
17     for (const contributor of contributors) {
18       const contributorUrl = contributor.url.replace('api.github.com/users', 'github.com')
19       console.log(` * [${contributor.login}](${contributorUrl})`)
20     }
21   }
22
23   {
24     const zanataConfig = readFileSync(require('os').homedir() + '/.config/zanata.ini').toString()
25     const zanataUsername = zanataConfig.match('.username=([^\n]+)')[1]
26     const zanataToken = zanataConfig.match('.key=([^\n]+)')[1]
27
28     const translators = await fetchZanata(zanataUsername, zanataToken)
29
30     console.log('\n\n# Translations\n')
31     for (const translator of translators) {
32       console.log(` * [${translator.username}](https://trad.framasoft.org/zanata/profile/view/${translator.username})`)
33     }
34   }
35
36   {
37     console.log('\n\n# Design\n')
38     console.log('By [Olivier Massain](https://twitter.com/omassain)\n')
39     console.log('Icons from [Robbie Pearce](https://robbiepearce.com/softies/)')
40   }
41 }
42
43 function get (url: string, headers: any = {}) {
44   return doRequest<any>({
45     uri: url,
46     json: true,
47     headers: Object.assign(headers, {
48       'User-Agent': 'PeerTube-App'
49     })
50   }).then(res => res.body)
51 }
52
53 async function fetchGithub (url: string) {
54   let next = url
55   let allResult = []
56
57   let i = 1
58
59   // Hard limit
60   while (i < 20) {
61     const result = await get(next + '?page=' + i)
62     if (result.length === 0) break
63
64     allResult = allResult.concat(result)
65     i++
66   }
67
68   return allResult
69 }
70
71 async function fetchZanata (zanataUsername: string, zanataPassword: string) {
72   const today = new Date().toISOString().split('T')[0]
73   const year2018 = `https://trad.framasoft.org/zanata/rest/project/peertube/version/develop/contributors/2018-01-01..2019-01-01`
74   const year2019 = `https://trad.framasoft.org/zanata/rest/project/peertube/version/develop/contributors/2019-01-01..${today}`
75
76   const headers = {
77     'X-Auth-User': zanataUsername,
78     'X-Auth-Token': zanataPassword
79   }
80   const [ results2018, results2019 ] = await Promise.all([
81     get(year2018, headers),
82     get(year2019, headers)
83   ])
84
85   return results2018.concat(results2019)
86 }