-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGitHub-issues-to-pdf.py
268 lines (212 loc) · 9.19 KB
/
GitHub-issues-to-pdf.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
### This script scrapes the issues for a github project, and saves each one as a PDF.
import pdfkit
import requests
import re
import os
from bs4 import BeautifulSoup
from datetime import datetime
# OPTIONS:
# Repository to fetch from (e.g. jackjamieson2/GitHub-issues-to-pdf)
repository = 'jackjamieson2/yarns-indie-reader'
# Output directory to save PDFs
output_dir = 'Exported PDFs/' + repository + "/"
# Generate automatic tags (True/False)
generate_auto_tags = True # If true will add automatically generated tags to
# bottom of the PDF in the form ##[tag]. See autotags() function for details
print("starting...")
# Autotags
def autotags(soup):
referenced = False
commit_found = False
tags = "<br><h1>Tags</h1>"
tags+="<br>###status: " + soup.select(".TableObject-item .State")[0].text
for item in soup.select('.discussion-item'):
if str(item).find('This was referenced')>=0:
referenced = True
if str(item).find('referenced this issue')>=0:
referenced = True
if str(item).find('id="ref-commit-')>=0:
commit_found = True
if referenced == True:
tags+="<br>###referenced"
if commit_found == True:
tags+="<br>###referenced_in_commit"
for item in soup.select('.labels a'):
tags+="<br>###current_label: " + item.text
for item in soup.select('.IssueLabel a'):
tags+="<br>###past_or_present_label: " + item.text
participants_N = len(soup.select('.participant-avatar'))
if participants_N ==1:
tags+="<br>###1_participant"
elif participants_N ==2:
tags+="<br>###2_participants"
elif participants_N >2:
tags+="<br>###>=3_participants" + "(" + str(participants_N) + ")"
for item in soup.select('.participant-avatar'):
participant_name = re.sub("/","",item.get('href'))
tags+="<br>###participant: " + participant_name
for item in soup.select('.assignee'):
tags+="<br>###assignee: " + item.text
return tags
def log_error(error):
if not os.path.isfile(output_dir + "error_log.txt"):
# Log file does not exist, so write explanatory header
with open(output_dir + "error_log.txt", "a") as myfile:
myfile.write("Errors reported for the following URLs, please check to ensure the generated PDFs are correct.")
with open(output_dir + "error_log.txt", "a") as myfile:
myfile.write("\n\n" + str(datetime.now()) + "\n" + error)
myfile.close()
return
#Options
options = {
'dpi':'300' # This zooms in to make the PDFs more readable (recommended)
}
# Look up how many issues the repository has
issue_count = 0
r = requests.get('https://github.com/' + repository + '/issues?q=is%3Aissue')
if r.status_code == 200:
soup = BeautifulSoup(r.content, "lxml")
issue = soup.find(class_="js-issue-row")
issue_count = int(re.sub('issue_',"",issue.get('id')))
print(str(issue_count) + " issues found")
# Create the output folder if it doesn't exist
if not os.path.exists(output_dir):
os.makedirs(output_dir)
errors = []
# Iterate through each issue page
for i in range (1,issue_count +1):
url = 'https://github.com/' + repository + '/issues/' + str(i)
r = requests.get(url)
if r.status_code == 200:
print('\nConverting page to PDF: ' + url)
c = r.text
# Strip versioning number from <link> paths (e.g. example.css?1234 -> example.css)
# This is needed to avoid an error with wkpdftohtml
# see thread at https://github.com/wkhtmltopdf/wkhtmltopdf/issues/2051
html = re.sub('#(\.css|\.js)\?[^"]+#', '$1', c)
soup = BeautifulSoup(html, "lxml")
html_head = str(soup.head)
html_body = str(soup.find(class_='repohead'))
html_body = str(html_body) + str(soup.find(id='show_issue'))
if generate_auto_tags == True:
tags = autotags(soup)
else:
tags = ""
full_html = html_head + html_body + tags
try:
### This script scrapes the issues for a github project, and saves each one as a PDF.
import pdfkit
import requests
import re
import os
from bs4 import BeautifulSoup
from datetime import datetime
# OPTIONS:
# Repository to fetch from (e.g. jackjamieson2/GitHub-issues-to-pdf)
repository = 'jackjamieson2/yarns-indie-reader'
# Output directory to save PDFs
output_dir = 'Exported PDFs/' + repository + "/"
# Generate automatic tags (True/False)
generate_auto_tags = True # If true will add automatically generated tags to
# bottom of the PDF in the form ##[tag]. See autotags() function for details
print("starting...")
# Autotags
def autotags(soup):
referenced = False
commit_found = False
tags = "<br><h1>Tags</h1>"
tags+="<br>###status: " + soup.select(".TableObject-item .State")[0].text
for item in soup.select('.discussion-item'):
if str(item).find('This was referenced')>=0:
referenced = True
if str(item).find('referenced this issue')>=0:
referenced = True
if str(item).find('id="ref-commit-')>=0:
commit_found = True
if referenced == True:
tags+="<br>###referenced"
if commit_found == True:
tags+="<br>###referenced_in_commit"
for item in soup.select('.labels a'):
tags+="<br>###current_label: " + item.text
for item in soup.select('.IssueLabel a'):
tags+="<br>###past_or_present_label: " + item.text
participants_N = len(soup.select('.participant-avatar'))
if participants_N ==1:
tags+="<br>###1_participant"
elif participants_N ==2:
tags+="<br>###2_participants"
elif participants_N >2:
tags+="<br>###>=3_participants" + "(" + str(participants_N) + ")"
for item in soup.select('.participant-avatar'):
participant_name = re.sub("/","",item.get('href'))
tags+="<br>###participant: " + participant_name
for item in soup.select('.assignee'):
tags+="<br>###assignee: " + item.text
return tags
def log_error(error):
if not os.path.isfile(output_dir + "error_log.txt"):
# Log file does not exist, so write explanatory header
with open(output_dir + "error_log.txt", "a") as myfile:
myfile.write("Errors reported for the following URLs, please check to ensure the generated PDFs are correct.")
with open(output_dir + "error_log.txt", "a") as myfile:
myfile.write("\n\n" + str(datetime.now()) + "\n" + error)
myfile.close()
return
#Options
options = {
'dpi':'300' # This zooms in to make the PDFs more readable (recommended)
}
# Look up how many issues the repository has
issue_count = 0
r = requests.get('https://github.com/' + repository + '/issues?q=is%3Aissue')
if r.status_code == 200:
soup = BeautifulSoup(r.content, "lxml")
issue = soup.find(class_="js-issue-row")
issue_count = int(re.sub('issue_',"",issue.get('id')))
print(str(issue_count) + " issues found")
# Create the output folder if it doesn't exist
if not os.path.exists(output_dir):
os.makedirs(output_dir)
errors = []
# Iterate through each issue page
for i in range (1,issue_count +1):
url = 'https://github.com/' + repository + '/issues/' + str(i)
r = requests.get(url)
if r.status_code == 200:
print('\nConverting page to PDF: ' + url)
c = r.text
# Strip versioning number from <link> paths (e.g. example.css?1234 -> example.css)
# This is needed to avoid an error with wkpdftohtml
# see thread at https://github.com/wkhtmltopdf/wkhtmltopdf/issues/2051
html = re.sub('#(\.css|\.js)\?[^"]+#', '$1', c)
soup = BeautifulSoup(html, "lxml")
html_head = str(soup.head)
html_body = str(soup.find(class_='repohead'))
html_body = str(html_body) + str(soup.find(id='show_issue'))
if generate_auto_tags == True:
tags = autotags(soup)
else:
tags = ""
full_html = html_head + html_body + tags
try:
if soup.find(id='show_issue'):
pdfkit.from_string(full_html, output_dir +str(i) +'.pdf', options=options)
else:
print('\nIssue does not exist:' + url)
except:
log_error(url)
elif r.status_code == 404:
print('\n404 not found: ' + url )
print('\n\nFinished!\nSaved PDFs for ' + str(i) + ' issues.' )
print('Find your exported PDFs in ' +output_dir )
else:
print("Repository not found: " + repository)
except:
log_error(url)
elif r.status_code == 404:
print('\n404 not found: ' + url )
print('\n\nFinished!\nSaved PDFs for ' + str(i) + ' issues.' )
print('Find your exported PDFs in ' +output_dir )
else:
print("Repository not found: " + repository)