-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathohrissues.py
276 lines (242 loc) · 10.8 KB
/
ohrissues.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
'''
OHRRPGCE Git Repo Issue Updoot Tabulator
Made for TMC and James, with love.
Rue Lazzaro 7/4/2020
'''
import requests
import os
from pathlib import Path
from io import StringIO
from html.parser import HTMLParser
import sys
issues_url = 'https://api.github.com/repos/ohrrpgce/ohrrpgce/issues'
issues_params = {'state':'open', 'page':'1', 'per_page':'100'}
issues_headers = {'accept': 'application/vnd.github.squirrel-girl-preview'}
class MLStripper(HTMLParser):
def __init__(self):
super().__init__()
self.reset()
self.strict = False
self.convert_charrefs= True
self.text = StringIO()
def handle_data(self, d):
self.text.write(d)
def get_data(self):
return self.text.getvalue()
def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()
def sort_and_prepare_return_final_list(collection,sortmode):
final_list = list()
sort_by_mostupvotes = sorted(collection, key=lambda x: (collection[x]['upvotes']), reverse=True)
sort_by_leastupvotes = sorted(collection, key=lambda x: (collection[x]['upvotes']),reverse=False)
sort_by_mostdownvotes = sorted(collection, key=lambda x: (collection[x]['downvotes']), reverse=True)
sort_by_leastdownvotes = sorted(collection, key=lambda x: (collection[x]['downvotes']),reverse=False)
sort_by_score_highest =sorted(collection, key=lambda x: (collection[x]['score']), reverse=True)
sort_by_score_lowest =sorted(collection, key=lambda x: (collection[x]['score']), reverse=False)
if sortmode == "highest_score":
for each_id in sort_by_score_highest:
final_list.append(collection[str(each_id)])
elif sortmode == "lowest_score":
for each_id in sort_by_score_lowest:
final_list.append(collection[str(each_id)])
elif sortmode == "most_upvotes":
for each_id in sort_by_mostupvotes:
final_list.append(collection[str(each_id)])
elif sortmode == "least_upvotes":
for each_id in sort_by_leastupvotes:
final_list.append(collection[str(each_id)])
elif sortmode == "most_downvotes":
for each_id in sort_by_mostdownvotes:
final_list.append(collection[str(each_id)])
elif sortmode == "least_downvotes":
for each_id in sort_by_leastdownvotes:
final_list.append(collection[str(each_id)])
else:
# I guess let's default to highest score?
for each_id in sort_by_score_highest:
final_list.append(collection[str(each_id)])
return final_list
def get_issues(URL=issues_url,PARAMS=issues_params,HEADERS=issues_headers):
r = requests.get(url = URL, params = PARAMS, headers=HEADERS)
issues_r_data = r.json()
PARAMS2 = PARAMS
del PARAMS["page"]
while 'next' in r.links.keys():
r = requests.get(r.links['next']['url'],headers=HEADERS,params=PARAMS2)
issues_r_data.extend(r.json())
return issues_r_data
def dictify_git(issues_r_data):
issues_data = dict()
for each in issues_r_data:
issue_url = each["url"]
comments_url = each["comments_url"]
issue_id = issue_url
issue_id = issue_id.replace('https://api.github.com/repos/ohrrpgce/ohrrpgce/issues/',"")
issue_id = issue_id.replace("/comments","")
try:
issue_upvotes = each["reactions"]["+1"]
except:
issue_upvotes = 0
try:
issue_downvotes = each["reactions"]["-1"]
except:
issue_downvotes = 0
label = "bug"
try:
for each_label in each["labels"]:
if "feature" in each_label["name"]:
label = "new_feature"
except:
pass
title = each["title"]
title = title.replace(","," ")
title = title.replace("\\","")
title = strip_tags(title)
issues_data[str(issue_id)] = dict()
issues_data[str(issue_id)]["issue_id"] = issue_id
issues_data[str(issue_id)]["title"] = title
issues_data[str(issue_id)]["upvotes"] = issue_upvotes
issues_data[str(issue_id)]["downvotes"] = issue_downvotes
issues_data[str(issue_id)]["label"] = label
issues_data[str(issue_id)]["url"] = 'https://github.com/ohrrpgce/ohrrpgce/issues/'+str(issue_id)
issues_data[str(issue_id)]["score"] = issue_upvotes - issue_downvotes
return issues_data
def write_csv(final_list: list, writefolder='',writefile='OHR-issues.csv',which_label='bug',quiet_mode=False):
try:
with open(writefolder+writefile,'w') as file_out:
file_out.write("ID,title,up,down,url\n")
if quiet_mode == False:
print ("Wrote headers...")
except:
print ("Couldn't write file to "+writefolder+"! Exiting!")
quit()
with open(Path(writefolder+writefile),'a') as file_out:
for each_line in final_list:
if which_label in each_line["label"]:
# Split this into multiple lines to make it more readable.
issue_id = each_line["issue_id"]
title = each_line["title"]
upvotes = each_line["upvotes"]
downvotes = each_line["downvotes"]
visit_url = each_line["url"]
writedata = f'{issue_id},{title},{upvotes},{downvotes},{visit_url}\n'
file_out.write(str(writedata))
if quiet_mode == False:
print ("Wrote "+str(issue_id)+" to "+writefile)
if quiet_mode == False:
print ("Done writing CSV")
def write_html(final_list: list, writefolder='',writefile='ohr-issues.html',quiet_mode=False):
with open(Path(writefolder+writefile),'w') as file_out:
style_text = \
"<style>\n"+\
"table, th, td {\n"+\
"border: 1px solid black;\n"+\
"}\n"+\
"</style>\n\n"
header_text = \
"<head> \n" + \
"<title> OHRRPGCE Issues </title> \n" + \
"<h1> OHRRPGCE Issues </h1> <p>\n"+ \
"</head>\n"
file_out.write(style_text+header_text)
file_out.write("<body>\n<h2> Issues </h2> \n <p> \n <table id=\"issue_table\" width=\"1000px\">\n")
with open(Path(writefolder+writefile),'a') as file_out:
file_out.write(f"\t<tr id=\"headers\">\n \t\t<td>issue_id</td>\n \t\t<td>title</td>\n \t\t<td>upvotes</td>\n \t\t<td>downvotes</td>\n \t\t<td>url</td>\n</tr>")
for each_line in final_list:
if each_line["label"] == "bug":
issue_id = each_line["issue_id"]
title = each_line["title"]
upvotes = each_line["upvotes"]
downvotes = each_line["downvotes"]
visit_url = each_line["url"]
file_out.write(f"\t<tr id=\"{issue_id}\">\n \t\t<td>{issue_id}</td>\n \t\t<td>{title}</td>\n \t\t<td>{upvotes}</td>\n \t\t<td>{downvotes}</td>\n \t\t<td><a href=\"{visit_url}\" target=\"_blank\">{visit_url}</a></td>\n</tr>")
if quiet_mode == False:
print ("Wrote "+str(issue_id)+" to issues table")
file_out.write("</table>\n\n")
file_out.write("\n<h2> Features </h2> \n <table id=\"feature_table\" width=\"1000px\">\n")
file_out.write(f"\t<tr id=\"headers\">\n \t\t<td>issue_id</td>\n \t\t<td>title</td>\n \t\t<td>upvotes</td>\n \t\t<td>downvotes</td>\n \t\t<td>url</td>\n</tr>")
if quiet_mode == False:
print ("\n")
for each_line in final_list:
if each_line["label"] == "new_feature":
issue_id = each_line["issue_id"]
title = each_line["title"]
upvotes = each_line["upvotes"]
downvotes = each_line["downvotes"]
visit_url = each_line["url"]
file_out.write(f"\t<tr id=\"{issue_id}\">\n \t\t<td>{issue_id}</td>\n \t\t<td>{title}</td>\n \t\t<td>{upvotes}</td>\n \t\t<td>{downvotes}</td>\n \t\t<td><a href=\"{visit_url}\" target=\"_blank\">{visit_url}</a></td>\n</tr>")
if quiet_mode == False:
print ("Wrote "+str(issue_id)+" to features table")
file_out.write("</table>\n</body>")
if quiet_mode == False:
print ("Done writing HTML")
def main(fn,sortmode,outputtype,quiet_mode=False):
git_data = get_issues()
issues_dict = dictify_git(git_data)
issues_sorted_dict_list = list()
issues_sorted_dict_list = sort_and_prepare_return_final_list(issues_dict,sortmode)
if outputtype == "html":
write_html(issues_sorted_dict_list,'',fn,quiet_mode)
if outputtype == "csv":
fn2 = fn
fn2 = fn.replace(".csv","-features.csv")
write_csv(issues_sorted_dict_list,'',fn,"bug",quiet_mode) # Bug to filter only labels with 'bug'
write_csv(issues_sorted_dict_list,'',fn2,"feature",quiet_mode) # Feature to get 'feature' requests.
def show_help():
print ("\n")
print ("Syntax: python3 "+__file__+" path_and_filename sortmode")
print ("Where 'path_and_filename is windows or linux friendly folder structure with write access.")
print ("The filename must end in .csv or .html!")
print ("Where sortmode is any of the following: ")
print ("highest_score, lowest_score")
print("most_upvotes,least_upvotes")
print("most_downvotes,least_downvotes")
print ("\n")
print ("Note: CSV mode has 2 output files. One will be your original filename. The second is your filename with '-features' appended to it for the feature requests.")
print ("Note: You may also optionally use the -q argument to enter 'quiet' mode, only displaying errors.")
print ("\n")
quit()
fnOK = False
sortOK = False
if (len(sys.argv) < 3):
show_help()
quit()
if (len(sys.argv) < 2):
show_help()
quit()
if os.path.exists(os.path.dirname(sys.argv[1])) == False:
show_help()
quit()
else:
fn = sys.argv[1]
if fn[-4:] == ".csv":
outputtype = "csv"
elif fn[-5:] == ".html":
outputtype = "html"
else:
print ("Output file must be .csv or .html.")
show_help()
quit()
fnOK = True
fn = sys.argv[1]
sortmode = sys.argv[2]
acceptable_sortmodes = [
'highest_score',
'lowest_score',
'most_upvotes',
'least_upvotes',
'most_downvotes',
'least_downvotes'
]
if sortmode in acceptable_sortmodes:
sortOK = True
if fnOK == True and sortOK == True:
if "-q" in sys.argv:
main(fn,sortmode,outputtype,quiet_mode=True)
else:
main(fn,sortmode,outputtype,quiet_mode=False)
else:
show_help()
quit()