diff --git a/main.py b/main.py index 81f53cc..2a7ac16 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ import os from study import study - +from utility import md5 def getAccounts(): result = [] diff --git a/study.py b/study.py index 2f0fcff..6af24bb 100644 --- a/study.py +++ b/study.py @@ -20,6 +20,8 @@ def study(username, password, ua): touch = bjySession.get(url="https://m.bjyouth.net/site/login") capUrl = "https://m.bjyouth.net" + re.findall( r'src="(/site/captcha.+)" alt=', touch.text)[0] + if "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD5uIDebA2qU746e/NVPiQSBA0Q" not in touch.text: + print("记录的公钥没有出现") capText = cap_recognize(bjySession.get(url=capUrl).content) # print(f'验证码识别: {capText}') login_r = bjySession.post('https://m.bjyouth.net/site/login', diff --git a/utility.py b/utility.py index 060b1a0..5e7480e 100644 --- a/utility.py +++ b/utility.py @@ -14,6 +14,11 @@ def encrypt(t): cipher_text = b64encode(cipher.encrypt(t.encode())) return cipher_text.decode() +def md5(s): + import hashlib + m = hashlib.md5() + m.update(s.encode()) + return m.hexdigest() def cap_recognize(cap): return DdddOcr().classification(denoise(cap)) @@ -22,25 +27,45 @@ def cap_recognize(cap): def denoise(cap): img = Image.open(BytesIO(cap)) steps = ((1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)) - _PX_WHITE = (250, 250, 250) + _PX_BACKGROUND = (250, 250, 250) + _PX_TARGET = (250, 0, 0) threshold, repeat = 7, 2 for _ in range(repeat): for j in range(img.width): for i in range(img.height): px = img.getpixel((j, i)) - if px == _PX_WHITE: + if px == _PX_BACKGROUND: continue count = 0 if px[0] < px[1] + px[2]: count = 2 for x, y in steps: if 0 <= j + x < img.width and 0 <= i + y < img.height: - if img.getpixel((j + x, i + y)) == _PX_WHITE: + if img.getpixel((j + x, i + y)) == _PX_BACKGROUND: count += 1 if count >= threshold: - img.putpixel((j, i), _PX_WHITE) + img.putpixel((j, i), _PX_BACKGROUND) + else: + img.putpixel((j, i), _PX_TARGET) buf = BytesIO() img.save(buf, format='PNG') return buf.getvalue() + +if __name__ == '__main__': + import requests + import re + bjySession = requests.session() + bjySession.timeout = 5 # set session timeout + touch = bjySession.get(url="https://m.bjyouth.net/site/login") + capUrl = "https://m.bjyouth.net" + re.findall( + r'src="(/site/captcha.+)" alt=', touch.text)[0] + cap1 = bjySession.get(url=capUrl).content + cap2 = denoise(cap1) + + img = Image.open(BytesIO(cap1)) + img.show() + + img = Image.open(BytesIO(cap2)) + img.show() \ No newline at end of file