| |
| |
| |
| |
| ''' |
| Download script for Germany TST |
| To run: |
| `python Germany_TST/download_script/download.py` |
| ''' |
|
|
| import requests |
| from bs4 import BeautifulSoup |
| from tqdm import tqdm |
| from pathlib import Path |
| from requests.adapters import HTTPAdapter |
| from urllib3.util.retry import Retry |
|
|
|
|
| def build_session( |
| max_retries: int = 3, |
| backoff_factor: int = 2, |
| session: requests.Session = None |
| ) -> requests.Session: |
| """ |
| Build a requests session with retries |
| |
| Args: |
| max_retries (int, optional): Number of retries. Defaults to 3. |
| backoff_factor (int, optional): Backoff factor. Defaults to 2. |
| session (requests.Session, optional): Session object. Defaults to None. |
| """ |
| session = session or requests.Session() |
| adapter = HTTPAdapter(max_retries=Retry(total=max_retries, backoff_factor=backoff_factor)) |
| session.mount("http://", adapter) |
| session.mount("https://", adapter) |
| session.headers.update({ |
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" |
| }) |
|
|
| return session |
|
|
|
|
| def main(): |
| """ |
| Download Germany TST (https://www.mathe-wettbewerbe.de/aufgaben#t-internationale-mathematik-olympiade) |
| """ |
| output_dir = Path(__file__).parent.parent / "raw" |
| output_dir.mkdir(parents=True, exist_ok=True) |
|
|
| |
| req_session = build_session() |
|
|
| |
| resp = req_session.get('https://www.mathe-wettbewerbe.de/aufgaben#t-internationale-mathematik-olympiade') |
| soup = BeautifulSoup(resp.text, 'html.parser') |
| germ_tst_list_ele = soup.find_all('article')[1] |
|
|
| for sec_ele in tqdm(germ_tst_list_ele.find_all('section')): |
| year = sec_ele.find('button').get_text(strip=True) |
| resources = sec_ele.find_all('a') |
|
|
| with_problems = [_['href'] for _ in resources if "Auswahlklausuren" in _.get_text(strip=True)] |
| with_solutions = [_['href'] for _ in resources if "Lösungen" in _.get_text(strip=True)] |
|
|
| |
| if len(with_solutions) != 0: |
| download_pdfs = with_solutions |
| else: |
| download_pdfs = with_problems |
|
|
| for uri in download_pdfs: |
| output_file = output_dir / f"de-{year}-{Path(uri).name}" |
|
|
| |
| if output_file.exists(): |
| continue |
|
|
| pdf_resp = req_session.get("https://www.mathe-wettbewerbe.de" + uri) |
|
|
| if pdf_resp.status_code != 200: |
| print(f"Year {year} Failed to download: {uri}") |
| continue |
|
|
| output_file.write_bytes(pdf_resp.content) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|