-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkorail_reservation.py
68 lines (60 loc) · 1.74 KB
/
korail_reservation.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
import sys
import time
from pydantic import BaseModel
from korail2 import Korail, AdultPassenger, TrainType, NoResultsError, KorailError
import os
from dotenv import load_dotenv
load_dotenv()
KORAIL_ID = os.getenv("KORAIL_ID")
KORAIL_PW = os.getenv("KORAIL_PW")
PSGRS = [AdultPassenger(1)]
TRAIN_TYPE = TrainType.KTX
class Train(BaseModel):
departure: str
destination: str
date: str
time: str
async def reserve_train(train: Train):
k = Korail(KORAIL_ID, KORAIL_PW)
if not k.login():
print("login fail")
exit(-1)
while True:
notFound = True
while notFound:
try:
sys.stdout.write(
"Finding Seat %s ➜ %s \r"
% (train.departure, train.destination)
)
sys.stdout.flush()
trains = k.search_train_allday(
train.departure,
train.destination,
train.date,
train.time,
passengers=PSGRS,
train_type=TRAIN_TYPE,
)
print(trains)
print("Found!!")
notFound = False
except NoResultsError:
sys.stdout.write("No Seats \r")
sys.stdout.flush()
time.sleep(2)
except Exception as e:
print(e)
time.sleep(2)
k.login()
seat = None
ok = False
try:
seat = k.reserve(trains[0], passengers=PSGRS)
ok = True
except KorailError as e:
print(e)
break
if ok:
print(seat)
return seat