This repository has been archived by the owner on May 1, 2020. It is now read-only.
/
get_statistics.py
72 lines (59 loc) · 2.69 KB
/
get_statistics.py
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
import os, glob, time
import requests as r
import simplejson as json
os.environ['TZ'] = 'Portugal'
time.tzset()
class qgisTransifex:
def __init__(self, user, passw, lang):
self.username = user
self.password = passw
self.language = lang
self.transifexApi = 'https://www.transifex.com/api/2/project'
self.availableApis = {'stats': 'stats'}
self.translateProjects = [
{'name': 'gui', 'pslug': 'QGIS'},
{'name': 'docs', 'pslug': 'qgis-documentation'},
{'name': 'site', 'pslug': 'qgis-website'}
]
def getResourcesList(self, pslug):
req = r.get('/'.join([self.transifexApi, pslug]) + '?details', auth=(self.username, self.password))
return json.loads(req.content)
def getResourceStats(self, pslug, rslug):
req = r.get('/'.join([self.transifexApi, pslug, 'resource', rslug, self.availableApis['stats'], self.language]), auth=(self.username, self.password))
return json.loads(req.content)
def getProjectStats(self, project, resources):
stats = {}
stats['trans_words'] = 0
stats['untrans_words'] = 0
stats['trans_entities'] = 0
stats['untrans_entities'] = 0
finalStats = {}
finalStats['project'] = project['name']
finalStats['updated_on'] = time.strftime('%d/%m/%Y %H:%M:%S')
rlist = resources['resources']
for resource in rlist:
rstats = self.getResourceStats(project['pslug'], resource['slug'])
stats['trans_words'] += rstats['translated_words']
stats['untrans_words'] += rstats['untranslated_words']
stats['trans_entities'] += rstats['translated_entities']
stats['untrans_entities'] += rstats['untranslated_entities']
stats['completed'] = float(stats['trans_entities']) / (stats['trans_entities'] + stats['untrans_entities']) * 100
finalStats['stats'] = stats
return finalStats
def getGlobalStats(self):
globalStats = []
for proj in self.translateProjects:
resources = self.getResourcesList(proj['pslug'])
projStats = self.getProjectStats(proj, resources)
globalStats.append(projStats)
return globalStats
def dumpJsonFile(self, path, stats, name):
os.chdir(path)
file = glob.glob(name)
if len(file) > 0:
os.remove(name)
with open(name, 'w') as outfile:
outfile.write(json.dumps(stats, indent=4, ensure_ascii=False))
qgisStats = qgisTransifex('user', 'pass', 'pt_PT')
stats = qgisStats.getGlobalStats()
qgisStats.dumpJsonFile('path', stats, 'file.json')