From 9821ccfa92ac19c215ee89ffe6f9d197c69bed6a Mon Sep 17 00:00:00 2001 From: hemant Date: Mon, 22 May 2023 19:36:47 +0530 Subject: [PATCH 1/2] Fix get csrf_token issue --- pyinstalive/api.py | 3 ++- pyinstalive/helpers.py | 5 ++++- requirements.txt | 3 ++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pyinstalive/api.py b/pyinstalive/api.py index b90ee20..1aea731 100644 --- a/pyinstalive/api.py +++ b/pyinstalive/api.py @@ -7,7 +7,8 @@ def get_csrf_token(): response = globals.session.session.get(Constants.LOGIN_PAGE) - return helpers.get_shared_data(response.text).get("csrf_token", None) + helper_res = helpers.get_shared_data(response.text) + return helper_res.get("csrf_token", None) def do_login(): now_epoch = int(datetime.now().timestamp()) diff --git a/pyinstalive/helpers.py b/pyinstalive/helpers.py index d35ac77..ee56518 100644 --- a/pyinstalive/helpers.py +++ b/pyinstalive/helpers.py @@ -89,7 +89,10 @@ def get_shared_data(data): match = re.search(r"\"raw\":\"({[^\n]*\\\"})", data) if match: match_str = string_escape(match.group(1)) - return json.loads(match_str) + csrf_token = re.search(r'"csrf_token":\s*"([^"]+)"', match_str) + csrf_token_value = csrf_token.group(1) + response = {"csrf_token": csrf_token_value} + return response def lock_exists(): return os.path.isfile(os.path.join(globals.config.download_path, globals.download.download_user + '.lock')) diff --git a/requirements.txt b/requirements.txt index 3d214e3..03205fb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ argparse>=1.4.0 -configparser>=4.0.2 \ No newline at end of file +configparser>=4.0.2 +requests==2.30.0 \ No newline at end of file From 3a0cd7b878866fe9eb5ebef28757a7f6f0407d48 Mon Sep 17 00:00:00 2001 From: hemant_manglani Date: Fri, 5 Jan 2024 16:59:35 +0530 Subject: [PATCH 2/2] Again fix login issue --- pyinstalive/helpers.py | 25 ++++++++++++++++++------- requirements.txt | 3 ++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/pyinstalive/helpers.py b/pyinstalive/helpers.py index ee56518..0d32bd5 100644 --- a/pyinstalive/helpers.py +++ b/pyinstalive/helpers.py @@ -6,6 +6,7 @@ import shlex import shutil import requests +from bs4 import BeautifulSoup from urllib.parse import urlparse @@ -86,13 +87,23 @@ def get_shared_data(data): match_str = match.group(1) return json.loads(match_str).get("config") else: - match = re.search(r"\"raw\":\"({[^\n]*\\\"})", data) - if match: - match_str = string_escape(match.group(1)) - csrf_token = re.search(r'"csrf_token":\s*"([^"]+)"', match_str) - csrf_token_value = csrf_token.group(1) - response = {"csrf_token": csrf_token_value} - return response + response = {} + soup = BeautifulSoup(data, 'html.parser') + all_scripts = soup.find_all('script', {"data-content-len":True, "src": False}) + chnk_len = 100 + for script in all_scripts: + if int(script["data-content-len"]) > 30000: + lines = script.text + for idx in range(0, len(lines), chnk_len): + csrf_token_match = re.findall(r"csrf_token", lines[idx : idx + chnk_len]) + if csrf_token_match: + matches = re.findall(r'\{([^}]+)\}', lines[idx : idx + chnk_len]) + if len(matches)>0: + dict_value = "{" + matches[0] + "}" + response = json.loads(dict_value) + break + return response + def lock_exists(): return os.path.isfile(os.path.join(globals.config.download_path, globals.download.download_user + '.lock')) diff --git a/requirements.txt b/requirements.txt index 03205fb..5564c55 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ argparse>=1.4.0 configparser>=4.0.2 -requests==2.30.0 \ No newline at end of file +requests==2.30.0 +beautifulsoup4==4.12.2 \ No newline at end of file