diff --git a/setup.py b/setup.py index c850945..f19a878 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name="sport80", - version="2.2.4", + version="2.2.5", description="Python API interface for the Sport80 sites", long_description='Intentionally empty', url="https://github.com/euanwm/sport80_api", @@ -11,8 +11,7 @@ author_email="euanmeston@gmail.com", license="BSD", install_requires=["requests", - "beautifulsoup4", - "js2py"], + "beautifulsoup4"], classifiers=["Programming Language :: Python :: 3.11"], python_requires='>=3.8' ) diff --git a/sport80/__init__.py b/sport80/__init__.py index 84d8d6a..d8debb8 100644 --- a/sport80/__init__.py +++ b/sport80/__init__.py @@ -4,8 +4,8 @@ from .sport80 import SportEighty from .sport80_http_client import SportEightyHTTP -from .helpers import pull_tables, convert_to_json, convert_to_py +from .helpers import pull_tables from .pages_enum import EndPoint -__version__ = "2.2.4" +__version__ = "2.2.5" __author__ = "Euan Meston" diff --git a/sport80/helpers.py b/sport80/helpers.py index 9e83276..ef87506 100644 --- a/sport80/helpers.py +++ b/sport80/helpers.py @@ -7,7 +7,7 @@ from logging import info, debug from bs4 import BeautifulSoup from requests import Response -from js2py import eval_js +# from js2py import eval_js from .pages_enum import LegacyEndPoint @@ -49,12 +49,6 @@ def collate_index(page_data: dict) -> dict: return switch_to_dict -def convert_to_py(js_vars: str) -> dict: - """ I really don't care at this stage """ - py_dict = eval_js(js_vars.lstrip('[]')) - return py_dict - - def resolve_to_ip(url: str) -> str: """ Returns IP address of the subdomain """ return socket.gethostbyname(url) diff --git a/sport80/sport80_http_client.py b/sport80/sport80_http_client.py index 00fa338..d29d71b 100644 --- a/sport80/sport80_http_client.py +++ b/sport80/sport80_http_client.py @@ -1,13 +1,13 @@ """ Busy backend shit """ import logging -import requests -from typing import Union, Optional - +import json +import re from urllib.parse import urljoin -from bs4 import BeautifulSoup +from typing import Union, Optional +import requests from .pages_enum import EndPoint, LegacyEndPoint -from .helpers import pull_tables, convert_to_json, convert_to_py, collate_index, event_dict_to_list +from .helpers import pull_tables, convert_to_json, collate_index, event_dict_to_list class SportEightyHTTP: @@ -37,14 +37,16 @@ def app_data(self): def pull_domain_env(self) -> dict: """ On both BWL and USAW sites, there is a JS dict needed for the API calls to work """ get_page = requests.get(urljoin(self.domain, EndPoint.INDEX_PAGE.value)) - soup = BeautifulSoup(get_page.content, "html.parser") - scripts_in_page = soup.find_all('script') - js_extract = [] - for js_section in scripts_in_page: - if "application/javascript" in js_section.attrs.values(): - js_extract.append(js_section) - if len(js_extract) == 1: - return convert_to_py(str(js_extract)) + page_data = get_page.text + reggie = re.compile(r"window.env = ({.*?});", re.DOTALL) + match = reggie.search(page_data) + if match: + try: + py_dict = json.loads(match.group(1)) + return py_dict + except json.JSONDecodeError: + return {} + return {} def test_token(self, token: str): api_url = urljoin(self.domain_env['RANKINGS_DOMAIN_URL'], EndPoint.RANKINGS_INDEX.value)