Created
August 6, 2019 14:16
-
-
Save vryazanov/4446a0a095fba5353236635dd1af4eec to your computer and use it in GitHub Desktop.
Habr proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| import aiohttp | |
| import aiohttp.web | |
| import bs4 | |
| LOCAL_DOMAIN = 'http://localhost:8080/' | |
| HABR_DOMAIN = 'https://habr.com/' | |
| def replace_links(soup): | |
| habr_re = re.compile(f'{HABR_DOMAIN}.*') | |
| for attr_name in ('href', 'xlink:href'): | |
| for tag in soup.find_all(attrs={attr_name: habr_re}): | |
| tag[attr_name] = tag[attr_name].replace(HABR_DOMAIN, LOCAL_DOMAIN) | |
| def replace_words(soup): | |
| exclude_tags = ('script', 'style') | |
| for tag in soup.find_all(text=True): | |
| forbidden_parents = [True for x in exclude_tags if tag.find_parent(x)] | |
| if tag.string != 'html' and not forbidden_parents: | |
| tag.string.replace_with( | |
| re.sub( | |
| r'\b([а-яa-z]{6})\b', r'\1™', | |
| tag.string, | |
| flags=re.IGNORECASE | |
| ) | |
| ) | |
| async def proxy(request): | |
| path = request.match_info.get('path') | |
| async with aiohttp.ClientSession() as session: | |
| async with session.get(f'{HABR_DOMAIN}{path}') as resp: | |
| content, content_type = await resp.read(), resp.content_type | |
| if content_type != 'text/html': | |
| body = content | |
| else: | |
| soup = bs4.BeautifulSoup(content, 'html.parser') | |
| replace_links(soup) | |
| replace_words(soup) | |
| body = soup.prettify() | |
| return aiohttp.web.Response(body=body, content_type=content_type) | |
| app = aiohttp.web.Application() | |
| app.add_routes([ | |
| aiohttp.web.get('/{path:.*}', proxy), | |
| ]) | |
| aiohttp.web.run_app(app) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to start:
aiohttpandbs4python server.pyhttp://localhost:8080in your browser