From 7bd815e0793cc62f4f72de63e8a559857bcb3c4e Mon Sep 17 00:00:00 2001 From: Takagi <2969124815@qq.com> Date: Sun, 22 Dec 2024 11:25:25 +0800 Subject: [PATCH] Update auth.py Use grammar in Python 3 --- auth.py | 155 +++++++++++++++++++++++++++----------------------------- 1 file changed, 74 insertions(+), 81 deletions(-) diff --git a/auth.py b/auth.py index ed51290..dd9031a 100644 --- a/auth.py +++ b/auth.py @@ -3,15 +3,14 @@ # Build-in / Std import os, sys, time, platform, random -import re, json, cookielib +import re, json, http.cookiejar from getpass import getpass # requirements import requests, termcolor - requests = requests.Session() -requests.cookies = cookielib.LWPCookieJar('cookies') +requests.cookies = http.cookiejar.LWPCookieJar('cookies') try: requests.cookies.load(ignore_discard=True) except: @@ -23,94 +22,102 @@ class Logging: @staticmethod def error(msg): if Logging.flag == True: - print "".join( [ termcolor.colored("ERROR", "red"), ": ", termcolor.colored(msg, "white") ] ) + print("".join([termcolor.colored("ERROR", "red"), ": ", termcolor.colored(msg, "white")])) @staticmethod def warn(msg): if Logging.flag == True: - print "".join( [ termcolor.colored("WARN", "yellow"), ": ", termcolor.colored(msg, "white") ] ) + print("".join([termcolor.colored("WARN", "yellow"), ": ", termcolor.colored(msg, "white")])) @staticmethod def info(msg): # attrs=['reverse', 'blink'] if Logging.flag == True: - print "".join( [ termcolor.colored("INFO", "magenta"), ": ", termcolor.colored(msg, "white") ] ) + print("".join([termcolor.colored("INFO", "magenta"), ": ", termcolor.colored(msg, "white")])) @staticmethod def debug(msg): if Logging.flag == True: - print "".join( [ termcolor.colored("DEBUG", "magenta"), ": ", termcolor.colored(msg, "white") ] ) + print("".join([termcolor.colored("DEBUG", "magenta"), ": ", termcolor.colored(msg, "white")])) @staticmethod def success(msg): if Logging.flag == True: - print "".join( [ termcolor.colored("SUCCES", "green"), ": ", termcolor.colored(msg, "white") ] ) + print("".join([termcolor.colored("SUCCES", "green"), ": ", termcolor.colored(msg, "white")])) # Setting Logging Logging.flag = True class LoginPasswordError(Exception): def __init__(self, message): - if type(message) != type("") or message == "": self.message = u"帐号密码错误" - else: self.message = message + if not isinstance(message, str) or message == "": + self.message = "帐号密码错误" + else: + self.message = message Logging.error(self.message) class NetworkError(Exception): def __init__(self, message): - if type(message) != type("") or message == "": self.message = u"网络异常" - else: self.message = message + if not isinstance(message, str) or message == "": + self.message = "网络异常" + else: + self.message = message Logging.error(self.message) + class AccountError(Exception): def __init__(self, message): - if type(message) != type("") or message == "": self.message = u"帐号类型错误" - else: self.message = message + if not isinstance(message, str) or message == "": + self.message = "帐号类型错误" + else: + self.message = message Logging.error(self.message) - - - - def download_captcha(): url = "https://www.zhihu.com/captcha.gif" r = requests.get(url, params={"r": random.random(), "type": "login"}, verify=False) if int(r.status_code) != 200: - raise NetworkError(u"验证码请求失败") - image_name = u"verify." + r.headers['content-type'].split("/")[1] - open( image_name, "wb").write(r.content) - """ - System platform: https://docs.python.org/2/library/platform.html - """ - Logging.info(u"正在调用外部程序渲染验证码 ... ") + raise NetworkError("验证码请求失败") + image_name = "verify." + r.headers['content-type'].split("/")[1] + open(image_name, "wb").write(r.content) + + Logging.info("正在调用外部程序渲染验证码 ... ") if platform.system() == "Linux": - Logging.info(u"Command: xdg-open %s &" % image_name ) - os.system("xdg-open %s &" % image_name ) + Logging.info("Command: xdg-open %s &" % image_name) + os.system("xdg-open %s &" % image_name) elif platform.system() == "Darwin": - Logging.info(u"Command: open %s &" % image_name ) - os.system("open %s &" % image_name ) + Logging.info("Command: open %s &" % image_name) + os.system("open %s &" % image_name) elif platform.system() in ("SunOS", "FreeBSD", "Unix", "OpenBSD", "NetBSD"): - os.system("open %s &" % image_name ) + os.system("open %s &" % image_name) elif platform.system() == "Windows": - os.system("%s" % image_name ) + os.system("%s" % image_name) else: - Logging.info(u"我们无法探测你的作业系统,请自行打开验证码 %s 文件,并输入验证码。" % os.path.join(os.getcwd(), image_name) ) + Logging.info("我们无法探测你的作业系统,请自行打开验证码 %s 文件,并输入验证码。" % os.path.join(os.getcwd(), image_name)) - sys.stdout.write(termcolor.colored(u"请输入验证码: ", "cyan") ) - captcha_code = raw_input( ) + sys.stdout.write(termcolor.colored("请输入验证码: ", "cyan")) + captcha_code = input() return captcha_code def search_xsrf(): - url = "http://www.zhihu.com/" - r = requests.get(url, verify=False) - if int(r.status_code) != 200: - raise NetworkError(u"验证码请求失败") - results = re.compile(r"\