Skip to content

Commit

Permalink
🎨 Improve structure of the code
Browse files Browse the repository at this point in the history
  • Loading branch information
boxie123 committed Jan 29, 2023
1 parent b920d0b commit 1bcbb22
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 87 deletions.
40 changes: 19 additions & 21 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
from rich.prompt import Prompt
from rich.tree import Tree

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

if __name__ == "__main__":
Expand Down Expand Up @@ -37,39 +33,41 @@
)
client = login.main()

function_dict = {
0: "退出",
1: "生成[b green]大航海[/b green]信息记录([b blue]xlsx[/b blue]格式)",
2: "生成可直接导入 [b green]BiliMessenger[/b green] 使用的大航海数据列表([b blue]csv[/b blue]格式)",
3: "生成[b green]所有礼物流水[/b green]列表([b blue]xlsx[/b blue]格式)",
4: "同时生成[b yellow]上述三个[/b yellow]文件",
}

# 询问用户使用什么功能
console.print("\n本程序目前拥有的功能:\n")
function_list = Tree("[b]功能列表[/b]")
function_list.add("[[b blue]0[/b blue]] 退出")
function_list.add(
"[[b blue]1[/b blue]] 生成[b green]大航海[/b green]信息记录([b blue]xlsx[/b blue]格式)"
)
function_list.add(
"[[b blue]2[/b blue]] 生成可直接导入 [b green]BiliMessenger[/b green] 使用的大航海数据列表([b blue]csv[/b blue]格式)"
)
function_list.add(
"[[b blue]3[/b blue]] 生成[b green]所有礼物流水[/b green]列表([b blue]xlsx[/b blue]格式)"
)
function_list.add("[[b blue]4[/b blue]] 同时生成[b yellow]上述三个[/b yellow]文件")
console.print(function_list)

function_list_tree = Tree("[b]功能列表[/b]")
for key, values in function_dict.items():
function_list_tree.add(f"[b][[cyan]{key}[/cyan]][/b] {values}")
console.print(function_list_tree)

console.print("之后会要求您输入想要查询的日期区间([b red]闭区间[/b red],统计结果包含开始和结束日期的礼物数据)")
console.print("\n[i]下列所有问题直接回车将默认为 [b cyan]括号中[/b cyan] 结果[/i]")

choice = Prompt.ask(
"\n请输入[b blue]相应数字[/b blue]来使用相对应的功能:",
choices=["0", "1", "2", "3", "4"],
choices=[str(key) for key in function_dict.keys()],
default="4",
)
if choice == "0":
sys.exit(0)

# 使用功能
gift_info = getGift.GiftInfo(client)
console.rule("获取礼物信息")
console.rule("[b]获取礼物信息", characters="=")
asyncio.run(gift_info.main(choice))

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

Expand Down
10 changes: 5 additions & 5 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,16 @@ build-backend = "pdm.pep517.api"
authors = [
{name = "铂屑", email = "[email protected]"},
]
requires-python = ">=3.10"
requires-python = ">=3.10,<3.12"
dependencies = [
"qrcode>=7.3.1",
"Pillow>=9.4.0",
"httpx>=0.23.3",
"xlsxwriter>=3.0.7",
"qrcode<8.0.0,>=7.3.1",
"Pillow<10.0.0,>=9.2.0",
"httpx<1.0.0,>=0.23.0",
"xlsxwriter<4.0.0,>=3.0.7",
"rich>=13.3.1",
]
name = "bilibili_getreceivedgiftstream"
version = "0.8.0"
version = "0.8.2.fix1"
description = "登录并获取bilibili账号某段时间礼物流水"
license = {text = "MIT"}

Expand Down
89 changes: 35 additions & 54 deletions src/getGift.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import httpx
import xlsxwriter
from rich.progress import track
from rich.prompt import IntPrompt, Confirm

from . import agent
from .console import console
Expand Down Expand Up @@ -68,41 +69,31 @@ def guard_info(gifts_list):
return gift_result, id_index


