Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 97dd612
Author: 铂屑 <80164656+boxie123@users.noreply.github.com>
Date:   Sun Jan 29 17:55:18 2023 +0800

    Update __init__.py

commit 60a55c0
Author: 铂屑 <80164656+boxie123@users.noreply.github.com>
Date:   Sun Jan 29 17:50:08 2023 +0800

    ✨Add tkinter to show qrcode

commit 9719d66
Merge: 5f0ee2a 1190b3d
Author: 铂屑 <80164656+boxie123@users.noreply.github.com>
Date:   Sun Jan 29 14:56:16 2023 +0800

    Merge branch 'main' into dev

commit 5f0ee2a
Author: 铂屑 <80164656+boxie123@users.noreply.github.com>
Date:   Sun Jan 29 02:54:39 2023 +0800

    Update getGift.py

commit 38a18e0
Author: 铂屑 <80164656+boxie123@users.noreply.github.com>
Date:   Sun Jan 29 02:51:02 2023 +0800

    Add a little advertising

commit 342535f
Author: 铂屑 <80164656+boxie123@users.noreply.github.com>
Date:   Sun Jan 29 02:15:19 2023 +0800

    💄 Beautify the CLI
  • Loading branch information
boxie123 committed Jan 29, 2023
1 parent 1190b3d commit 231b9b3
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 120 deletions.
111 changes: 0 additions & 111 deletions login.py

This file was deleted.

14 changes: 8 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
from rich.prompt import Prompt
from rich.tree import Tree

import getGift
import login
import up_to_date
from console import console
from src import (
getGift,
login,
up_to_date,
)
from src.console import console

if __name__ == "__main__":
# 获取用户登录状态
Expand All @@ -33,7 +35,7 @@
subtitle="UID 1485569",
)
)
client = login.bzlogin()
client = login.main()

# 询问用户使用什么功能
console.print("\n本程序目前拥有的功能:\n")
Expand Down Expand Up @@ -67,7 +69,7 @@

# 检测更新
try:
up_to_date.main("v0.8.1")
up_to_date.main("v0.8.2")
except Exception:
console.print("检测失败")

Expand Down
7 changes: 7 additions & 0 deletions src/__init__.py
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.
4 changes: 2 additions & 2 deletions getGift.py → src/getGift.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import xlsxwriter
from rich.progress import track

import agent
from console import console
from . import agent
from .console import console


# 处理全部礼物数据生成字典以及{uid:name}字典
Expand Down
180 changes: 180 additions & 0 deletions src/login.py
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()
2 changes: 1 addition & 1 deletion up_to_date.py → src/up_to_date.py
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):
Expand Down

0 comments on commit 231b9b3

Please sign in to comment.