-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskeleton.py
280 lines (270 loc) · 9.4 KB
/
skeleton.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
# from bs4 import BeautifulSoup
from requests_html import HTMLSession
import json
from dotenv import load_dotenv
import os
from pymongo import MongoClient
import PyPDF2, io
load_dotenv(".env")
client = MongoClient(os.environ.get("MONGO_URL"))
infoDB, infoCollection = None, None
try:
infoDB = client['info-db']
infoCollection = infoDB['info-collection']
except:
print("cannot connect to DB")
class Website:
def __init__(self, url):
self.url = url
self.title = None
self.summary = None
self.paragraphs = None
self.links = None
self.saved = False
# try to retrieve info from database
info = None
if(infoCollection != None): info = infoCollection.find_one({'url':self.url})
if(info):
self.saved = True
self.title = info['title']
self.url = info['url']
self.summary = info['summary']
self.links = info['links']
self.paragraphs = info['paragraphs']
return
self.html = self.render()
def getURL(self):
return self.url
def getTitle(self):
if(self.title == None):
try:
self.title = self.html.find('title')[0].text
except:
self.title = f"title for {self.url}"
return self.title
def getSummary(self):
if(self.summary == None):
try:
attempts = 0
while(attempts < 30):
try: self.summary = self.html.find('p')[attempts].text
except: self.summary = super().getSummary()
attempts += 1
if(len(self.summary) > 100): return self.summary
self.summary = f"summary for {self.url}"
except:
self.summary = f"summary for {self.url}"
return self.summary
def getJSON(self):
return {'title': self.getTitle(),
'url': self.getURL(),
'summary': self.getSummary()}
def getJSONString(self):
info = self.getJSON()
return json.dumps(info)
def render(self):
session = HTMLSession()
response = session.get(self.url)
return response.html
class PDF(Website):
def __init__(self, url):
super().__init__(url)
if(self.saved == False):
self.save()
def exportToDB(self):
self.pdfReader = PyPDF2.PdfFileReader(io.BytesIO(self.html._html))
self.getParagraphs()
return {'title': self.getTitle(),
'url': self.getURL(),
'summary': self.getSummary(),
'paragraphs': self.getParagraphs(),
'links': self.getLinks() }
def save(self):
self.saved = True
if(infoCollection != None): infoCollection.insert_one(self.exportToDB())
def getTitle(self):
if(self.title == None):
try: self.title = self.pdfReader.getDocumentInfo().title
except: self.title = super().getTitle()
return self.title
def getURL(self):
return self.url
def getLinks(self):
return []
def getParagraphs(self):
if(self.paragraphs == None):
try: self.paragraphs = [self.pdfReader.getPage(i).extractText() for i in range(self.pdfReader.numPages)]
except: self.paragraphs = ['error occured']
return self.paragraphs
def getSummary(self):
if(self.summary == None):
try: self.summary = self.paragraphs[0]
except: self.summary = 'error occured'
return self.summary
def getJSON(self):
return {'title': self.getTitle(),
'url': self.getURL(),
'summary': self.getSummary()}
def getJSONString(self):
info = self.getJSON()
return json.dumps(info)
class Wiki(Website):
def __init__(self, url):
url = url.split('#')[0]
super().__init__(url)
# if unsaved yet
if(self.saved == False):
# save() generates json for the first time
self.save()
def save(self):
self.saved = True
if(infoCollection != None): infoCollection.insert_one(self.exportToDB())
def exportToDB(self):
return {'title': self.getTitle(),
'url': self.getURL(),
'summary': self.getSummary(),
'paragraphs': self.getParagraphs(),
'links': self.getLinks() }
def getTitle(self):
if(self.title == None):
try: self.title = self.html.find('#firstHeading')[0].text
except: self.title = super().getTitle()
return self.title
def getURL(self):
return self.url
def getSummary(self): #
if(self.summary == None):
attempts = 1
while(attempts < 10):
try: self.summary = self.html.find('p')[attempts].text
except: self.summary = super().getSummary()
attempts += 1
if(len(self.summary) > 100): break
return self.summary
def getParagraphs(self):
if(self.paragraphs == None):
self.paragraphs = [i.text for i in self.html.find('p')]
return self.paragraphs
def getLinks(self):
if(self.links == None):
self.links = list(self.html.absolute_links)
return self.links
def getJSON(self):
return {'title': self.getTitle(),
'url': self.getURL(),
'summary': self.getSummary()}
def getJSONString(self):
info = self.getJSON()
return json.dumps(info)
class ArXiv(Website):
def __init__(self, url):
url = url.replace('.pdf', '').replace('pdf','abs') # in case url comes in the form of pdf
self.url = url
self.authors = None
super().__init__(url)
if(self.saved == False):
self.save()
def getTitle(self):
if(self.title == None):
self.title = self.html.find('title')[0].text.split('\n')[0]
return self.title
def getAuthors(self):
if(self.authors == None):
self.authors = self.html.find('.authors')[0].text.split('\n')[0].replace('Authors:','').split(',')
return self.authors
def getAbstract(self):
return self.html.find('.abstract')[0].text.split('\n')[0]
def getSummary(self): # summary is just abstract
if(self.summary == None):
self.summary = self.getAbstract()
return self.summary
def getURL(self):
return self.url
def save(self):
return
def getJSONString(self):
return {'title': self.getTitle(),
'url': self.getURL(),
'summary': self.getSummary()}
class Medium(Website):
def __init__(self, url):
url = url.split('#')[0]
super().__init__(url)
# if unsaved yet
if(self.saved == False):
# save() generates json for the first time
self.save()
def save(self):
self.saved = True
if(infoCollection != None): infoCollection.insert_one(self.exportToDB())
def exportToDB(self):
return {'title': self.getTitle(),
'url': self.getURL(),
'summary': self.getSummary(),
'paragraphs': self.getParagraphs(),
'links': self.getLinks() }
def getTitle(self):
if(self.title == None):
try: self.title = self.html.find('.pw-post-title')[0].text
except: self.title = super().getTitle()
return self.title
def getURL(self):
return self.url
def getSummary(self): #
if(self.summary == None):
attempts = 0
while(attempts < 10):
try: self.summary = self.html.find('.pw-post-body-paragraph')[attempts].text
except: self.summary = super().getSummary()
attempts += 1
if(len(self.summary) > 100): break
return self.summary
def getParagraphs(self):
if(self.paragraphs == None):
self.paragraphs = [i.text for i in self.html.find('p')]
return self.paragraphs
def getLinks(self):
if(self.links == None):
self.links = list(self.html.absolute_links)
return self.links
def getJSON(self):
return {'title': self.getTitle(),
'url': self.getURL(),
'summary': self.getSummary()}
def getJSONString(self):
info = self.getJSON()
return json.dumps(info)
class Edge:
def __init__(self, A, B):
self.A = A
self.B = B
class Node:
def __init__(self, url):
self.url = url
self.obj = None
self.edgesOut = []
self.edgesIn = []
if('en.wikipedia.org' in self.url): self.obj = Wiki(self.url)
def addEdge(self, B):
self.edgesOut.append(Edge(self,B))
self.edgesIn.append(Edge(B,self))
class WikiCrawler:
def __init__(self):
self.nodes = []
self.queue = []
def stepBFS(self, node = None):
if(node == None): node = self.queue[0]
if(node in self.queue): self.queue.remove(node)
if(node.obj == None): return
print(f'bfs @ {node.url}')
for nextLink in node.obj.links:
next = Node(nextLink)
node.addEdge(next)
self.nodes.append(next)
self.queue.append(next)
if(__name__ == "__main__"):
pass
# wc = WikiCrawler()
# wc.stepBFS(Node('https://en.wikipedia.org/wiki/Quantum_field_theory'))
# for i in range(10):
# wc.stepBFS()