-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
61 lines (43 loc) · 1.46 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
import re
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class Request:
def __init__(self, url):
self._url = url
def get_url(self):
return self._url
class Resource:
def __init__(self, har_data):
self._request = Request(har_data['request']['url'])
self._response = Response(har_data['response']['headers'],
har_data['response']['content'])
def get_fullpath(self):
url = self._request.get_url()
return re.sub('^(https?://)', '', url)
def get_content(self):
return self._response.get_content()
def is_image(self):
"""Verify if the resource is an image.
"""
content_type = self._response.get_content_type()
if content_type is None:
return False
if re.search("image/(gif|png|jpe?g|webp)", content_type):
return True
return False
def __repr__(self):
"""Returns a representation for resource.
"""
return "<Resource is_image={}>".format(self.is_image())
class Response:
def __init__(self, headers, content):
self._headers = headers
self._content = content
def get_content(self) -> str:
return self._content
def get_content_type(self) -> str:
for header in self._headers:
if header['name'].lower() == 'content-type':
return header['value']
return None