Skip to content

Commit 8ab3349

Browse files
author
sheepzh
committed
Upload count trend chart to Gist
1 parent c710d06 commit 8ab3349

File tree

2 files changed

+110
-21
lines changed

2 files changed

+110
-21
lines changed

script/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pygal
2-
cairosvg
2+
cairosvg
3+
requests

script/user_count.py

Lines changed: 108 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import sys
44
import json
55
import os
6-
import math
76
import cairosvg
7+
import requests
8+
import math
89

9-
argv = sys.argv
10+
svg_file_path = os.path.join('output', 'user_count.svg')
1011

1112

1213
def smooth_count(last_value, step_num, current_value, data):
@@ -16,6 +17,20 @@ def smooth_count(last_value, step_num, current_value, data):
1617
data.append(current_value)
1718

1819

20+
def quit_with(msg: str):
21+
print(msg)
22+
quit()
23+
24+
25+
def zoom(data, reduction):
26+
i = 0
27+
new_data = []
28+
while i < len(data):
29+
new_data.append(data[i])
30+
i += reduction
31+
return new_data
32+
33+
1934
def render():
2035
edge_user = read_json('user_edge')
2136
chrome_user = read_json('user_chrome')
@@ -59,7 +74,13 @@ def render():
5974
smooth_count(last_chrome, chrome_step, last_chrome, chrome_data)
6075
smooth_count(last_edge, edge_step, last_edge, edge_data)
6176
smooth_count(last_firefox, firefox_step, last_firefox, firefox_data)
62-
print(chrome_data, edge_data, firefox_data)
77+
78+
data_size = len(chrome_data)
79+
reduction = math.floor(data_size/100)
80+
chrome_data = zoom(chrome_data, reduction)
81+
edge_data = zoom(edge_data, reduction)
82+
firefox_data = zoom(firefox_data, reduction)
83+
sorted_date = zoom(sorted_date, reduction)
6384

6485
chart = pygal.StackedLine(
6586
fill=True, style=pygal.style.styles['default'](label_font_size=8))
@@ -70,10 +91,9 @@ def render():
7091
chart.add('Chrome', chrome_data)
7192
chart.add('Edge', edge_data)
7293
svg = chart.render()
73-
file_name = 'output/user_count.svg'
74-
with open(file_name, 'wb') as svg_file:
94+
with open(svg_file_path, 'wb') as svg_file:
7595
svg_file.write(svg)
76-
cairosvg.svg2svg(file_obj=open(file_name, 'r'), write_to=file_name)
96+
cairosvg.svg2svg(file_obj=open(svg_file_path, 'r'), write_to=svg_file_path)
7797

7898

7999
def read_json(name):
@@ -101,8 +121,7 @@ def write_json(name, json_obj):
101121
def add_chrome(file_path):
102122
json_file = 'user_chrome'
103123
if not os.path.exists(file_path):
104-
print("File not found", file_path)
105-
quit()
124+
quit_with("File not found: {}".format(file_path))
106125
# 1. parse input data
107126
input_data = {}
108127
with open(file_path, encoding='utf8', mode='r') as csv_file:
@@ -132,8 +151,7 @@ def add_chrome(file_path):
132151
def add_edge(file_path):
133152
json_file = 'user_edge'
134153
if not os.path.exists(file_path):
135-
print("File not found", file_path)
136-
quit()
154+
quit_with("File not found: {}".format(file_path))
137155
# 1. parse input data
138156
input_data = {}
139157
with open(file_path, encoding='utf8', mode='r') as csv_file:
@@ -164,8 +182,7 @@ def add_edge(file_path):
164182
def add_firefox(file_path):
165183
json_file = 'user_firefox'
166184
if not os.path.exists(file_path):
167-
print("File not found", file_path)
168-
quit()
185+
quit_with("File not found: {}".format(file_path))
169186
# 1. parse input data
170187
input_data = {}
171188
with open(file_path, encoding='utf8', mode='r') as csv_file:
@@ -191,9 +208,9 @@ def add_firefox(file_path):
191208

