|
1 | 1 | """Python script to fetch a customisation report from the Guild website.""" |
2 | 2 |
|
3 | 3 | from typing import Final, Mapping, TYPE_CHECKING |
| 4 | + |
4 | 5 | import aiohttp |
5 | 6 | import bs4 |
6 | | -from bs4 import BeautifulSoup |
| 7 | +import certifi |
| 8 | +import ssl |
7 | 9 | import re |
| 10 | + |
| 11 | +from bs4 import BeautifulSoup |
8 | 12 | from datetime import datetime |
9 | 13 |
|
10 | 14 | if TYPE_CHECKING: |
|
22 | 26 | SALES_TO_TIME_KEY: Final[str] = "ctl00$ctl00$Main$AdminPageContent$drDateRange$txtToTime" |
23 | 27 |
|
24 | 28 |
|
25 | | -async def get_msl_context(url: str, auth_cookie: str) -> tuple[dict[str, str], dict[str, str]]: |
| 29 | +ssl_context: ssl.SSLContext = ssl.create_default_context(cafile=certifi.where()) |
| 30 | + |
| 31 | + |
| 32 | +async def get_msl_context( |
| 33 | + url: str, auth_cookie: str |
| 34 | +) -> tuple[dict[str, str], dict[str, str]]: |
26 | 35 | """Get the required context headers, data and cookies to make a request to MSL.""" |
27 | 36 |
|
28 | 37 | BASE_COOKIES: Mapping[str, str] = { |
29 | 38 | ".ASPXAUTH": auth_cookie, |
30 | 39 | } |
31 | 40 |
|
32 | | - http_session: aiohttp.ClientSession = aiohttp.ClientSession( |
33 | | - headers=BASE_HEADERS, |
34 | | - cookies=BASE_COOKIES, |
35 | | - ) |
36 | 41 | data_fields: dict[str, str] = {} |
37 | 42 | cookies: dict[str, str] = {} |
38 | | - async with http_session, http_session.get(url=url) as field_data: |
| 43 | + async with ( |
| 44 | + aiohttp.ClientSession( |
| 45 | + headers=BASE_HEADERS, cookies=BASE_COOKIES |
| 46 | + ) as http_session, |
| 47 | + http_session.get(url=url, ssl=ssl_context) as field_data, |
| 48 | + ): |
39 | 49 | data_response = BeautifulSoup( |
40 | 50 | markup=await field_data.text(), |
41 | 51 | features="html.parser", |
@@ -87,11 +97,12 @@ async def fetch_report_url_and_cookies( |
87 | 97 |
|
88 | 98 | data_fields.update(form_data) |
89 | 99 |
|
90 | | - session_v2: aiohttp.ClientSession = aiohttp.ClientSession( |
91 | | - headers=BASE_HEADERS, |
92 | | - cookies=cookies, |
93 | | - ) |
94 | | - async with (session_v2, session_v2.post(url=SALES_REPORTS_URL, data=data_fields) as http_response): # noqa: E501 |
| 100 | + async with ( |
| 101 | + aiohttp.ClientSession(headers=BASE_HEADERS, cookies=cookies) as session_v2, |
| 102 | + session_v2.post( |
| 103 | + url=SALES_REPORTS_URL, data=data_fields, ssl=ssl_context |
| 104 | + ) as http_response, |
| 105 | + ): # noqa: E501 |
95 | 106 | if http_response.status != 200: |
96 | 107 | print("Returned a non 200 status code!!") |
97 | 108 | print(http_response) |
@@ -148,11 +159,10 @@ async def get_product_customisations( |
148 | 159 | print("Failed to retrieve customisations report URL.") |
149 | 160 | raise ValueError("Failed to retrieve customisations report URL.") |
150 | 161 |
|
151 | | - file_session: aiohttp.ClientSession = aiohttp.ClientSession( |
152 | | - headers=BASE_HEADERS, |
153 | | - cookies=cookies, |
154 | | - ) |
155 | | - async with file_session, file_session.get(url=report_url) as file_response: |
| 162 | + async with ( |
| 163 | + aiohttp.ClientSession(headers=BASE_HEADERS, cookies=cookies) as file_session, |
| 164 | + file_session.get(url=report_url, ssl=ssl_context) as file_response, |
| 165 | + ): |
156 | 166 | if file_response.status != 200: |
157 | 167 | print("Customisation report file session returned a non 200 status code.") |
158 | 168 | print(file_response) |
|
0 commit comments