-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
295 lines (239 loc) · 9.32 KB
/
models.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
from collections import deque, OrderedDict
from copy import copy
import re
from urlparse import urlsplit, urlunsplit, urljoin
import requests
import logging
import errors
# regex
HtmlTitleTagRegex = re.compile(r'<title>(?P<title>.+)</title>', \
re.IGNORECASE | re.DOTALL)
HtmlHrefAttrRegex = re.compile( \
r'<(?P<tag>[a-z]+)(?: rel=[\'"](?P<rel>[^\'"]+)[\'"])?[^<]* href=[\'"](?P<href>[^\'"]+)[\'"][^>]*>(?:<span [^>]*>(?:</span>)?)*(?P<text>[^<]*)', re.IGNORECASE)
HtmlSrcAttrRegex = re.compile( \
r'<(?P<tag>[a-z]+)[^<]* src=[\'"](?P<src>[^\'"]+)[\'"][^>]*>', re.IGNORECASE)
HtmlActionAttrRegex = re.compile( \
r'<(?P<tag>[a-z]+)[^<]* (?:form)?action=[\'"](?P<action>[^\'"]+)[\'"][^>]*>', re.IGNORECASE)
class Page(object):
'''
Page of a website.
'''
def __init__(self, domain, url, content):
'''
@param domain: (str) base domain.
@param url: (str) page's url without the domain.
@param content: (str) HTML content of the page.
'''
super(Page, self).__init__()
domain = domain.strip().lower() # just to be safe
self.__url = url
# find the page's title
match = HtmlTitleTagRegex.search(content)
self.__title = match.group('title').strip() if match is not None \
else 'No title'
# find assets
self.__assets = []
for match in HtmlSrcAttrRegex.finditer(content):
self.__assets.append(match.group('src').strip())
# find form actions
self.__actions = []
for match in HtmlActionAttrRegex.finditer(content):
self.__actions.append(match.group('action').strip())
# find links
self.__internal_links = []
self.__external_links = []
self.__other_links = []
domain_netloc = urlsplit(domain)[1]
for match in HtmlHrefAttrRegex.finditer(content):
href = match.group('href')
text = match.group('text')
# filter out in-page references
if len(href) == 0 or href[0] == '#':
continue
scheme, netloc, path, _, _ = urlsplit(href)
# check for external URLs
if len(netloc) > 0 and netloc != domain_netloc:
href = urlunsplit((scheme, netloc, path, '', ''))
if match.group('tag').lower() == 'a':
self.__external_links.append((href, text))
elif match.group('tag').lower() == 'link' and match.group('rel').lower() == 'stylesheet':
self.__assets.append(href)
else:
self.__other_links.append((href, match.group('rel')))
continue
# filter out useless URLs
if scheme in ('mailto', 'tel', 'javascript') or len(path) == 0:
continue
if path[0] != '/':
logging.warn("Unsupported relative path (%s) at %s", path, url)
# create absolute path ignoring query & fragment parameters
href = urlunsplit(('', '', '/%s' % path.lstrip('/'), '', ''))
# ignore links to self
if href == self.__url:
continue
if match.group('tag').lower() == 'a':
self.__internal_links.append((href, text))
elif match.group('tag').lower() == 'link' and match.group('rel').lower() == 'stylesheet':
self.__assets.append(href)
else:
self.__other_links.append((href, match.group('rel')))
# remove duplicates & make read-only
self.__assets = tuple(OrderedDict.fromkeys(self.__assets).keys())
self.__actions = tuple(OrderedDict.fromkeys(self.__actions).keys())
self.__internal_links = tuple(OrderedDict.fromkeys(self.__internal_links).keys())
self.__external_links = tuple(OrderedDict.fromkeys(self.__external_links).keys())
self.__other_links = tuple(OrderedDict.fromkeys(self.__other_links).keys())
@property
def title(self):
'''
Get the page's title.
@return: (str).
'''
return self.__title
@property
def url(self):
'''
Get the page's URL. Does not include the domain name.
@return: (str).
'''
return self.__url
@property
def assets(self):
'''
Get the page's static assets.
@return: (tuple).
'''
return copy(self.__assets)
@property
def actions(self):
'''
Get the page's form actions.
@return: (tuple).
'''
return copy(self.__actions)
@property
def links(self):
'''
Get the page's links (internal & external).
@return: (tuple).
'''
return copy(self.__internal_links + self.__external_links)
@property
def internal_links(self):
'''
Get the page's internal links.
@return: (tuple).
'''
return copy(self.__internal_links)
@property
def external_links(self):
'''
Get the page's external links.
@return: (tuple).
'''
return copy(self.__external_links)
@property
def other_links(self):
'''
Get the page's other (non-<a>) links.
@return: (tuple).
'''
return copy(self.__other_links)
class Sitemap(object):
'''
Website's sitemap.
'''
def __init__(self, domain):
'''
@param domain: (str).
'''
super(Sitemap, self).__init__()
arr = urlsplit(domain)
if len(arr[0]) == 0:
raise TypeError('Domain name must include "http(s)://".')
self.__domain = '%s://%s' % (arr[0], arr[1])
self.__pages = OrderedDict() # {url: Page}
self.__crawl()
def __str__(self):
'''
To string method.
@return: (str).
'''
s = ['Sitemap for %s:\n' % self.__domain.encode('utf-8')]
for page in self.__pages.itervalues():
s.append('\t-> %s (%s)\n' % (page.title.encode('utf-8'), page.url.encode('utf-8')))
s.append('\t Static assets:\n')
assets = page.assets
if len(assets) > 0:
for asset in assets:
s.append('\t\t%s\n' % asset.encode('utf-8'))
else:
s.append('\t\tNone.\n')
s.append('\t Links:\n')
links = page.links
if len(links) > 0:
for (link, text) in sorted(links):
s.append('\t\t%s - %s\n' % (link.encode('utf-8'), text.encode('utf-8')))
else:
s.append('\t\tNone.\n')
actions = page.actions
if len(actions) > 0:
s.append('\t Form actions:\n')
for action in actions:
s.append('\t\t%s\n' % action.encode('utf-8'))
other_links = page.other_links
if len(other_links) > 0:
s.append('\t Other links:\n')
for (link,rel) in other_links:
if rel is None:
s.append('\t\t%s\n' % link.encode('utf-8'))
else:
s.append('\t\t[rel=%s] %s\n' % (rel.encode('utf-8'), link.encode('utf-8')))
return ''.join(s)
def __crawl(self):
'''
Crawl the given domain and create the sitemap.
'''
urls_to_check = deque(['/'])
while True:
try:
url = urls_to_check.popleft()
except:
break # no more URLs to check
response = requests.get( \
urljoin(self.__domain, url, allow_fragments=False))
try:
response.raise_for_status()
assert response.status_code == 200
if 'video' in response.headers.get('content-type'):
continue
# only crawl HTML pages
assert 'html' in response.headers.get('content-type')
except:
logging.exception("Error getting %s", url)
continue
try:
page = self.__create_page_for(url, response.text)
for (link, text) in page.internal_links:
try:
# ignore URLs already crawled or already in queue
assert link not in self.__pages \
and link not in urls_to_check
urls_to_check.append(link)
except AssertionError:
pass
except errors.PageExistsError:
pass
def __create_page_for(self, url, content):
'''
Create a page for an URL.
@param url: (str) page's URL.
@param content: (str) page's HTML content.
@return: (Page).
@raise PageExistsError: if a page already exists for the same URL.
'''
if url in self.__pages:
raise errors.PageExistsError()
page = Page(self.__domain, url, content)
self.__pages[url] = page
return page