192209

193210
def add():
211+
argv = sys.argv
194212
if len(argv) < 4 or argv[2] not in ['c', 'e', 'f']:
195-
print("add [c/e/f] [file_name]")
196-
quit()
213+
quit_with("add [c/e/f] [file_name]")
197214
browser = argv[2]
198215
file_path = argv[3]
199216
if browser == 'c':
@@ -206,21 +223,92 @@ def add():
206223
pass
207224

208225

226+
GIST_TOKEN_ENV = 'TIMER_USER_COUNT_GIST_TOKEN'
227+
DESC = "User count of timer, auto-generated"
228+
229+
230+
def upload2gist():
231+
argv = sys.argv
232+
# 1. find token and svg file
233+
token = argv[2] if len(argv) > 2 else os.environ.get(GIST_TOKEN_ENV)
234+
if not token:
235+
quit_with(
236+
"Token is None, please input with command or set with environment TIMER_USER_COUNT_GIST_TOKEN"
237+
)
238+
if not os.path.exists(svg_file_path):
239+
quit_with('Svg file not found')
240+
# 2. find exist gist file
241+
token_header = 'Bearer {}'.format(token)
242+
headers = {
243+
"Accept": "application/vnd.github+json",
244+
"Authorization": token_header
245+
}
246+
response = requests.get('https://api.github.com/gists', headers=headers)
247+
status_code = response.status_code
248+
if status_code != 200:
249+
quit_with("Failed to list exist gists: statusCode={}".format(status_code))
250+
gist_list = json.loads(response.content)
251+
exist_gist = next(
252+
(gist for gist in gist_list if 'description' in gist and gist['description'] == DESC),
253+
None
254+
)
255+
svg_content = ''
256+
with open(svg_file_path, 'r') as file:
257+
svg_content = '\r\n'.join(file.readlines())
258+
if not svg_content:
259+
quit_with("Failed to read svg file")
260+
data = json.dumps({
261+
"description": DESC,
262+
"public": True,
263+
"files": {
264+
"user_count.svg": {
265+
"content": svg_content
266+
}
267+
}
268+
})
269+
# 3. create new one or update
270+
if not exist_gist:
271+
print("Gist not found, so try to create one")
272+
response = requests.post('https://api.github.com/gists',
273+
data=data, headers=headers)
274+
status_code = response.status_code
275+
if status_code != 200 and status_code != 201:
276+
quit_with(
277+
'Failed to create new gist: statusCode={}'.format(status_code)
278+
)
279+
else:
280+
print('Success to create new gist')
281+
else:
282+
gist_id = exist_gist['id']
283+
response = requests.post('https://api.github.com/gists/{}'.format(gist_id),
284+
data=data, headers=headers)
285+
status_code = response.status_code
286+
if status_code != 200 and status_code != 201:
287+
quit_with("Failed to update gist: id={}, statusCode={}".format(
288+
gist_id,
289+
status_code
290+
))
291+
else:
292+
print("Success to update gist")
293+
294+
209295
def main():
210-
if len(argv) == 1 or argv[1] not in ['render', 'add']:
211-
print("Supported command: render, add")
296+
argv = sys.argv
297+
if len(argv) == 1 or argv[1] not in ['render', 'add', 'upload']:
298+
print("Supported command: render, add, upload")
212299
quit()
213300

214301
cmd = argv[1]
215302
if cmd == 'render':
216303
render()
217304
elif cmd == 'add':
218305
add()
306+
elif cmd == 'upload':
307+
render()
308+
upload2gist()
219309
else:
220310
pass
221311

222312

223-
main()
224-
225-
226-
# print(lineChart)
313+
if __name__ == '__main__':
314+
main()

0 commit comments

Comments
 (0)