majuna/app/terraform/block_external.py

35 lines
1.2 KiB
Python
Raw Normal View History

from typing import List, Dict
2022-03-10 14:26:22 +00:00
from bs4 import BeautifulSoup
import requests
from app import app
from app.terraform.block_mirror import BlockMirrorAutomation
class BlockExternalAutomation(BlockMirrorAutomation):
2022-06-17 12:42:42 +01:00
"""
Automation task to import proxy reachability results from external source.
"""
short_name = "block_external"
description = "Import proxy reachability results from external source"
_content: bytes
2022-06-17 12:42:42 +01:00
def _fetch(self) -> None:
user_agent = {'User-agent': 'BypassCensorship/1.0'}
page = requests.get(app.config['EXTERNAL_CHECK_URL'], headers=user_agent)
self._content = page.content
2022-06-17 12:42:42 +01:00
def _parse(self) -> None:
soup = BeautifulSoup(self._content, 'html.parser')
2022-06-17 12:42:42 +01:00
h2 = soup.find_all('h2') # pylint: disable=invalid-name
div = soup.find_all('div', class_="overflow-auto mb-5")
i = 0
for idx, heading in enumerate(h2):
if not div[idx].div and heading.text in app.config['EXTERNAL_VANTAGE_POINTS']:
anchors = div[idx].find_all('a')
for anchor in anchors:
self.patterns.append("https://" + anchor.text)
i += 1