-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathextract_id_title_url.py
180 lines (157 loc) · 6.06 KB
/
extract_id_title_url.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
#!/usr/bin/env python2.7
# encoding: utf-8
# Originally from: https://github.com/webcompat/issue_parser/blob/master/extract_id_title_url.py
'''
extract_id_title.py
Created by Hallvord R. M. Steen on 2014-10-25.
Modified by Karl
Mozilla Public License, version 2.0
see LICENSE
Dumps data from webcompat.com bug tracker
by default creates one CSV file (webcompatdata.csv)
and one JSON file (webcompatdata-bzlike.json)
the JSON file uses many of the field names Bugzilla uses in its export,
so the output from this script can be used where Bugzilla data is expected
'''
import csv
import json
import re
import socket
import sys
import time
from urllib.request import Request
from urllib.request import urlopen
# Config
URL_REPO = "https://api.github.com/repos/webcompat/web-bugs"
VERBOSE = True
# Seconds. Loading searches can be slow
socket.setdefaulttimeout(240)
def get_remote_file(url, req_json=False):
while True:
try:
print('Getting ' + url)
req = Request(url)
req.add_header('User-agent', 'AreWeCompatibleYetBot')
if req_json:
req.add_header('Accept', 'application/vnd.github.v3+json')
bzresponse = urlopen(req, timeout=240)
return {"headers": bzresponse.info(),
"data": json.loads(bzresponse.read().decode('utf8'))}
except:
print('Wait ten minutes before next request...')
time.sleep(600)
def extract_url(issue_body):
'''Extract the URL for an issue from WebCompat.
URL in webcompat.com bugs follow this pattern:
**URL**: https://example.com/foobar
'''
url_pattern = re.compile('\*\*URL\*\*\: (.*)\n')
url_match = re.search(url_pattern, issue_body)
if url_match:
url = url_match.group(1).strip()
if not url.startswith(('http://', 'https://')):
url = "http://%s" % url
else:
url = ""
return url
def extract_data(json_data, results_csv, results_bzlike):
resolution_labels = ["duplicate", "invalid", "wontfix", "fixed",
"worksforme"]
whiteboard_labels = ["needsinfo", "contactready", "sitewait",
"needscontact", "needsdiagnosis"]
for issue in json_data["data"]:
# Extracting data
body = issue["body"]
url = extract_url(body)
bug_id = issue["number"]
link = 'https://webcompat.com/issues/%s' % bug_id
issue_title = issue["title"].strip()
if VERBOSE:
print('Issue %s: %s' % (bug_id, issue_title))
creation_time = issue['created_at']
last_change_time = issue['updated_at']
issue_state = issue['state']
cf_last_resolved = issue['closed_at']
if issue_state == 'open':
status = 'OPEN'
else:
status = 'RESOLVED'
# Extracting the labels
labels_list = [label['name'] for label in issue['labels']]
# areWEcompatibleyet is only about mozilla bugs
if any([('firefox' or 'mozilla') in label for label in labels_list]):
# Defining the OS
if any(['mobile' in label for label in labels_list]):
op_sys = 'Gonk (Firefox OS)'
elif any(['android' in label for label in labels_list]):
op_sys = 'Android'
else:
op_sys = ''
# Did the bug had a resolution?
resolution = ''
resolution_set = set(labels_list).intersection(resolution_labels)
if resolution_set:
resolution = resolution_set.pop().upper()
# Gathering Whiteboard keys
whiteboard = ''.join(['[%s] ' % label for label in labels_list
if label in whiteboard_labels])
# creating CSV file
if issue_title:
results_csv.append("%i\t%s\t%s\t%s" % (
bug_id, issue_title, url, link))
# Creating dictionary
bzlike = {"id": bug_id,
"summary": issue_title,
"url": url,
"whiteboard": whiteboard,
"op_sys": op_sys,
"creation_time": creation_time,
"last_change_time": last_change_time,
"status": status,
"cf_last_resolved": cf_last_resolved,
"resolution": resolution,
"body": body
}
results_bzlike.append(bzlike)
def extract_next_link(link_hdr):
'''Given a HTTP Link header, extract the "next" link.
Link header has the pattern:
'<https://example.com/foobar?page=2>; rel="next",
<https://example.com/foobar?page=100>; rel="last"'
We need:
https://example.com/foobar?page=2
When no more "next", we return an empty string.
'''
next_link = ''
links = link_hdr.split(',')
for link in links:
link_only, rel = link.split(';')
if 'next' in rel:
next_link = link_only.strip(' <>')
break
return next_link
def get_webcompat_data(url_repo=URL_REPO):
'''Extract Issues data from github repo.
Start with the first page and follow hypermedia links to explore the rest.
'''
next_link = '%s/issues?per_page=100&page=1&filter=all&state=all' % (url_repo)
results = []
bzresults = []
while next_link:
response_data = get_remote_file(next_link, True)
extract_data(response_data, results, bzresults)
next_link = extract_next_link(response_data["headers"]["link"])
return results, {"bugs": bzresults}
def main():
results, bzresults = get_webcompat_data(URL_REPO)
# webcompatdata.csv
with open('webcompatdata.csv', 'w') as f:
f.write("\n".join(results))
print("Wrote {} items to webcompatdata.csv ".format(len(results)))
# webcompatdata-bzlike.json
with open('webcompatdata-bzlike.json', 'w') as f:
f.write(json.dumps(bzresults, indent=4))
print("Wrote {} items to webcompatdata-bzlike.json".format(
len(bzresults['bugs'])))
if __name__ == "__main__":
sys.exit(main())