Skip to content

Commit

Permalink
feat: show bus list and choose bus
Browse files Browse the repository at this point in the history
  • Loading branch information
Thungghuan committed Sep 18, 2022
1 parent cb682fe commit 7a62fe9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
20 changes: 10 additions & 10 deletions bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def __init__(self, token) -> None:
session = requests.session()
session.headers["User-Agent"] = user_agent
session.headers["authorization"] = token
session.headers["Content-Type"] = "application/json"

self.session = session
self.base_url = "https://life.gzic.scut.edu.cn/commute/open/commute"
Expand All @@ -28,23 +29,22 @@ def ticket_detail(self, ticket_id):

return result

def get_bus_list(self, page_num=0, page_size=0):
url = "{}/commuteOrder/frequencyChoice?PageNum={}&PageSize={}".format(
self.base_url, page_num, page_size
)

def get_bus_list(self, start_campus, end_campus, date):
url = self.base_url + "/commuteOrder/frequencyChoice?PageNum=0&PageSize=0"

data = {
"endCampus": "大学城校区",
"endDate": "2022/09/25",
"startDate": "2022/09/18",
"startCampus": "广州国际校区",
"startDate": date,
"startCampus": start_campus,
"endDate": date,
"endCampus": end_campus,
"startHsTime": "00:00",
"endHsTime": "23:59",
}

result = self.session.post(url, data).json()
result = self.session.post(url, json=data).json()

return result
return result["list"]

def reserve_bus(self):
url = "{}/commuteOrder/submitTicket"
Expand Down
43 changes: 41 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os.path as path
from datetime import datetime
import questionary
from get_token import get_token, check_token_expired
from bus import Bus
Expand All @@ -21,7 +22,6 @@ def login():


def main():

print("gzic_bus / gzic校巴预约")

token = ""
Expand All @@ -40,7 +40,46 @@ def main():
login()

bus = Bus(token)
print(bus.list_reserve())

campus = ["广州国际校区", "大学城校区", "五山校区"]

start_campus = questionary.select("请选择起点", choices=campus).ask()
end_campus = questionary.select(
"请选择终点", choices=list(filter(lambda x: x != start_campus, campus))
).ask()

today = datetime.today().strftime("%Y/%m/%d")
date = questionary.text("请输入查询日期,格式为:yyyy/mm/dd".format(today), default=today).ask()

bus_list = bus.get_bus_list(start_campus, end_campus, date)
bus_choices = []

if len(bus_list) > 0:
for idx, bus in enumerate(bus_list):
bus_choices.append(
{
"name": "{}. {}-{}".format(
idx + 1, bus["startDate"], bus["endDate"]
),
"value": idx,
"disabled": bus["tickets"] == 0,
}
)

bus_idx = questionary.select(
"请选择班次(灰色为被预约完的班次):",
choices=bus_choices,
style=questionary.Style(
[
("disabled", "#858585 italic"),
]
)
).ask()

print(bus_list[bus_idx])

else:
print("{}已经没有校巴了".format(date))


if __name__ == "__main__":
Expand Down

0 comments on commit 7a62fe9

Please sign in to comment.