-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgather_info.py
59 lines (35 loc) · 1.03 KB
/
gather_info.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
from gutenberg.query import get_metadata
from metadata import metadata
import re
def isvalid(id_num):
"""
Check if a gutenberg book is an english textbook.
Args:
id_num: id of gutenberg book.
Returns:
a boolean which shows if id_num is id of an english text book
"""
try:
language = metadata(id_num)['language']
form = get_metadata('formaturi', id_num)
if 'en' not in language:
return False
form = ' '.join(form)
if re.search(r'\d+\.txt', form):
return True
return False
except:
return False
def find_books_html(text):
"""
Gather all books ids which are linked in a web page.
Args:
text: raw-text of a web page
Returns:
a list of integers which is all id of books find in that page
"""
links = re.findall(r'https://www.gutenberg.org/ebooks/.*', text)
links = ' '.join(links)
ids = re.findall(r'\d+', links)
ids = [int(x) for x in ids]
return ids