Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ter-s committed Sep 4, 2022
1 parent 6c73344 commit 007caed
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

from study import study

from utility import md5

def getAccounts():
result = []
Expand Down
2 changes: 2 additions & 0 deletions study.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
33 changes: 29 additions & 4 deletions utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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()

0 comments on commit 007caed

Please sign in to comment.