def try_int(num):
while True:
try:
int_num = int(num)
break
except ValueError:
console.print("\n[bold red]Warning:[/bold red] 输入错误,请输入纯数字")
num = console.input("请重新输入:")
return int_num


class GiftInfo:
# 构造函数,生成特定格式的日期列表
def __init__(self, client):
def __init__(self, client: httpx.Client):
self.cookies = client.cookies
client.close()
ask_list = ["年份", "月份", "日期"]
datetime_today = datetime.datetime.today()
today_date_list = [
datetime_today.year,
datetime_today.month,
datetime_today.day,
]
while True:
console.rule("输入查询开始日期")
year_begin = console.input("请输入想查询的开始[b blue]年份[/b blue](四位纯数字)(直接回车默认今年):")
if year_begin == "":
self.year_begin = datetime.datetime.today().year
else:
self.year_begin = try_int(year_begin)

month_begin = console.input("请输入想查询的开始[b blue]月份[/b blue](直接回车默认本月):")
if month_begin == "":
self.month_begin = datetime.datetime.today().month
else:
self.month_begin = try_int(month_begin)
console.rule("[b]输入查询开始日期")

begin_date_list = [0 for _ in range(3)]
for i in range(3):
ask = "请输入想查询的开始[b blue]{}[/b blue]".format(ask_list[i])
begin_date_list[i] = IntPrompt.ask(
ask,
console=console,
default=today_date_list[i],
)

day_begin = console.input("请输入想查询的开始[b blue]日期[/b blue](直接回车默认今日):")
if day_begin == "":
self.day_begin = datetime.datetime.today().day
else:
self.day_begin = try_int(day_begin)
self.year_begin, self.month_begin, self.day_begin = begin_date_list

try:
date_begin = datetime.date(
Expand All @@ -121,30 +112,22 @@ def __init__(self, client):
)
)
console.print("您查询的日期超出范围的部分数据将[b red]全部为0[/b red]\n")
while True:
choice = console.input("输入[b blue]y[/b blue]确认继续:")
if choice == "y" or choice == "Y":
break
if not Confirm.ask("是否确认继续?", console=console):
sys.exit(0)

while True:
console.rule("输入查询末尾日期")
year_end = console.input("请输入想查询的结束[b blue]年份[/b blue](四位纯数字)(直接回车默认今年):")
if year_end == "":
self.year_end = datetime.datetime.today().year
else:
self.year_end = try_int(year_end)

month_end = console.input("请输入想查询的结束[b blue]月份[/b blue](直接回车默认本月):")
if month_end == "":
self.month_end = datetime.datetime.today().month
else:
self.month_end = try_int(month_end)
console.rule("[b]输入查询末尾日期")

end_date_list = [0 for _ in range(3)]
for i in range(3):
ask = "请输入想查询的末尾[b blue]{}[/b blue]".format(ask_list[i])
end_date_list[i] = IntPrompt.ask(
ask,
console=console,
default=today_date_list[i],
)

day_end = console.input("请输入想查询的结束[b blue]日期[/b blue](直接回车默认今日):")
if day_end == "":
self.day_end = datetime.datetime.today().day
else:
self.day_end = try_int(day_end)
self.year_end, self.month_end, self.day_end = end_date_list

try:
date_end = datetime.date(self.year_end, self.month_end, self.day_end)
Expand All @@ -155,10 +138,8 @@ def __init__(self, client):

if date_begin > date_end:
console.print("\n[bold red]Warning:[/bold red] 开始日期晚于结束日期,将生成空表格\n")
while True:
choice = console.input("输入0确认继续:")
if choice == "0":
break
if not Confirm.ask("是否确认继续?", console=console):
sys.exit(0)

day_list_range = []
delta = datetime.timedelta(days=1)
Expand Down
2 changes: 1 addition & 1 deletion src/up_to_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

def main(now_tag):
console.print("\n\n")
console.rule("检测更新")
console.rule("[b]检测更新", characters="=")
header = {"accept": "application/vnd.github.v3+json"}
url = (
"https://api.github.com/repos/boxie123/Bilibili_GetReceivedGiftStream/releases"
Expand Down

0 comments on commit 1bcbb22

Please sign in to comment.