forked from evbogdanov/aio-habr-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
85 lines (65 loc) · 2.46 KB
/
proxy.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
from aiohttp import web, ClientSession
import async_timeout
import bs4
import re
################################################################################
### Constants
################################################################################
HOST = '127.0.0.1'
PORT = 8080
WEBSITE = 'https://habrahabr.ru'
TAGS_TO_IGNORE = ['pre', 'code', 'script', 'style', 'head', 'title', 'meta']
RE_WORD_WITH_SIX_CHARS = re.compile(r'(\b\w{6}\b)')
################################################################################
### HTML modifications
################################################################################
def should_modify_tag(tag):
if tag.parent.name in TAGS_TO_IGNORE:
return False
if isinstance(tag, bs4.Comment) or isinstance(tag, bs4.Doctype):
return False
return True
def modify_tag(tag):
text = tag.string.strip()
if not text:
return
text = RE_WORD_WITH_SIX_CHARS.sub(r'\1™', text)
tag.string.replace_with(text)
def modify_html(html):
soup = bs4.BeautifulSoup(html, 'lxml')
for link in soup.find_all('a'):
href = link.get('href')
if href:
link['href'] = href.replace(WEBSITE, '')
for tag in soup.find_all(text=True):
if should_modify_tag(tag):
modify_tag(tag)
return soup.prettify()
################################################################################
### Client/server stuff
################################################################################
async def fetch(url):
async with ClientSession() as session:
with async_timeout.timeout(10):
async with session.get(url) as response:
if 'text/html' in response.headers['Content-Type']:
return True, await response.text()
return False, await response.read()
async def handle(request):
url = request.rel_url
is_html, content = await fetch(f'{WEBSITE}{url}')
if is_html:
response = {'text': modify_html(content),
'content_type': 'text/html'}
else:
response = {'body': content}
return web.Response(**response)
################################################################################
### Main
################################################################################
def main():
app = web.Application()
app.router.add_get('/{tail:.*}', handle)
web.run_app(app, host=HOST, port=PORT)
if __name__ == '__main__':
main()