-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommand_Handler.py
81 lines (64 loc) · 3.04 KB
/
Command_Handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from WebTask.Web_Crawler import Download_Task
from Analysis.JsonHandler_Sta import Station_List
from Analysis.JsonHandler_Bus import Bus_List
from Analysis.JsonHandler_R import getRouteDetail
def eBusCommand(CommandArr):
Command = CommandArr[0]
if(Command == "/help" or Command == "/help@NHITC_Bot"):
Output = "*為必填資訊\n/route <*路線名稱> <方向(0去程, 1回程)>\n/station <*站牌名稱> <路線名稱>\n/bus <*車號>"
return Output
KeyWord = CommandArr[1]
KeyWord = KeyWord.replace('(','(')
KeyWord = KeyWord.replace(')',')')
if(Command == "/route" or Command == "/route@NHITC_Bot"):
#是否搜尋方向
Direction = 2
if(len(CommandArr) == 3):
Direction = int(CommandArr[2])
Buffer = getRouteDetail(KeyWord)
Output = []
if(Direction != 2):
Output.append(Buffer[Direction])
else:
Output = Buffer
return Output
if(Command == "/station" or Command == "/station@NHITC_Bot"):
#是否同時搜尋路線
RouteName = ""
if(len(CommandArr) == 3):
RouteName = "/" + CommandArr[2]
Output = {}
#搜尋臺北市各站到站資訊
Search_URL = f"https://ptx.transportdata.tw/MOTC/v2/Bus/EstimatedTimeOfArrival/City/Taipei{RouteName}?$format=JSON&$filter=StopName/Zh_tw eq '{KeyWord}'&$orderby=RouteUID"
Output_Code, Buffer = Download_Task(Search_URL)
print(Output_Code, end = "\n")
if(Buffer != "[]"):
Output = Station_List(Buffer, Output)
#搜尋新北市各站到站資訊
Search_URL = f"https://ptx.transportdata.tw/MOTC/v2/Bus/EstimatedTimeOfArrival/City/NewTaipei{RouteName}?$format=JSON&$filter=StopName/Zh_tw eq '{KeyWord}'&$orderby=RouteUID"
Output_Code, Buffer = Download_Task(Search_URL)
print(Output_Code, end = "\n")
if(Buffer != "[]"):
Output = Station_List(Buffer, Output)
strOutput = f"{KeyWord}\n"
for Run in Output.keys():
strOutput += f"{Run}\n{Output[Run]}\n"
if(strOutput == ""):
strOutput = "Station not found"
return strOutput
if(Command == "/bus" or Command == "/bus@NHITC_Bot"):
#取得臺北市公車資料
Search_URL = "https://ptx.transportdata.tw/MOTC/v2/Bus/RealTimeNearStop/City/Taipei?$format=JSON&$filter=PlateNumb eq '%s'" %KeyWord
Output_Code, Output = Download_Task(Search_URL)
print(Output_Code, end = "\n")
#取得新北市公車資料
if(Output == "[]"):
Search_URL = "https://ptx.transportdata.tw/MOTC/v2/Bus/RealTimeNearStop/City/NewTaipei?$format=JSON&$filter=PlateNumb eq '%s'" %KeyWord
Output_Code, Output = Download_Task(Search_URL)
print(Output_Code, end = "\n")
#判斷是否有找到此車輛
if(Output == "[]"):
Output = "Bus not found"
else:
Output = Bus_List(Output)
return Output