-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTraTicketBooker.py
1328 lines (1219 loc) · 48.8 KB
/
TraTicketBooker.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# Author: gw19
# Copyright (c) 2017 GW19
# -----------------------------------#
#
import json
import sys
from datetime import datetime, timedelta
from PIL import Image
from PyQt4 import QtCore, QtGui
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
QtCore.QTextCodec.setCodecForTr(
QtCore.QTextCodec.codecForName('utf8')
)
class TraTicketBooker(QtGui.QDialog):
# The GUI of TRA Ticket Booker.
def __init__(self, parent=None):
super(TraTicketBooker, self).__init__(parent)
self.setWindowTitle('TRA Ticket Booker')
self.setWindowIcon(QtGui.QIcon('Icons\\rocket_ship.png'))
self.font_size = 12
font_label_standard = QtGui.QFont('微軟正黑體', self.font_size)
QtGui.QApplication.setFont(font_label_standard)
QtGui.QApplication.setApplicationName('TRA Ticket Booker')
QtGui.QApplication.setApplicationVersion('1.0')
self.setWindowFlags(
QtCore.Qt.WindowCloseButtonHint |
QtCore.Qt.WindowMinimizeButtonHint
)
QtGui.QApplication.setStyle('cleanlooks')
# Set and Show Splash Screen.
pic_splash = QtGui.QPixmap(
'Icons\\splash_screen.png'
)
splash_screen = QtGui.QSplashScreen(
pic_splash,
QtCore.Qt.WindowStaysOnTopHint
)
splash_font = splash_screen.font()
splash_font.setFamily('華康中圓體')
splash_font.setPixelSize(23)
splash_screen.setFont(splash_font)
splash_screen.show()
processing_message = '正在初始化應用程式...'
splash_screen.showMessage(
splash_screen.tr(processing_message),
QtCore.Qt.AlignBottom |
QtCore.Qt.AlignHCenter|
QtCore.Qt.AlignAbsolute,
QtCore.Qt.black
)
# Get the List of ComboBoxes.
self.station_list = self.get_station_list()
if self.station_list == -1:
QtGui.QMessageBox.critical(
self, "Error", self.tr(
'車站資料讀取失敗,' +
'請檢查檔案「station.json」' +
'是否存在並與主程式置放於' +
'同一個資料夾中。'
)
)
sys.exit()
# Set the Font of Labels.
self.font_label_go_back = QtGui.QFont(
'微軟正黑體', self.font_size
)
self.font_label_go_back.setBold(True)
# Get the Data of TRA.
self.date_list = self.get_date_list()
self.time_list = self.get_time_list()
self.train_list = self.get_train_list()
self.ticket_list = self.get_ticket_list()
self.input_data = {}
self.select_data = {}
self.select_data_value = {}
self.input_number = {}
self.re_order = 0
# Start UP the Selenium + PhantomJS Driver.
self.driver = self.start_up_driver()
# Close Splash Screen
# After Completely Initialized.
splash_screen.close()
# Setup the Initial Layouts.
self.set_layout()
def set_layout(self):
# Setup the Initial Layouts.
# -------------------------------------------------------------------#
# ----------------------- Set the layout "ID". ----------------------#
self.layout_id_label = QtGui.QGridLayout()
self.layout_id_input = QtGui.QGridLayout()
self.layout_id_ps = QtGui.QGridLayout()
self.label_id = QtGui.QLabel('身份證字號')
self.layout_id_label.addWidget(
self.label_id, 0, 0
)
self.line_edit_id = QtGui.QLineEdit()
self.layout_id_input.addWidget(
self.line_edit_id, 0, 0
)
self.line_edit_id.setMaxLength(10) # Max Length of ID
# Connecting to the Button-Event while Press "Enter".
self.line_edit_id.returnPressed.connect(
self.button_book_go_back_event
)
self.label_ps = QtGui.QLabel(
'Enter=訂來回票'
)
self.label_ps.setFont(
QtGui.QFont('微軟正黑體', 11, italic=True)
)
self.layout_id_ps.addWidget(
self.label_ps, 0, 0, 1, 2
)
# -------------------------------------------------------------------#
# ---------------- Set the layout "Label, Go/Single". ---------------#
self.layout_combo_label = QtGui.QGridLayout()
self.layout_go_title = QtGui.QGridLayout()
self.layout_go_combo = QtGui.QGridLayout()
self.layout_button_book = QtGui.QGridLayout()
self.title_go = QtGui.QLabel('【 出發/單程 】')
self.layout_go_title.addWidget(
self.title_go, 0, 0
)
self.title_go.setFont(
self.font_label_go_back
)
# Align to Center of Label.
self.title_go.setAlignment(
QtCore.Qt.AlignCenter |
QtCore.Qt.AlignVCenter
)
# Station - From
self.label_station_from = QtGui.QLabel('起站')
self.layout_combo_label.addWidget(
self.label_station_from, 1, 0
)
self.combo_box_station_go_from = QtGui.QComboBox()
self.layout_go_combo.addWidget(
self.combo_box_station_go_from, 1, 0
)
self.combo_box_station_go_from.addItems(
self.station_list
)
# (default 108-中壢)
self.combo_box_station_go_from.setCurrentIndex(15)
self.combo_box_station_go_from.activated.connect(
self.cb_stn_from_event
)
# Station - End
self.label_station_end = QtGui.QLabel('到站')
self.layout_combo_label.addWidget(
self.label_station_end, 2, 0
)
self.combo_box_station_go_end = QtGui.QComboBox()
self.layout_go_combo.addWidget(
self.combo_box_station_go_end, 2, 0
)
self.combo_box_station_go_end.addItems(
self.station_list
)
# (default 158-斗六)
self.combo_box_station_go_end.setCurrentIndex(57)
self.combo_box_station_go_end.activated.connect(
self.cb_stn_end_event
)
# Date
self.label_date = QtGui.QLabel('日期')
self.layout_combo_label.addWidget(
self.label_date, 3, 0
)
self.combo_box_date_go = QtGui.QComboBox()
self.layout_go_combo.addWidget(
self.combo_box_date_go, 3, 0
)
self.combo_box_date_go.addItems(self.date_list)
self.combo_box_date_go.setCurrentIndex(3)
self.combo_box_date_go.activated.connect(
self.cb_go_date_event
)
# Time - From
self.label_time_from = QtGui.QLabel('起始時間')
self.layout_combo_label.addWidget(
self.label_time_from, 4, 0
)
self.combo_box_time_go_from = QtGui.QComboBox()
self.layout_go_combo.addWidget(
self.combo_box_time_go_from, 4, 0
)
self.combo_box_time_go_from.addItems(self.time_list)
self.combo_box_time_go_from.setCurrentIndex(17)
self.combo_box_time_go_from.activated.connect(
self.cb_go_time_event
)
# Time - End
self.label_time_end = QtGui.QLabel('截止時間')
self.layout_combo_label.addWidget(
self.label_time_end, 5, 0
)
self.combo_box_time_go_end = QtGui.QComboBox()
self.layout_go_combo.addWidget(
self.combo_box_time_go_end, 5, 0
)
self.combo_box_time_go_end.addItems(
self.time_list[
self.combo_box_time_go_from.currentIndex()+1:
]
)
self.combo_box_time_go_end.setCurrentIndex(1)
# Train Type
self.label_train = QtGui.QLabel('車種')
self.layout_combo_label.addWidget(
self.label_train, 6, 0
)
self.combo_box_train_go = QtGui.QComboBox()
self.layout_go_combo.addWidget(
self.combo_box_train_go, 6, 0
)
self.combo_box_train_go.addItems(self.train_list)
self.combo_box_train_go.setCurrentIndex(1)
# Ticket - Numbers
self.label_ticket = QtGui.QLabel('訂票張數')
self.layout_combo_label.addWidget(
self.label_ticket, 7, 0
)
self.combo_box_ticket_go = QtGui.QComboBox()
self.layout_go_combo.addWidget(
self.combo_box_ticket_go, 7, 0
)
self.combo_box_ticket_go.addItems(self.ticket_list)
# Button - Buy Single Ticket
self.button_book_single = QtGui.QPushButton(
'\n訂單程票\n'
)
self.layout_button_book.addWidget(
self.button_book_single, 0, 0
)
icon_single = QtGui.QIcon('Icons\\single_arrow.png')
self.button_book_single.setIcon(icon_single)
self.connect(
self.button_book_single,
QtCore.SIGNAL('clicked()'),
self.button_book_single_event
)
self.button_book_single.setAutoDefault(False)
# -------------------------------------------------------------------#
# ---------------------- Set the layout "Back". ---------------------#
self.layout_back_title = QtGui.QGridLayout()
self.layout_back_combo = QtGui.QGridLayout()
self.title_back = QtGui.QLabel('【 回程 】')
self.layout_back_title.addWidget(
self.title_back, 0, 0
)
self.title_back.setFont(
self.font_label_go_back
)
# Align to Center of Label.
self.title_back.setAlignment(
QtCore.Qt.AlignCenter |
QtCore.Qt.AlignVCenter
)
# Station - From
self.combo_box_station_back_from = QtGui.QComboBox()
self.layout_back_combo.addWidget(
self.combo_box_station_back_from, 1, 0
)
self.combo_box_station_back_from.addItems(
self.station_list
)
# (default 158-斗六)
self.combo_box_station_back_from.setCurrentIndex(
self.combo_box_station_go_end.currentIndex()
)
self.combo_box_station_back_from.setDisabled(True)
# Station - End
self.combo_box_station_back_end = QtGui.QComboBox()
self.layout_back_combo.addWidget(
self.combo_box_station_back_end, 2, 0
)
self.combo_box_station_back_end.addItems(
self.station_list
)
# (default 108-中壢)
self.combo_box_station_back_end.setCurrentIndex(
self.combo_box_station_go_from.currentIndex()
)
self.combo_box_station_back_end.setDisabled(True)
# Date
self.combo_box_date_back = QtGui.QComboBox()
self.layout_back_combo.addWidget(
self.combo_box_date_back, 3, 0
)
self.combo_box_date_back.addItems(self.date_list)
self.combo_box_date_back.setCurrentIndex(5)
# Time - From
self.combo_box_time_back_from = QtGui.QComboBox()
self.layout_back_combo.addWidget(
self.combo_box_time_back_from, 4, 0
)
self.combo_box_time_back_from.addItems(
self.time_list
)
self.combo_box_time_back_from.setCurrentIndex(17)
self.combo_box_time_back_from.activated.connect(
self.cb_back_time_event
)
# Time - End
self.combo_box_time_back_end = QtGui.QComboBox()
self.layout_back_combo.addWidget(
self.combo_box_time_back_end, 5, 0
)
self.combo_box_time_back_end.addItems(
self.time_list[
self.combo_box_time_back_from.currentIndex()+1:
]
)
self.combo_box_time_back_end.setCurrentIndex(1)
# Train - Type
self.combo_box_train_back = QtGui.QComboBox()
self.layout_back_combo.addWidget(
self.combo_box_train_back, 6, 0
)
self.combo_box_train_back.addItems(
self.train_list
)
self.combo_box_train_back.setCurrentIndex(1)
# Ticket - Numbers
self.combo_box_ticket_back = QtGui.QComboBox()
self.layout_back_combo.addWidget(
self.combo_box_ticket_back, 7, 0
)
self.combo_box_ticket_back.addItems(
self.ticket_list
)
# Button - Buy Go-Back Ticket
self.button_book_go_back = QtGui.QPushButton(
'\n訂來回票\n'
)
self.layout_button_book.addWidget(
self.button_book_go_back, 0, 1
)
icon_go_back = QtGui.QIcon(
'Icons\\go_return_arrow.png'
)
self.button_book_go_back.setIcon(icon_go_back)
self.connect(
self.button_book_go_back,
QtCore.SIGNAL('clicked()'),
self.button_book_go_back_event
)
self.button_book_go_back.setAutoDefault(False)
# -------------------------------------------------------------------#
# -----------------Set the layout "Input and Result". ---------------#
## Show Image
self.layout_input_result = QtGui.QGridLayout()
self.label_auth_num = QtGui.QLabel(
'【 認證碼顯示/輸入 】'
)
self.layout_input_result.addWidget(
self.label_auth_num, 0, 0, 1, 2
)
self.label_auth_num.setFont(
self.font_label_go_back
)
self.label_auth_num.setAlignment( # Align to Center
QtCore.Qt.AlignCenter |
QtCore.Qt.AlignVCenter
)
self.pic_box = QtGui.QLabel()
self.layout_input_result.addWidget(
self.pic_box, 1, 0, 3, 2
)
self.pic_box.setStyleSheet(
'border:1px solid rgb(0, 0, 0)'
)
## Confirm and Send the Authent-Number.
self.line_edit_num = QtGui.QLineEdit()
self.layout_input_result.addWidget(
self.line_edit_num, 4, 0
)
self.line_edit_num.setMaxLength(6) # Max Length of ID
# Connecting to the Button-Event while Press "Enter".
self.line_edit_num.returnPressed.connect(
self.button_input_num_event
)
self.button_input_num = QtGui.QPushButton('確認')
self.layout_input_result.addWidget(
self.button_input_num, 4, 1
)
icon_input_num = QtGui.QIcon('Icons\\confirm.png')
self.button_input_num.setIcon(icon_input_num)
self.connect(
self.button_input_num,
QtCore.SIGNAL('clicked()'),
self.button_input_num_event
)
self.button_input_num.setAutoDefault(False)
## Show Results
self.label_result = QtGui.QLabel(
'【 訂票結果 】'
)
self.layout_input_result.addWidget(
self.label_result, 5, 0, 1, 2
)
self.label_result.setFont(
self.font_label_go_back
)
# Align to Center of Label.
self.label_result.setAlignment(
QtCore.Qt.AlignCenter |
QtCore.Qt.AlignVCenter
)
#
self.label_show_result = QtGui.QLabel()
self.layout_input_result.addWidget(
self.label_show_result, 6, 0, 8, 2
)
self.label_show_result.setStyleSheet(
'border:1px solid rgb(0, 0, 0)'
)
self.label_show_result.setAlignment(
QtCore.Qt.AlignTop |
QtCore.Qt.AlignLeft
)
self.label_show_result.setWordWrap(True)
# -------------------------------------------------------------------#
# ---------------- Make Layouts in the Main Window. -----------------#
# ---------------- And Set the Location of Layouts. -----------------#
self.layout_main = QtGui.QGridLayout(self)
self.layout_main.setSpacing(10)
# Layout ID Block
self.layout_main.addLayout(
self.layout_id_label, 0, 0
)
self.layout_main.addLayout(
self.layout_id_input, 0, 1
)
self.layout_main.addLayout(
self.layout_id_ps, 0, 2, 1, 2
)
# Layout Go Title Block
self.layout_main.addLayout(
self.layout_go_title, 1, 1
)
# Layout Back Title Block
self.layout_main.addLayout(
self.layout_back_title, 1, 2
)
# Layout Go-Back Combobox Block
self.layout_main.addLayout(
self.layout_combo_label, 2, 0
)
self.layout_main.addLayout(
self.layout_go_combo, 2, 1
)
self.layout_main.addLayout(
self.layout_back_combo, 2, 2
)
# Layout Single / Go-Back Button Block
self.layout_main.addLayout(
self.layout_button_book, 3, 1, 1, 2
)
# Layout Result Block
self.layout_main.addLayout(
self.layout_input_result, 1, 3, 4, 1
)
self.layout_main.setSizeConstraint(
QtGui.QLayout.SetFixedSize
)
# -------------------------------------------------------------------#
# -------------------------- Set the Size. --------------------------#
## QWidget Size
width_pic_box = self.pic_box.width()
reduce_ratio = 0.32
self.line_edit_id.setFixedWidth(
width_pic_box * reduce_ratio * 0.6
)
self.pic_box.setFixedWidth(
width_pic_box * reduce_ratio
)
self.line_edit_num.setFixedWidth(
width_pic_box * reduce_ratio * 0.6
)
self.button_input_num.setFixedWidth(
width_pic_box * reduce_ratio * 0.356
)
self.label_show_result.setFixedWidth(
width_pic_box * reduce_ratio
)
## Icons Size
size_set = [3, 4, 2] # For Windows 7
# size_set = [4, 5, 2.5] # For Windows 10
self.button_book_single.setIconSize(
QtCore.QSize(
self.font_size * size_set[0],
self.font_size * size_set[0]
)
)
self.button_book_go_back.setIconSize(
QtCore.QSize(
self.font_size * size_set[1],
self.font_size * size_set[1]
)
)
self.button_input_num.setIconSize(
QtCore.QSize(
self.font_size * size_set[2],
self.font_size * size_set[2]
)
)
# ComboBox Style
self.combo_box_station_go_from.setStyle(
QtGui.QStyleFactory.create('plastique')
)
self.combo_box_station_go_end.setStyle(
QtGui.QStyleFactory.create('plastique')
)
self.combo_box_station_back_from.setStyle(
QtGui.QStyleFactory.create('plastique')
)
self.combo_box_station_back_end.setStyle(
QtGui.QStyleFactory.create('plastique')
)
# -----------------------------------------------------------------------#
# ----------------------------- GUI Events. -----------------------------#
def start_up_driver(self):
driver = webdriver.PhantomJS()
return driver
def get_station_list(self):
try:
with open('station.json', 'r', encoding='utf8') as f:
sdata = json.loads(f.read())
# Sort it by station numbers
sdata_sorted = sorted(
sdata, key=lambda s: s['時刻表編號']
)
station_list = []
for ii in range(len(sdata_sorted)):
station_list.append(
'%03d' % int(sdata_sorted[ii]['編號'])
+ '-'
+ sdata_sorted[ii]['站名']
)
return station_list
except IOError as ioerr:
print('File Error: ' + str(ioerr))
return -1
def get_date_list(self):
date_list = []
for ii in range(17):
if datetime.today().time().hour < 23:
local_time = datetime.today() + timedelta(ii)
else:
local_time = datetime.today() + timedelta(ii + 1)
lt_year, lt_mon, lt_day, lt_hour, lt_min, lt_sec, \
lt_wday, lt_yday, lt_isdst = local_time.timetuple()
date_list.append(
'%04d/%02d/%02d'
% (lt_year, lt_mon, lt_day)
)
return date_list
def get_time_list(self):
time_list = []
for ii in range(24):
time_list.append('%02d:%02d' % (ii, 00))
return time_list
def get_train_list(self):
train_list = ['全部車種', '自強號', '莒光號', '復興號']
return train_list
def get_ticket_list(self):
ticket_list = ['1', '2', '3', '4', '5', '6']
return ticket_list
def cb_stn_from_event(self):
self.combo_box_station_back_end.setCurrentIndex(
self.combo_box_station_go_from.currentIndex()
)
def cb_stn_end_event(self):
self.combo_box_station_back_from.setCurrentIndex(
self.combo_box_station_go_end.currentIndex()
)
def cb_go_date_event(self):
self.combo_box_date_back.clear()
self.combo_box_date_back.addItems(
self.date_list[
self.combo_box_date_go.currentIndex():
]
)
self.combo_box_date_back.setCurrentIndex(0)
def cb_go_time_event(self):
self.combo_box_time_go_end.clear()
self.combo_box_time_go_end.addItems(
self.time_list[
self.combo_box_time_go_from.currentIndex()+1:
]
)
self.combo_box_time_go_end.setCurrentIndex(1)
def cb_back_time_event(self):
self.combo_box_time_back_end.clear()
self.combo_box_time_back_end.addItems(
self.time_list[
self.combo_box_time_back_from.currentIndex()+1:
]
)
self.combo_box_time_back_end.setCurrentIndex(1)
def button_book_single_event(self):
# While Pushed the Button "Buy Single Ticket".
self.input_data.clear()
self.select_data.clear()
self.select_data_value.clear()
self.input_data.update({
'person_id': self.line_edit_id.text()
})
self.select_data.update({
'from_station':
self.combo_box_station_go_from.currentText(),
'to_station':
self.combo_box_station_go_end.currentText(),
'getin_date':
self.combo_box_date_go.currentText(),
'getin_start_dtime':
self.combo_box_time_go_from.currentText(),
'getin_end_dtime':
self.combo_box_time_go_end.currentText(),
'train_type':
self.combo_box_train_go.currentText(),
'order_qty_str':
self.combo_box_ticket_go.currentText(),
})
# Deal with the Values in order to Input Data in TRA.
self.station_go_from_value = \
self.combo_box_station_go_from.currentText().split('-')[0]
self.station_go_end_value = \
self.combo_box_station_go_end.currentText().split('-')[0]
self.date_go_value = \
self.combo_box_date_go.currentText() \
+ '-' + '%02d' % (
self.date_list.index(
self.combo_box_date_go.currentText()
)
)
if self.combo_box_train_go.currentIndex() == 0:
self.train_go_value = '*4'
else:
self.train_go_value = \
'*' + str(self.combo_box_train_go.currentIndex())
self.select_data_value.update({
'from_station':
self.station_go_from_value,
'to_station':
self.station_go_end_value,
'getin_date':
self.date_go_value,
'getin_start_dtime':
self.combo_box_time_go_from.currentText(),
'getin_end_dtime':
self.combo_box_time_go_end.currentText(),
'train_type':
self.train_go_value,
'order_qty_str':
self.combo_box_ticket_go.currentText(),
})
self.book_type = 1
self.run_main_process()
def button_book_go_back_event(self):
# While Pushed the Button "Buy Go-Back Ticket".
self.input_data.clear()
self.select_data.clear()
self.select_data_value.clear()
self.input_data.update({
'person_id': self.line_edit_id.text()
})
self.select_data.update({
'from_station':
self.combo_box_station_go_from.currentText(),
'to_station':
self.combo_box_station_go_end.currentText(),
'getin_date':
self.combo_box_date_go.currentText(),
'getin_date2':
self.combo_box_date_back.currentText(),
'getin_start_dtime':
self.combo_box_time_go_from.currentText(),
'getin_end_dtime':
self.combo_box_time_go_end.currentText(),
'getin_start_dtime2':
self.combo_box_time_back_from.currentText(),
'getin_end_dtime2':
self.combo_box_time_back_end.currentText(),
'train_type':
self.combo_box_train_go.currentText(),
'train_type2':
self.combo_box_train_back.currentText(),
'order_qty_str':
self.combo_box_ticket_go.currentText(),
'order_qty_str2':
self.combo_box_ticket_back.currentText()
})
# Deal with the Values in order to Input Data in TRA.
self.station_go_from_value = \
self.combo_box_station_go_from.currentText().split('-')[0]
self.station_go_end_value = \
self.combo_box_station_go_end.currentText().split('-')[0]
self.date_go_value = \
self.combo_box_date_go.currentText() \
+ '-' + '%02d' % (
self.date_list.index(
self.combo_box_date_go.currentText()
)
)
self.date_back_value = \
self.combo_box_date_back.currentText() \
+ '-' + '%02d' % (
self.date_list.index(
self.combo_box_date_back.currentText()
)
)
if self.combo_box_train_go.currentIndex() == 0:
self.train_go_value = '*4'
else:
self.train_go_value = \
'*' + str(self.combo_box_train_go.currentIndex())
if self.combo_box_train_back.currentIndex() == 0:
self.train_back_value = '*4'
else:
self.train_back_value = \
'*' + str(self.combo_box_train_back.currentIndex())
self.select_data_value.update({
'from_station':
self.station_go_from_value,
'to_station':
self.station_go_end_value,
'getin_date':
self.date_go_value,
'getin_date2':
self.date_back_value,
'getin_start_dtime':
self.combo_box_time_go_from.currentText(),
'getin_end_dtime':
self.combo_box_time_go_end.currentText(),
'getin_start_dtime2':
self.combo_box_time_back_from.currentText(),
'getin_end_dtime2':
self.combo_box_time_back_end.currentText(),
'train_type':
self.train_go_value,
'train_type2':
self.train_back_value,
'order_qty_str':
self.combo_box_ticket_go.currentText(),
'order_qty_str2':
self.combo_box_ticket_back.currentText()
})
self.book_type = 2
self.run_main_process()
def button_input_num_event(self):
# Get the Inputed Authent-Number
self.input_number.clear()
self.input_number = {
'randInput': self.line_edit_num.text()
}
self.fill_input(self.input_number)
# Submit Aythentication Number,
# Show Final Results
self.submit_auth_number()
# -----------------------------------------------------------------------#
# --------------- MAIN PROCESS of Pushing Button "Booking" ------------- #
def run_main_process(self):
# Run the Main Process while Pushed the Button.
# Connect to TRA Webpage
self.connect_to_webpage()
# -- Take Inputed Values (driver.fill) -- #
self.fill_input(self.input_data)
self.fill_select()
# -- Submit User Data -- #
self.submit_user_data()
# -- Save and Open Authent-Number Image -- #
self.save_and_open_image()
def connect_to_webpage(self):
url_target_sg = 'http://railway1.hinet.net/csearch.htm'
url_target_gb = 'http://railway.hinet.net/ctkind2.htm'
self.driver.delete_all_cookies()
wait = WebDriverWait(self.driver, timeout=6)
try:
# Booking Single Ticket.
if self.book_type == 1:
self.driver.get(url_target_sg)
wait.until(
EC.presence_of_element_located(
(By.TAG_NAME, 'button')
)
)
# Booking Go-Back Ticket.
elif self.book_type == 2:
self.driver.get(url_target_gb)
wait.until(
EC.presence_of_element_located(
(By.TAG_NAME, 'button')
)
)
except:
self.label_show_result.setText(
'【連線逾時或是網路不穩定】\n' +
'【請檢查網路環境以及是否為尖峰時段。】'
)
def fill_input(self, data_for_input):
# Fill the data inputed by user into the form on the website.
input_data_key = list(data_for_input)
for ii in input_data_key:
self.driver.execute_script(
"document.querySelector('input[name=" +
'"' + ii + '"' + "]').value = " + "'" +
data_for_input[ii] + "'"
)
def fill_select(self):
# Fill the data selected by user into the form on the website.
select_data_key = list(self.select_data_value)
for ii in select_data_key:
self.driver.execute_script(
"document.querySelector('select[name=" +
'"' + ii + '"' + "]').value = " + "'" +
self.select_data_value[ii] + "'"
)
def submit_user_data(self):
# Submit to Authent-Number Page (press button).
wait = WebDriverWait(self.driver, timeout=6)
try:
self.driver.execute_script(
'document.getElementsByTagName("button")[0].click()'
)
wait.until(
EC.presence_of_element_located(
(By.ID, 'idRandomPic')
)
)
except:
self.label_show_result.setText(
'【連線逾時或是網路不穩定】\n' +
'【請檢查網路環境以及是否為尖峰時段。】'
)
def save_and_open_image(self):
# Save Image
image_path = 'temp\\ImageOut.png'
element_img = self.driver.find_element_by_id('idRandomPic')
left = element_img.location['x']
top = element_img.location['y']
right = element_img.location['x'] + element_img.size['width']
bottom = element_img.location['y'] + element_img.size['height']
self.driver.save_screenshot(image_path)
im = Image.open(image_path)
im = im.crop((left, top, right, bottom))
im.save(image_path)
# Open Image
try:
self.pic_auth_num = QtGui.QPixmap(image_path)
self.pic_box.setPixmap(self.pic_auth_num)
# Clear and Focus to the Authent-Number Inputing Field.
self.line_edit_num.clear()
self.line_edit_num.setFocus()
if self.re_order == 1:
self.label_show_result.setText(
'→ 已重新執行訂票程序,請再次輸入認證碼。'
)
self.label_show_result.setStyleSheet(
'color: rgb(0, 0, 0);'
)
self.re_order = 0
except IOError as err:
self.pic_box.setText(
'File ERROR: ' + str(err)
)
self.label_show_result.setText(
'顯示圖片程序發生錯誤,可能是找不到認證碼圖片檔,' +
'請重新操作。\n'
)
self.label_show_result.setStyleSheet(
'color: rgb(200, 0, 0);'
)
# -----------------------------------------------------------------------#
# ----- MAIN PROCESS of Pushing Button "Submit authentication code"----- #
def submit_auth_number(self):
# Submit Authent-Number to the Next Page.
self.driver.execute_script(
'document.getElementsByTagName("button")[2].click();'
)
if self.book_type == 1:
self.show_final_result_sg()
elif self.book_type == 2:
self.show_final_result_gb()
def show_final_result_sg(self):
# Show Final Results with Different Cases.
try:
wait = WebDriverWait(self.driver, timeout=2)
wait.until(
EC.text_to_be_present_in_element(
(By.TAG_NAME, 'font'), '亂數號碼錯誤'
)
)
result_final1 = self.driver.execute_script(
'result_final = ' +
'document.getElementsByTagName("font")[0].innerText;' +
'return result_final'
)