-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit 97dd612 Author: 铂屑 <[email protected]> Date: Sun Jan 29 17:55:18 2023 +0800 Update __init__.py commit 60a55c0 Author: 铂屑 <[email protected]> Date: Sun Jan 29 17:50:08 2023 +0800 ✨Add tkinter to show qrcode commit 9719d66 Merge: 5f0ee2a 1190b3d Author: 铂屑 <[email protected]> Date: Sun Jan 29 14:56:16 2023 +0800 Merge branch 'main' into dev commit 5f0ee2a Author: 铂屑 <[email protected]> Date: Sun Jan 29 02:54:39 2023 +0800 Update getGift.py commit 38a18e0 Author: 铂屑 <[email protected]> Date: Sun Jan 29 02:51:02 2023 +0800 Add a little advertising commit 342535f Author: 铂屑 <[email protected]> Date: Sun Jan 29 02:15:19 2023 +0800 💄 Beautify the CLI
- Loading branch information
Showing
8 changed files
with
198 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from . import ( # noqa: F401 | ||
agent, | ||
console, | ||
getGift, | ||
login, | ||
up_to_date, | ||
) |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
import http.cookiejar as cookielib | ||
import json | ||
import os | ||
import tempfile | ||
import tkinter | ||
import tkinter.font | ||
|
||
import httpx | ||
import qrcode | ||
from PIL.ImageTk import PhotoImage | ||
|
||
from . import agent | ||
from .console import console | ||
|
||
API = { | ||
"qrcode": { | ||
"get_qrcode_and_token": { | ||
"url": "http://passport.bilibili.com/qrcode/getLoginUrl", | ||
"method": "GET", | ||
"verify": False, | ||
"comment": "请求二维码及登录密钥", | ||
}, | ||
"get_events": { | ||
"url": "http://passport.bilibili.com/qrcode/getLoginInfo", | ||
"method": "POST", | ||
"verify": False, | ||
"data": {"oauthKey": "str: 登陆密钥"}, | ||
"comment": "获取最新信息", | ||
}, | ||
} | ||
} | ||
|
||
headers = { | ||
"User-Agent": agent.get_user_agents(), | ||
"Referer": "https://www.bilibili.com/", | ||
} | ||
headerss = { | ||
"User-Agent": agent.get_user_agents(), | ||
"Host": "passport.bilibili.com", | ||
"Referer": "https://passport.bilibili.com/login", | ||
} | ||
|
||
# ---------------------------------------------------------------- | ||
# 二维码登录 | ||
# ---------------------------------------------------------------- | ||
|
||
photo = None # 图片的全局变量 | ||
client = httpx.Client(verify=False) | ||
login_key = "" | ||
qrcode_image = None | ||
is_destroy = False | ||
id_ = 0 # 事件 id,用于取消 after 绑定 | ||
|
||
|
||
def islogin(): | ||
"""检查登陆是否有效 | ||
Returns: | ||
client: httpx.Client | ||
""" | ||
global client | ||
try: | ||
client.cookies.jar.load(ignore_discard=True) | ||
except Exception: | ||
console.print("[b green]bzcookies.txt[/b green] 载入失败") | ||
loginurl = client.get( | ||
"https://api.bilibili.com/x/web-interface/nav", headers=headers | ||
).json() | ||
if loginurl["code"] == 0: | ||
console.print( | ||
"Cookies值有效,[b blue]{}[/b blue],已登录!".format(loginurl["data"]["uname"]) | ||
) | ||
return client, True | ||
else: | ||
console.print("Cookies值已经失效,[blue]已弹出二维码[/blue],请重新扫码登录!") | ||
return client, False | ||
|
||
|
||
def make_qrcode(url) -> str: | ||
qr = qrcode.QRCode() | ||
qr.add_data(url) | ||
img = qr.make_image() | ||
img.save(os.path.join(tempfile.gettempdir(), "qrcode.png")) | ||
return os.path.join(tempfile.gettempdir(), "qrcode.png") | ||
|
||
|
||
def login_with_qrcode(): | ||
"""通过二维码登陆 | ||
Raises: | ||
Exception: 登陆失败 | ||
""" | ||
|
||
global photo | ||
global login_key, qrcode_image | ||
global id_ | ||
global client | ||
|
||
root = tkinter.Tk() | ||
root.title("扫码登录") | ||
qrcode_image = update_qrcode() | ||
photo = PhotoImage(file=qrcode_image) | ||
qrcode_label = tkinter.Label(root, image=photo, width=500, height=500) | ||
qrcode_label.pack() | ||
big_font = tkinter.font.Font(root, size=25) | ||
log = tkinter.Label(root, text="二维码未失效,请扫码!", font=big_font, fg="red") | ||
log.pack() | ||
|
||
def update_events(): | ||
global id_ | ||
global is_destroy, client | ||
events_api = API["qrcode"]["get_events"] | ||
events = client.post( | ||
events_api["url"], | ||
data={"oauthKey": login_key, "gourl": "https://www.bilibili.com/"}, | ||
headers=headerss, | ||
).json() | ||
if "code" in events.keys() and events["code"] == -412: | ||
raise Exception(events["message"]) | ||
if events["data"] == -4: | ||
log.configure(text="二维码未失效,请扫码!", fg="red", font=big_font) | ||
elif events["data"] == -5: | ||
log.configure(text="已扫码,请确认!", fg="orange", font=big_font) | ||
elif events["data"] == -2: | ||
update_qrcode() | ||
log.configure(text="二维码已刷新,请重新扫码", fg="blue", font=big_font) | ||
elif isinstance(events["data"], dict): | ||
url = events["data"]["url"] | ||
client.get(url, headers=headers) | ||
client.cookies.jar.save() | ||
|
||
log.configure(text="登入成功!", fg="green", font=big_font) | ||
|
||
root.after(2000, destroy) | ||
id_ = root.after(2000, update_events) | ||
root.update() | ||
|
||
def destroy(): | ||
global id_ | ||
root.after_cancel(id_) # type: ignore | ||
root.destroy() | ||
|
||
root.after(500, update_events) | ||
root.mainloop() | ||
root.after_cancel(id_) # type: ignore | ||
|
||
|
||
def update_qrcode() -> str: | ||
"""更新二维码 | ||
Returns: | ||
str: 二维码 | ||
""" | ||
global login_key, qrcode_image, client | ||
api = API["qrcode"]["get_qrcode_and_token"] | ||
qrcode_login_data = json.loads(client.get(api["url"]).text)["data"] | ||
login_key = qrcode_login_data["oauthKey"] | ||
qrcode = qrcode_login_data["url"] | ||
qrcode_image = make_qrcode(qrcode) | ||
return qrcode_image | ||
|
||
|
||
def main(): | ||
global client | ||
|
||
nowdir = os.getcwd() | ||
result_file = os.path.join(nowdir, "bzcookies.txt") | ||
if not os.path.exists(result_file): | ||
with open(result_file, "w") as f: | ||
f.write("") | ||
client.cookies = cookielib.LWPCookieJar(filename=result_file) | ||
client, status = islogin() | ||
if not status: | ||
login_with_qrcode() | ||
|
||
return client | ||
|
||
|
||
# if __name__ == "__main__": | ||
# main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import httpx | ||
|
||
from console import console | ||
from .console import console | ||
|
||
|
||
def main(now_tag): | ||
|