From 163d7de8f5bbc005afc95d2d3847db11fc12c2e8 Mon Sep 17 00:00:00 2001 From: rdavydov <15850461+rdavydov@users.noreply.github.com> Date: Fri, 16 Dec 2022 19:52:03 +0300 Subject: [PATCH] 1.5.4 temporary login fix --- TwitchChannelPointsMiner/__init__.py | 2 +- .../classes/TwitchLogin.py | 60 ++++++++++++++----- requirements.txt | 1 + setup.py | 3 +- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/TwitchChannelPointsMiner/__init__.py b/TwitchChannelPointsMiner/__init__.py index 3a76474d..1c7a048c 100644 --- a/TwitchChannelPointsMiner/__init__.py +++ b/TwitchChannelPointsMiner/__init__.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -__version__ = "1.5.3" +__version__ = "1.5.4" from .TwitchChannelPointsMiner import TwitchChannelPointsMiner __all__ = [ diff --git a/TwitchChannelPointsMiner/classes/TwitchLogin.py b/TwitchChannelPointsMiner/classes/TwitchLogin.py index d439e036..f11fe808 100644 --- a/TwitchChannelPointsMiner/classes/TwitchLogin.py +++ b/TwitchChannelPointsMiner/classes/TwitchLogin.py @@ -8,6 +8,8 @@ import os import pickle +import browser_cookie3 + import requests from TwitchChannelPointsMiner.classes.Exceptions import ( @@ -43,7 +45,8 @@ class TwitchLogin(object): "password", "user_id", "email", - "cookies" + "cookies", + "shared_cookies" ] def __init__(self, client_id, device_id, username, user_agent, password=None): @@ -61,6 +64,7 @@ def __init__(self, client_id, device_id, username, user_agent, password=None): self.email = None self.cookies = [] + self.shared_cookies = [] def login_flow(self): logger.info("You'll have to login to Twitch!") @@ -71,8 +75,8 @@ def login_flow(self): "remember_me": True, } # login-fix - use_backup_flow = False - #use_backup_flow = True + #use_backup_flow = False + use_backup_flow = True for attempt in range(0, 25): password = ( @@ -133,8 +137,8 @@ def login_flow(self): # If the user didn't load the password from run.py we can just ask for it again. break # login-fix - elif err_code == 1000: - #elif err_code in [1000, 5022]: + #elif err_code == 1000: + elif err_code in [1000, 5022, 5023, 5024]: logger.info( "Console login unavailable (CAPTCHA solving required)." ) @@ -235,8 +239,33 @@ def login_flow_backup(self, password = None): return False return self.get_cookie_value("auth-token")""" - logger.error("Backup login flow is not available. Use a VPN or wait a while to avoid the CAPTCHA.") - return False + + #logger.error("Backup login flow is not available. Use a VPN or wait a while to avoid the CAPTCHA.") + #return False + + """Backup OAuth login flow in case manual captcha solving is required""" + browser = input( + "What browser do you use? Chrome (1), Firefox (2), Other (3): " + ).strip() + if browser not in ("1", "2"): + logger.info("Your browser is unsupported, sorry.") + return None + + input( + "Please login inside your browser of choice (NOT incognito mode) and press Enter..." + ) + logger.info("Loading cookies saved on your computer...") + twitch_domain = ".twitch.tv" + if browser == "1": # chrome + cookie_jar = browser_cookie3.chrome(domain_name=twitch_domain) + else: + cookie_jar = browser_cookie3.firefox(domain_name=twitch_domain) + #logger.info(f"cookie_jar: {cookie_jar}") + cookies_dict = requests.utils.dict_from_cookiejar(cookie_jar) + #logger.info(f"cookies_dict: {cookies_dict}") + self.username = cookies_dict.get("login") + self.shared_cookies = cookies_dict + return cookies_dict.get("auth-token") def check_login(self): if self.login_check_result: @@ -248,17 +277,20 @@ def check_login(self): return self.login_check_result def save_cookies(self, cookies_file): - #pickle.dump(self.cookies, open(cookies_file, "wb")) - # ^ only this line was needed with Selenium ^ - cookies_dict = self.session.cookies.get_dict() - cookies_dict["auth-token"] = self.token - if "persistent" not in cookies_dict: # saving user id cookies - cookies_dict["persistent"] = self.user_id + #cookies_dict = self.session.cookies.get_dict() + #print(f"cookies_dict2pickle: {cookies_dict}") + #cookies_dict["auth-token"] = self.token + #if "persistent" not in cookies_dict: # saving user id cookies + # cookies_dict["persistent"] = self.user_id + # old way saves only 'auth-token' and 'persistent' self.cookies = [] + cookies_dict = self.shared_cookies + #print(f"cookies_dict2pickle: {cookies_dict}") for cookie_name, value in cookies_dict.items(): self.cookies.append({"name": cookie_name, "value": value}) - pickle.dump(self.cookies, open(cookies_file, "wb")) + #print(f"cookies2pickle: {self.cookies}") + pickle.dump(self.cookies, open(cookies_file, "wb")) def get_cookie_value(self, key): for cookie in self.cookies: diff --git a/requirements.txt b/requirements.txt index 5a011c2f..23e6dbb7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,4 @@ colorama flask irc pandas +browser_cookie3 diff --git a/setup.py b/setup.py index 30afcecd..18868643 100644 --- a/setup.py +++ b/setup.py @@ -36,7 +36,8 @@ def read(fname): "colorama", "flask", "irc", - "pandas" + "pandas", + "browser_cookie3" ], long_description=read("README.md"), long_description_content_type="text/markdown",