-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
201 lines (162 loc) · 5.17 KB
/
utils.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
#!/usr/bin/env python
import re
import os
import io
import sys
import glob
from zipfile import BadZipfile
import epub
import simplejson
from elasticsearch import Elasticsearch
from elasticsearch.exceptions import ConnectionError, RequestError
from bs4 import BeautifulSoup
from app import app
__all__ = [
'parse_epub',
'list_epub_files',
'create_index',
'delete_index',
'index_data',
'create_index',
]
def create_index(name):
"""Create an elastic search index"""
es_client = Elasticsearch()
schema = None
with io.FileIO(app.config['ES_INDEX_SCHEMA'], 'r') as stream:
schema = simplejson.loads(stream.read())
try:
es_client.indices.create(index=name, body=schema)
print 'Successfully created index: {}'.format(app.config['ES_INDEX_NAME'])
except ConnectionError ,e:
sys.exit(e.error)
except RequestError, e:
sys.exit(e.error)
def delete_index(name):
"""Create an elastic search index"""
es_client = Elasticsearch()
try:
es_client.indices.delete(index=name)
print 'Successfully deleted index: {}'.format(app.config['ES_INDEX_NAME'])
except ConnectionError ,e:
sys.exit(e.error)
except RequestError, e:
sys.exit(e.error)
def index_data(index_name, epub_path):
"""Index magazine data"""
es_client = Elasticsearch()
files = list_epub_files(epub_path)
for _file in files:
data = parse_epub(_file)
magazine_title = data['title']
articles = data['articles']
print 'Indexing {} articles in {}'.format(len(articles), magazine_title)
for article in articles:
document = {
'name': magazine_title,
'title': article['title'],
'author': article['author'],
'content': article['content']
}
try:
es_client.index(index=index_name, doc_type='articles', body=document)
except ConnectionError ,e:
sys.exit(e.error)
except RequestError, e:
sys.exit(e.error)
def query_index(name, query):
"""Query index for data"""
es_client = Elasticsearch()
base_query = {
"size": 20,
"sort": ["_score"],
"query": {
"multi_match" : {
"query": "",
"fields": [
"name",
"title",
"title.partial.front",
"title.partial.middle",
"author",
"author.partial.front",
"content",
"content.partial.front",
"content.partial.middle",
"content.partial.back"
]
}
}
}
base_query['query']['multi_match']['query'] = query
try:
results = es_client.search(index=name, body=base_query)
if 'hits' in results['hits']:
if len(results['hits']['hits']) > 0:
return results['hits']['hits']
return []
except ConnectionError ,e:
sys.exit(e.error)
except RequestError, e:
sys.exit(e.error)
def clean_data(data):
"""Clean up unwanted markup in data"""
data = data.strip()
data = data.replace('\n', ' ')
return data
def find_article_title(soup):
"""Find the title of an article using BeautifulSoup"""
title = soup.find('h1')
if title:
return clean_data(title.text)
return None
def find_article_author(soup):
"""Find the author of an article using BeautifulSoup"""
author = soup.find_all(text=re.compile(r"By\s*.*"))
if author:
return clean_data(author[0].replace('By ', ''))
return None
def find_article_content(soup):
"""Find the content of an article using BeautifulSoup"""
content = []
for tag in soup.find_all(['p', 'pre']):
content.append(clean_data(tag.text))
return ''.join(content)
def parse_epub(_file):
"""Parse the epub file and return a dictionary containing
information about the magazine and articles"""
data = {
'title': '',
'articles': []
}
try:
magazine = epub.open_epub(_file)
except BadZipfile:
return data
# get the title of the magazine
data['title'] = magazine.toc.title
for item_id, _ in magazine.opf.spine.itemrefs:
item = magazine.get_item(item_id)
contents = magazine.read_item(item)
soup = BeautifulSoup(contents)
article = {}
title = find_article_title(soup)
author = find_article_author(soup)
content = find_article_content(soup)
if all([title, author, content]):
article['title'] = title
article['author'] = author
article['content'] = content
data['articles'].append(article)
return data
def list_epub_files(path):
"""Get a list of epub files with absolute path"""
files = []
if os.path.isdir(path):
magazines = glob.glob(os.path.join(path, '*.epub'))
for magazine in magazines:
_file = os.path.join(path, magazine)
files.append(_file)
return files
if __name__ == '__main__':
run()