-
Notifications
You must be signed in to change notification settings - Fork 0
/
wbuild.py
executable file
·342 lines (297 loc) · 11.4 KB
/
wbuild.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env python3
import os
import sys
import http.cookiejar
import logging
import re
import lxml.html
import requests
from requests.exceptions import RequestException, BaseHTTPError
from bs4 import BeautifulSoup
import argparse
import textwrap
ARGS_ERROR_EXIT_CODE = 1
HTTP_ERROR_EXIT_CODE = 2
class HttpUtils:
class GetPageError(Exception):
"""
The exception raised when smth went wrong in 'get_page' function.
"""
def __init__(self, desc, url):
super().__init__(desc, url)
self.desc = desc
self.url = url
def __str__(self):
tmpl = 'The error "%s" occured while getting the page %s'
return tmpl % (self.desc, self.url)
def __init__(self, cookies_file=None):
self.cookies_file = cookies_file
self.session = requests.Session()
self.load_cookies()
self.set_headers()
def load_cookies(self):
if not self.cookies_file:
return
if not os.path.isfile(self.cookies_file):
raise NameError('Cannot find cookies file %s' % self.cookies_file)
cookie_jar = http.cookiejar.MozillaCookieJar()
cookie_jar.load(self.cookies_file)
self.session.cookies = cookie_jar
def set_headers(self):
self.session.headers = {'Accept': 'text/html'}
def get_page(self, url, utf8=False):
"""
Get HTML page or raise GetPageError exception.
If content-type header isn't 'text/html' the exception raised as well
as when download error occured.
"""
try:
req = self.session.get(url)
except (RequestException, BaseHTTPError):
raise HttpUtils.GetPageError(
'An exception occured at the http request performing', url)
if req.status_code != requests.codes['ok']:
raise HttpUtils.GetPageError(
'HTTP status code: %d' % req.status_code, url)
if utf8:
req.encoding = 'utf-8'
content_type = req.headers['content-type']
if content_type.startswith('text/html'):
return req.text
else:
raise HttpUtils.GetPageError(
'Content type "%s" != "text/html"' % content_type, url)
@staticmethod
def full_url(url, context_url):
"""
Get full (absolute) URL from arbitrary URL and page where it placed.
Assume 'context_url' are full url.
"""
proto, tail = context_url.split(':', 1)
context_base = proto + '://' + tail.lstrip('/').split('/', 1)[0]
if url.startswith('#'):
context_page = context_url.split('#', 1)[0]
return context_page + url
elif url.startswith('//'):
return proto + ':' + url
elif url.startswith('/'):
return context_base.rstrip('/') + '/' + url.lstrip('/')
elif url.startswith(('http://', 'https://', 'ftp://')):
return url
else:
# Need we support relational link like
# 'smth.html', './smth.html', or '../smth.html'?
raise NameError('bad url in \'full_url\':\nurl: ' + url + '\n')
class Notabenoid:
"""
http://notabenoid.org
"""
books = {
'whatif': {
'url': 'http://notabenoid.org/book/41531',
'filter_re': r'^what-if #\d+\.? .*$',
},
'xkcd': {
'url': 'http://notabenoid.org/book/45995',
'filter_re': None,
},
}
def __init__(self, book_name, http_utils):
self.book_name = book_name
if self.book_name not in Notabenoid.books.keys():
raise NameError('Unknown Notabenoid book: ' + self.book_name)
self.book = Notabenoid.books[book_name]
self.http_utils = http_utils
self.list_doc = None
self.article_doc = None
def get_list_of_articles(self, filtering=False):
if self.list_doc is None:
html = self.http_utils.get_page(self.book['url'])
self.list_doc = lxml.html.document_fromstring(html)
articles = []
links = self.list_doc.cssselect('table#Chapters tr td.t a')
for link in links:
title = link.text
url = HttpUtils.full_url(link.get('href'), self.book['url'])
article = (title, url)
if filtering and self.book['filter_re']:
if re.match(self.book['filter_re'], title):
articles.append(article)
else:
articles.append(article)
return articles
def get_original(self, url):
if self.article_doc is None:
html = self.http_utils.get_page(url)
self.article_doc = lxml.html.document_fromstring(html)
fragments = []
for elem in self.article_doc.cssselect('table#Tr td.o div p.text'):
elem_html = lxml.html.tostring(elem)
elem_text = BeautifulSoup(elem_html, 'html.parser').text
fragments.append(elem_text)
return fragments
def get_translation(self, url, page=None):
if self.article_doc is None or page is not None:
if page:
html = self.http_utils.get_page(url + page)
else:
html = self.http_utils.get_page(url)
self.article_doc = lxml.html.document_fromstring(html)
groups = []
for group_elem in self.article_doc.cssselect('table#Tr td.t'):
group_id = group_elem.getparent().cssselect(
'td.o p.info a.ord')[0].text
group = {
'fragments': [],
'id': group_id,
}
for elem in group_elem.xpath('./div[@id]'):
elem_html = lxml.html.tostring(elem.cssselect('p.text')[0])
text = BeautifulSoup(elem_html, 'html.parser').text
text = re.sub(r'(^|\n)<-->', r'\g<1> ', text)
text = re.sub(r'^TODO: replace \'<-->\' with \' \'\n', '',
text)
text = re.sub(r'\n\[labels\].+\[/labels\]', '', text,
flags=re.DOTALL)
author = elem.cssselect('p.info a.user')[0].text
rating = int(elem.cssselect('div.rating a.current')[0].text)
fragment = {
'text': text,
'author': author,
'rating': rating,
}
group['fragments'].append(fragment)
groups.append(group)
next_pages = self.article_doc.cssselect('div#pages-bottom p.n a')
if len(next_pages):
groups.extend(self.get_translation(url, next_pages[0].get('href')))
return groups
def format_help(s):
"""
Remove indentation and add an empty line at the end.
"""
return textwrap.dedent(s.lstrip('\n')) + '\n'
def get_args():
""" Check and get arguments. Exit with a message when smth went wrong. """
parser = argparse.ArgumentParser(
description=format_help(
"""
The tool for grabbing Russian translation of What If? articles from
Notabenoid.
"""),
formatter_class=argparse.RawTextHelpFormatter,
add_help=False)
# Add the --help argument explicitly to format its message
# in accordance to other ones: start from a capital letter
# and leave an empty line at the end.
parser.add_argument(
'-h', '--help',
action='help',
default=argparse.SUPPRESS,
help=format_help(
"""
Show this help message and exit.
"""))
parser.add_argument(
'--all',
action='store_true',
help=format_help(
"""
Print list of articles, originals and all translations' variants.
By default only last of top rated translations' variants will be
printed.
"""))
parser.add_argument('cookies_file')
try:
args = parser.parse_args()
except argparse.ArgumentError as exc:
logging.critical(str(exc))
parser.print_usage(file=sys.stderr)
exit(ARGS_ERROR_EXIT_CODE)
return args
def print_all(args, notabenoid):
articles = notabenoid.get_list_of_articles(filtering=True)
for article in articles:
print(str(article))
print('')
last_article = articles[0]
fragments = notabenoid.get_original(last_article[1])
for fragment in fragments:
print(fragment + '\n')
groups = notabenoid.get_translation(last_article[1])
for i, group in enumerate(groups):
maybe_newline = ('\n' if i < len(groups) - 1 else '')
for fragment in group['fragments']:
print(fragment['text'])
print('%s %s %d%s' % (group['id'], fragment['author'],
fragment['rating'], maybe_newline))
def print_ambiguity(ambiguity):
if len(ambiguity) > 0:
logging.warning('')
logging.warning('Ambiguity fragments')
logging.warning('===================')
for num, fragments in ambiguity:
for fragment in fragments:
logging.warning('')
logging.warning(fragment['text'])
logging.warning('#%d %s %d', num, fragment['author'],
fragment['rating'])
logging.warning('')
logging.warning('========')
if len(ambiguity) > 0:
logging.warning('')
logging.warning('WARNING!')
logging.warning(
'There are ambiguity fragments, last one choosed in each group.')
logging.warning(
'Check the output above to be sure the script selects it right.')
def print_top(args, notabenoid):
articles = notabenoid.get_list_of_articles(filtering=True)
last_article = articles[0]
orig_fragments = notabenoid.get_original(last_article[1])
groups = notabenoid.get_translation(last_article[1])
ambiguity = []
for i, group in enumerate(groups):
maybe_newline = ('\n' if i < len(groups) - 1 else '')
top_rating = 0
top_rated = []
for fragment in group['fragments']:
if fragment['rating'] == top_rating:
top_rated.append(fragment)
elif fragment['rating'] > top_rating:
top_rated = [fragment]
top_rating = fragment['rating']
if len(top_rated) > 0:
print(top_rated[-1]['text'] + maybe_newline)
else:
logging.warning('')
logging.warning('WARNING!')
logging.warning('The original fragment was used because no ' +
'translations found for it.')
print(orig_fragments[i] + maybe_newline)
if len(top_rated) > 1:
ambiguity.append((i, top_rated))
print_ambiguity(ambiguity)
def prettify_logging_shorter():
""" Setup logger format. """
# TODO: colors when isatty()
root_logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter('{message}', style='{')
handler.setFormatter(formatter)
root_logger.addHandler(handler)
def main():
prettify_logging_shorter()
args = get_args()
http_utils = HttpUtils(cookies_file=args.cookies_file)
try:
notabenoid = Notabenoid('whatif', http_utils)
except HttpUtils.GetPageError as exc:
logging.critical(str(exc))
exit(HTTP_ERROR_EXIT_CODE)
if args.all:
print_all(args, notabenoid)
else:
print_top(args, notabenoid)
if __name__ == '__main__':
main()