-
Notifications
You must be signed in to change notification settings - Fork 15
/
count-addon-downloads.py
70 lines (61 loc) · 1.87 KB
/
count-addon-downloads.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
#!/usr/bin/env python
# This script generates the dynamic data for http://zapbot.github.io/zap-mgmt-scripts/addons.html
import glob,json,os,sys
TAG = '2.7'
# Number of days to chart
days = 7
# Maintain running totals so we can work out the differences
counts = {}
# The maximum number of download of each add-on over all days
max = {}
# The number of downloads by day+addon name
downloads = {}
# The dates
dates = []
files = sorted(glob.glob('../zap-mgmt-scripts_gh-pages/stats/ext-releases-*'))
# Need an extra day to get the right baseline
files = files[-(days+1):]
# Print out the column definitions
sys.stdout.write(' [\'Add-on\' ')
for file in files[1:]:
date = file[-15:-5]
sys.stdout.write(', \'' + date + '\'')
dates.append(date)
sys.stdout.write('],\n')
for file in files:
date = file[-15:-5]
with open(file) as stats_file:
stats = json.load(stats_file)
for stat in stats:
if (stat['tag_name'] == TAG):
assets = {}
for asset in stat['assets']:
name = asset['name']
count = asset['download_count']
if (name in counts):
assets[name] = (count - counts[name])
if (name in max):
if (assets[name] > max[name]):
# new max
max[name] = assets[name]
else:
# first one
max[name] = assets[name]
else:
# Baseline
assets[name] = count
counts[name] = count
downloads[date + '-' + name] = assets[name]
# Work out the top downloads
top = sorted(max.items(), key=lambda x: (x[1],x[0]), reverse=True)
top = top[:20]
for addon in top:
name = addon[0]
sys.stdout.write(' [\'' + name + '\'')
for date in dates:
sys.stdout.write(', ')
if (date + '-' + name in downloads):
sys.stdout.write(str(downloads[date + '-' + name]))
else:
sys.stdout.write('0')
sys.stdout.write('],\n')