-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquote.py
executable file
·234 lines (180 loc) · 6.08 KB
/
quote.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
#!/usr/bin/env python3
import time
import argparse
import os
import sys
import random
import re
import requests
def logger(level, message):
print('[' + str(int(time.time())) + ']' +
'[' + level + ']' + ' ' + message, file=sys.stderr)
def parse(book):
header = []
body = []
footer = []
mark_head = 1
mark_body = 0
mark_footer = 0
if args.debug:
logger('debug', 'parser is in head')
for line in book:
if 'START OF THE PROJECT' in line or 'START OF THIS PROJECT' in line:
if args.debug:
logger('debug', 'parser is in body')
mark_head = 0
mark_body = 1
continue
if 'END OF THE PROJECT' in line or 'END OF THIS PROJECT' in line:
if args.debug:
logger('debug', 'parser is in footer')
mark_body = 0
mark_footer = 1
continue
# remove leading and trailing whitespace
line.strip()
# correct double spacing
line = re.sub(' +', ' ', line)
# replace smart quotes
line = re.sub('“', '"', line)
line = re.sub('”', '"', line)
line = re.sub('’', "'", line)
if mark_head == 1:
header.append(line)
elif mark_body == 1:
body.append(line)
elif mark_footer == 1:
footer.append(line)
return header, body, footer
def process(header, body):
title_regex = re.compile('^Title:\\s+(.+)')
author_regex = re.compile('^Author:\\s+(.+)')
title = None
author = None
for line in header:
if 'The New McGuffey' in line:
return None, 'book is The New McGuffey Reader'
if 'Language:' in line and 'English' not in line:
return None, "book isn't in English"
if 'Title:' in line:
match = title_regex.search(line)
title = match.group(1)
if args.debug:
logger('debug', 'title: ' + title)
if 'Author:' in line:
match = author_regex.search(line)
author = match.group(1)
if args.debug:
logger('debug', 'author: ' + author)
if title is None:
return None, 'title was not found'
if author is None:
return None, 'author was not found'
build_variable = ''
paragraphs = []
for line in body:
if len(line) > 0:
build_variable = '{}{} '.format(build_variable, line)
else:
paragraphs.append(build_variable)
build_variable = ''
if args.debug:
logger('debug', 'paragraphs found: ' + str(len(paragraphs)))
quote_regex = re.compile('^["].+["]\\s*$')
quotes = []
for paragraph in paragraphs:
match = quote_regex.search(paragraph)
if match is not None:
if len(paragraph) > 90 and len(paragraph) < 113:
quotes.append(paragraph)
if args.debug:
logger('debug', 'quote was found: ' + paragraph)
if len(quotes) == 0:
return None, 'quote was not found'
return quotes, None
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'-d',
'--debug',
help='print more information during the run',
action='store_true')
parser.add_argument(
'-b',
'--book',
help='manually specify the book number',
type=int)
global args
args = parser.parse_args()
try:
with open(os.path.dirname(__file__) + '/catalog.txt') as catalog_fh:
catalog = catalog_fh.read().splitlines()
except Exception as e:
s = str(e)
logger('error', s)
sys.exit(1)
book_number = None
book_name = None
if args.book:
book_number = str(args.book)
for line in catalog:
if line == book_number + '.txt' or line == book_number + '-0.txt':
book_name = line
break
if book_name is None:
logger('error', 'book not found - ' + book_number)
sys.exit(1)
book_number_regex = re.compile('^(\\d+)')
download_error_count = 0
while True:
if args.book is None:
random.seed(a=time.time())
book_name = catalog[random.randint(0, len(catalog) - 1)]
match = book_number_regex.search(book_name)
book_number = str(match.group(1))
page_link = 'https://gutenberg.org/ebooks/' + book_number
book_link = 'https://aleph.pglaf.org'
if len(book_number) == 1:
book_link = book_link + '/0/' + book_number + '/' + book_name
else:
for i in range(len(book_number) - 1):
book_link = book_link + '/' + book_number[i]
book_link = book_link + '/' + book_number + '/' + book_name
if args.debug:
logger('debug', 'page link: ' + page_link)
logger('debug', 'book link: ' + book_link)
response = requests.get(book_link)
response.encoding = 'utf-8'
if response.status_code != 200:
download_error_count = download_error_count + 1
logger(
'error',
'download response was ' +
str(response.status_code) +
' - ' +
book_number)
if args.book:
sys.exit(1)
elif download_error_count == 20:
logger('error', 'download limit (20) reached')
else:
continue
book = response.text.splitlines()
header, body, footer = parse(book)
quotes, error = process(header, body)
if error is not None:
logger('error', error + ' - ' + book_number)
if args.book:
sys.exit(1)
else:
continue
quote = ''
if len(quotes) > 1:
random.seed(a=time.time())
quote = quotes[random.randint(0, len(quotes) - 1)]
else:
quote = quotes[len(quotes) - 1]
print('{} {}'.format(quote, page_link))
break
if __name__ == '__main__':
main()