-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathchrome_tixcraft.py
2838 lines (2392 loc) · 107 KB
/
chrome_tixcraft.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
#!/usr/bin/env python
#encoding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
# for close tab.
from selenium.common.exceptions import NoSuchWindowException
# for alert
from selenium.common.exceptions import UnexpectedAlertPresentException
from selenium.common.exceptions import NoAlertPresentException
# for alert 2
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
# for ["pageLoadStrategy"] = "eager"
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# for wait #1
import time
import os
import sys
import platform
import json
import random
import re
from datetime import datetime
# for error output
import logging
logging.basicConfig()
logger = logging.getLogger('logger')
#執行方式:python chrome_tixcraft.py 或 python3 chrome_tixcraft.py
#附註1:沒有寫的很好,很多地方應該可以模組化。
#附註2:
CONST_APP_VERSION = u"MaxBot (2019.09.24)"
CONST_FROM_TOP_TO_BOTTOM = u"from top to bottom"
CONST_FROM_BOTTOM_TO_TOP = u"from bottom to top"
CONST_RANDOM = u"random"
CONST_SELECT_ORDER_DEFAULT = CONST_FROM_TOP_TO_BOTTOM
CONST_SELECT_OPTIONS_DEFAULT = (CONST_FROM_TOP_TO_BOTTOM, CONST_FROM_BOTTOM_TO_TOP, CONST_RANDOM)
CONST_SELECT_OPTIONS_ARRAY = [CONST_FROM_TOP_TO_BOTTOM, CONST_FROM_BOTTOM_TO_TOP, CONST_RANDOM]
# initial webdriver
# 說明:初始化 webdriver
driver = None
# 讀取檔案裡的參數值
basis = ""
if hasattr(sys, 'frozen'):
basis = sys.executable
else:
basis = sys.argv[0]
app_root = os.path.dirname(basis)
config_filepath = os.path.join(app_root, 'settings.json')
config_dict = None
if os.path.isfile(config_filepath):
with open(config_filepath) as json_data:
config_dict = json.load(json_data)
homepage = None
browser = None
ticket_number = None
facebook_account = None
auto_press_next_step_button = None
auto_fill_ticket_number = None
auto_fill_ticket_price = None
date_auto_select_enable = None
date_auto_select_mode = None
date_keyword = None
area_auto_select_enable = None
area_auto_select_mode = None
area_keyword = None
kktix_area_auto_select_mode = None
kktix_area_keyword = None
kktix_answer_dictionary = None
kktix_answer_dictionary_list = None
if not config_dict is None:
# read config.
if 'homepage' in config_dict:
homepage = config_dict["homepage"]
if 'browser' in config_dict:
browser = config_dict["browser"]
# default ticket number
# 說明:自動選擇的票數
#ticket_number = "2"
ticket_number = ""
if 'ticket_number' in config_dict:
ticket_number = str(config_dict["ticket_number"])
facebook_account = ""
if 'facebook_account' in config_dict:
facebook_account = str(config_dict["facebook_account"])
# for ["kktix"]
if 'kktix' in config_dict:
auto_press_next_step_button = config_dict["kktix"]["auto_press_next_step_button"]
auto_fill_ticket_number = config_dict["kktix"]["auto_fill_ticket_number"]
if 'area_mode' in config_dict["kktix"]:
kktix_area_auto_select_mode = config_dict["kktix"]["area_mode"]
kktix_area_auto_select_mode = kktix_area_auto_select_mode.strip()
if not kktix_area_auto_select_mode in CONST_SELECT_OPTIONS_ARRAY:
kktix_area_auto_select_mode = CONST_SELECT_ORDER_DEFAULT
if 'area_keyword' in config_dict["kktix"]:
kktix_area_keyword = config_dict["kktix"]["area_keyword"]
if kktix_area_keyword is None:
kktix_area_keyword = ""
kktix_area_keyword = kktix_area_keyword.strip()
if 'answer_dictionary' in config_dict["kktix"]:
kktix_answer_dictionary = config_dict["kktix"]["answer_dictionary"]
if kktix_answer_dictionary is None:
kktix_answer_dictionary = ""
kktix_answer_dictionary = kktix_answer_dictionary.strip()
if len(kktix_answer_dictionary) > 0:
kktix_answer_dictionary_list = kktix_answer_dictionary.split(',')
# for ["tixcraft"]
if 'tixcraft' in config_dict:
date_auto_select_enable = config_dict["tixcraft"]["date_auto_select"]["enable"]
date_auto_select_mode = config_dict["tixcraft"]["date_auto_select"]["mode"]
if not date_auto_select_mode in CONST_SELECT_OPTIONS_ARRAY:
date_auto_select_mode = CONST_SELECT_ORDER_DEFAULT
if 'date_keyword' in config_dict["tixcraft"]["date_auto_select"]:
date_keyword = config_dict["tixcraft"]["date_auto_select"]["date_keyword"]
date_keyword = date_keyword.strip()
area_auto_select_enable = config_dict["tixcraft"]["area_auto_select"]["enable"]
area_auto_select_mode = config_dict["tixcraft"]["area_auto_select"]["mode"]
if not area_auto_select_mode in CONST_SELECT_OPTIONS_ARRAY:
area_auto_select_mode = CONST_SELECT_ORDER_DEFAULT
if 'area_keyword' in config_dict["tixcraft"]["area_auto_select"]:
area_keyword = config_dict["tixcraft"]["area_auto_select"]["area_keyword"]
area_keyword = area_keyword.strip()
# output config:
print("version", CONST_APP_VERSION)
print("homepage", homepage)
print("browser", browser)
print("ticket_number", ticket_number)
print("facebook_account", facebook_account)
# for kktix
print("==[kktix]==")
print("auto_press_next_step_button", auto_press_next_step_button)
print("auto_fill_ticket_number", auto_fill_ticket_number)
print("kktix_area_keyword", kktix_area_keyword)
print("kktix_answer_dictionary", kktix_answer_dictionary)
# for tixcraft
print("==[tixcraft]==")
print("date_auto_select_enable", date_auto_select_enable)
print("date_auto_select_mode", date_auto_select_mode)
print("date_keyword", date_keyword)
print("area_auto_select_enable", area_auto_select_enable)
print("area_auto_select_mode", area_auto_select_mode)
print("area_keyword", area_keyword)
# entry point
# 說明:自動開啟第一個的網頁
if homepage is None:
homepage = ""
if len(homepage) == 0:
homepage = "https://tixcraft.com/activity/"
Root_Dir = ""
if browser == "chrome":
chrome_options = None
# default os is linux/mac
chromedriver_path =Root_Dir+ "webdriver/chromedriver"
if platform.system()=="windows":
chromedriver_path =Root_Dir+ "webdriver/chromedriver.exe"
extension_path = Root_Dir + "webdriver/AdBlock.crx"
extension_file_exist = os.path.isfile(extension_path)
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
#chrome_options.add_argument("--disable-popup-blocking");
if extension_file_exist:
chrome_options.add_extension(extension_path)
else:
print("extention not exist")
extension_path = Root_Dir + "webdriver/BlockYourselfFromAnalytics.crx"
extension_file_exist = os.path.isfile(extension_path)
if extension_file_exist:
chrome_options.add_extension(extension_path)
else:
print("extention not exist")
caps = DesiredCapabilities().CHROME
#caps["pageLoadStrategy"] = u"normal" # complete
caps["pageLoadStrategy"] = u"eager" # interactive
#caps["pageLoadStrategy"] = u"none"
#caps["unhandledPromptBehavior"] = u"dismiss and notify" # default
caps["unhandledPromptBehavior"] = u"ignore"
#caps["unhandledPromptBehavior"] = u"dismiss"
driver = webdriver.Chrome(options=chrome_options, executable_path=chromedriver_path, desired_capabilities=caps)
if browser == "firefox":
# default os is linux/mac
chromedriver_path =Root_Dir+ "webdriver/geckodriver"
if platform.system()=="windows":
chromedriver_path =Root_Dir+ "webdriver/geckodriver.exe"
driver = webdriver.Firefox(executable_path=chromedriver_path)
driver.get(homepage)
else:
print("Config error!")
# common functions.
def find_between( s, first, last ):
try:
start = s.index( first ) + len( first )
end = s.index( last, start )
return s[start:end]
except ValueError:
return ""
# convert web string to reg pattern
def convert_string_to_pattern(my_str, dynamic_length=True):
my_hint_anwser_length = len(my_str)
my_formated = ""
if my_hint_anwser_length > 0:
my_anwser_symbols = u"()[]<>{}-"
for idx in range(my_hint_anwser_length):
char = my_str[idx:idx+1]
if char in my_anwser_symbols:
my_formated += (u'\\' + char)
continue
pattern = re.compile(u"[A-Z]")
match_result = pattern.match(char)
#print("match_result A:", match_result)
if not match_result is None:
my_formated += u"[A-Z]"
pattern = re.compile(u"[a-z]")
match_result = pattern.match(char)
#print("match_result a:", match_result)
if not match_result is None:
my_formated += u"[a-z]"
pattern = re.compile(u"[\d]")
match_result = pattern.match(char)
#print("match_result d:", match_result)
if not match_result is None:
my_formated += u"[\d]"
# for dynamic length
if dynamic_length:
for i in range(10):
my_formated = my_formated.replace(u"[A-Z][A-Z]",u"[A-Z]")
my_formated = my_formated.replace(u"[a-z][a-z]",u"[a-z]")
my_formated = my_formated.replace(u"[\d][\d]",u"[\d]")
my_formated = my_formated.replace(u"[A-Z]",u"[A-Z]+")
my_formated = my_formated.replace(u"[a-z]",u"[a-z]+")
my_formated = my_formated.replace(u"[\d]",u"[\d]+")
return my_formated
def get_answer_list_by_question(captcha_text_div_text):
return_list = None
my_answer_delimitor = ""
#if u"?" in captcha_text_div_text or u"?" in captcha_text_div_text:
if True:
tmp_text = captcha_text_div_text
tmp_text = tmp_text.replace(u' ',u' ')
tmp_text = tmp_text.replace(u':',u':')
# for hint
tmp_text = tmp_text.replace(u'*',u'*')
# replace ex.
tmp_text = tmp_text.replace(u'例如',u'範例')
tmp_text = tmp_text.replace(u'如:',u'範例:')
tmp_text = tmp_text.replace(u'舉例',u'範例')
if not u'範例' in tmp_text:
tmp_text = tmp_text.replace(u'例',u'範例')
# important, maybe 例 & ex occurs at same time.
tmp_text = tmp_text.replace(u'ex:',u'範例:')
tmp_text = tmp_text.replace(u'Ex:',u'範例:')
#tmp_text = tmp_text.replace(u'[',u'(')
#tmp_text = tmp_text.replace(u']',u')')
tmp_text = tmp_text.replace(u'?',u'?')
tmp_text = tmp_text.replace(u'(',u'(')
tmp_text = tmp_text.replace(u')',u')')
# is need to convert . ? I am not sure!
tmp_text = tmp_text.replace(u'。',u' ')
my_question = ""
my_options = ""
my_hint = ""
my_hint_anwser = ""
my_anwser_formated = ""
if u"?" in tmp_text:
question_index = tmp_text.find(u"?")
my_question = tmp_text[:question_index+1]
if u"。" in tmp_text:
question_index = tmp_text.find(u"。")
my_question = tmp_text[:question_index+1]
if len(my_question) == 0:
my_question = tmp_text
#print(u"my_question:", my_question)
# get hint from quota.
hint_list = None
# ps: hint_list is not options list
# try rule1:
if u'(' in tmp_text and u')' in tmp_text and u'範例' in tmp_text:
#import re
#print("text:" , re.findall('\([\w]+\)', tmp_text))
hint_list = re.findall(u'\(.*?\)', tmp_text)
#print("hint_list:", hint_list)
# try rule2:
if hint_list is None:
if u'【' in tmp_text and u'】' in tmp_text and u'範例' in tmp_text:
#import re
#print("text:" , re.findall('\([\w]+\)', tmp_text))
hint_list = re.findall(u'【.*?】', tmp_text)
# try rule3:
if not hint_list is None:
for hint in hint_list:
if u'範例' in hint:
my_hint = hint
if my_hint[:1] == u'【':
my_hint = my_hint[1:]
if my_hint[-1:] == u'】':
my_hint = my_hint[:-1]
break;
else:
# get hint from rule 3: with '(' & '), but ex: is outside
if u'半形' in hint:
hint_index = tmp_text.find(hint)
ex_index = tmp_text.find(u"範例")
if ex_index > 0:
ex_end_index = tmp_text.find(u" ",ex_index)
if ex_end_index < 0:
ex_end_index = tmp_text.find(u"(",ex_index)
if ex_end_index < 0:
ex_end_index = tmp_text.find(u"(",ex_index)
if ex_end_index < 0:
ex_end_index = tmp_text.find(u".",ex_index)
if ex_end_index < 0:
ex_end_index = tmp_text.find(u"。",ex_index)
if ex_end_index >=0:
my_hint = tmp_text[hint_index:ex_end_index+1]
# try rule4:
# get hint from rule 3: without '(' & '), but use "*"
if len(my_hint) == 0:
target_symbol = u"*"
if target_symbol in tmp_text :
star_index = tmp_text.find(target_symbol)
space_index = tmp_text.find(u" ", star_index + len(target_symbol))
my_hint = tmp_text[star_index: space_index]
# is need to merge next block
if len(my_hint) > 0:
target_symbol = my_hint + u" "
if target_symbol in tmp_text :
star_index = tmp_text.find(target_symbol)
next_block_index = star_index + len(target_symbol)
space_index = tmp_text.find(u" ", next_block_index)
next_block = tmp_text[next_block_index: space_index]
if u'範例' in next_block:
my_hint += u' ' + next_block
if len(my_hint) > 0:
my_hint_anwser = my_hint[my_hint.find(u"範例")+2:].strip()
if u'答案' in my_hint_anwser and u'填入' in my_hint_anwser:
# 答案為B需填入Bb)
fill_index = my_hint_anwser.find(u"填入")
my_hint_anwser = my_hint_anwser[fill_index+2:].strip()
if my_hint_anwser[:1] == u":":
my_hint_anwser = my_hint_anwser[1:]
if my_hint[:1] == u"(":
if my_hint_anwser[-1:] == u")":
my_hint_anwser = my_hint_anwser[:-1]
if my_hint_anwser[-1:] == u"。":
my_hint_anwser = my_hint_anwser[:-1]
#print(u"my_hint_anwser:", my_hint_anwser)
# try rule5:
# get hint from rule 3: n個半形英文大寫
if len(my_hint) == 0:
target_symbol = u"個半形英文大寫"
if target_symbol in tmp_text :
star_index = tmp_text.find(target_symbol)
space_index = tmp_text.find(u" ", star_index)
answer_char_count = tmp_text[star_index-1:star_index]
if answer_char_count.isnumeric():
star_index -= 1
my_hint_anwser = u'A' * int(answer_char_count)
my_hint = tmp_text[star_index: space_index]
target_symbol = u"個英文大寫"
if target_symbol in tmp_text :
star_index = tmp_text.find(target_symbol)
space_index = tmp_text.find(u" ", star_index)
answer_char_count = tmp_text[star_index-1:star_index]
if answer_char_count.isnumeric():
star_index -= 1
my_hint_anwser = u'A' * int(answer_char_count)
my_hint = tmp_text[star_index: space_index]
target_symbol = u"個半形英文小寫"
if target_symbol in tmp_text :
star_index = tmp_text.find(target_symbol)
space_index = tmp_text.find(u" ", star_index)
answer_char_count = tmp_text[star_index-1:star_index]
if answer_char_count.isnumeric():
star_index -= 1
my_hint_anwser = u'a' * int(answer_char_count)
my_hint = tmp_text[star_index: space_index]
target_symbol = u"個英文小寫"
if target_symbol in tmp_text :
star_index = tmp_text.find(target_symbol)
space_index = tmp_text.find(u" ", star_index)
answer_char_count = tmp_text[star_index-1:star_index]
if answer_char_count.isnumeric():
star_index -= 1
my_hint_anwser = u'a' * int(answer_char_count)
my_hint = tmp_text[star_index: space_index]
if len(my_hint) > 0:
my_anwser_formated = convert_string_to_pattern(my_hint_anwser)
# try rule6:
# get hint from rule 3: n個英數半形字
if len(my_hint) == 0:
target_symbol = u"個英數半形字"
if target_symbol in tmp_text :
star_index = tmp_text.find(target_symbol)
space_index = tmp_text.find(u" ", star_index)
answer_char_count = tmp_text[star_index-1:star_index]
if answer_char_count.isnumeric():
star_index -= 1
my_anwser_formated = u'[A-Za-z\d]' * int(answer_char_count)
my_hint = tmp_text[star_index: space_index]
if len(my_hint) == 0:
target_symbol = u"個半形"
if target_symbol in tmp_text :
star_index = tmp_text.find(target_symbol)
space_index = tmp_text.find(u" ", star_index)
answer_char_count = tmp_text[star_index-1:star_index]
if answer_char_count.isnumeric():
star_index -= 1
my_anwser_formated = u'[A-Za-z\d]' * int(answer_char_count)
my_hint = tmp_text[star_index: space_index]
#print(u"my_hint:", my_hint)
#print(u"my_anwser_formated:", my_anwser_formated)
my_options = tmp_text
my_options = my_options.replace(my_question,u"")
my_options = my_options.replace(my_hint,u"")
# try rule7:
# check is chinese/english in question, if match, apply my_options rule.
if len(my_hint) > 0:
tmp_text_org = captcha_text_div_text
if u'範例:' in tmp_text:
tmp_text_org = tmp_text_org.replace(u'Ex:','ex:')
target_symbol = u"ex:"
if target_symbol in tmp_text_org :
star_index = tmp_text_org.find(target_symbol)
my_options = tmp_text_org[star_index-1:]
#print(u"my_options:", my_options)
if len(my_anwser_formated) > 0:
allow_delimitor_symbols = ")].: }"
pattern = re.compile(my_anwser_formated)
search_result = pattern.search(my_options)
if not search_result is None:
(span_start, span_end) = search_result.span()
if len(my_options) > (span_end+1)+1:
maybe_delimitor = my_options[span_end+0:span_end+1]
if maybe_delimitor in allow_delimitor_symbols:
my_answer_delimitor = maybe_delimitor
#print(u"my_answer_delimitor:", my_answer_delimitor)
# try all possible options.
tmp_text = captcha_text_div_text
tmp_text = tmp_text.replace(u' ',u' ')
tmp_text = tmp_text.replace(u'例如',u'範例')
tmp_text = tmp_text.replace(u'例:',u'範例')
tmp_text = tmp_text.replace(u'如:',u'範例')
tmp_text = tmp_text.replace(u'舉例',u'範例')
#tmp_text = tmp_text.replace(u'[',u'(')
#tmp_text = tmp_text.replace(u']',u')')
if len(my_anwser_formated) > 0:
#print("text:" , re.findall('\([\w]+\)', tmp_text))
new_pattern = my_anwser_formated
if len(my_answer_delimitor) > 0:
new_pattern = my_anwser_formated + u'\\' + my_answer_delimitor
return_list = re.findall(new_pattern, my_options)
if not return_list is None:
if len(return_list) == 1:
# re-sample for this case.
return_list = re.findall(my_anwser_formated, my_options)
# try rule8:
if return_list is None:
# need replace to space to get first options.
tmp_text = captcha_text_div_text
tmp_text = tmp_text.replace(u'?',u' ')
tmp_text = tmp_text.replace(u'?',u' ')
tmp_text = tmp_text.replace(u'。',u' ')
delimitor_symbols_left = [u"(",u"[",u"{", " ", " ", " ", " "]
delimitor_symbols_right = [u")",u"]",u"}", ":", ".", ")", "-"]
idx = -1
for idx in range(len(delimitor_symbols_left)):
symbol_left = delimitor_symbols_left[idx]
symbol_right = delimitor_symbols_right[idx]
if symbol_left in tmp_text and symbol_right in tmp_text and u'半形' in tmp_text:
hint_list = re.findall(u'\\'+ symbol_left + u'[\\w]+\\'+ symbol_right , tmp_text)
#print("hint_list:", hint_list)
if not hint_list is None:
if len(hint_list) > 1:
return_list = []
my_answer_delimitor = symbol_right
for options in hint_list:
if len(options) > 2:
my_anwser = options[1:-1]
#print("my_anwser:",my_anwser)
if len(my_anwser) > 0:
return_list.append(my_anwser)
if not return_list is None:
break
#print("return_list:", return_list)
return return_list, my_answer_delimitor
# from detail to game
def tixcraft_redirect(url):
game_name = ""
# get game_name from url
if "https://tixcraft.com/activity/detail/" in url:
url_split = url.split("/")
if len(url_split) >= 6:
game_name = url_split[5]
if url == "https://tixcraft.com/activity/detail/%s" % (game_name,):
entry_url = "https://tixcraft.com/activity/game/%s" % (game_name,)
driver.get(entry_url)
def date_auto_select(url):
game_name = ""
if "https://tixcraft.com/activity/game/" in url:
url_split = url.split("/")
if len(url_split) >= 6:
game_name = url_split[5]
# choose date
if url == "https://tixcraft.com/activity/game/%s" % (game_name,):
if len(date_keyword) == 0:
el = None
if date_auto_select_mode == CONST_FROM_TOP_TO_BOTTOM:
try:
el = driver.find_element(By.CSS_SELECTOR, '.btn-next')
except Exception as exc:
print("find .btn-next fail")
else:
# from down to top
days = None
try:
days = driver.find_elements(By.CSS_SELECTOR, '.btn-next')
if len(days) > 0:
el = days[len(days)-1]
except Exception as exc:
pass
#print("find a tag fail")
if el is not None:
# first date.
try:
el.click()
except Exception as exc:
print("try to click .btn-next fail")
else:
# match keyword.
date_list = None
try:
date_list = driver.find_elements(By.CSS_SELECTOR, '#gameList > table > tbody > tr')
except Exception as exc:
print("find #gameList fail")
if date_list is not None:
match_keyword_row = False
for row in date_list:
row_text = ""
try:
row_text = row.text
except Exception as exc:
print("get text fail")
break
if len(row_text) > 0:
if date_keyword in row_text:
match_keyword_row = True
el = None
try:
el = row.find_element(By.CSS_SELECTOR, '.btn-next')
except Exception as exc:
print("find .btn-next fail")
if el is not None:
# first date.
try:
el.click()
except Exception as exc:
print("try to click .btn-next fail")
if match_keyword_row:
break
# auto refresh for date list page.
el_list = None
try:
el_list = driver.find_elements(By.CSS_SELECTOR, '.btn-next')
if el_list is None:
driver.refresh()
else:
if len(el_list) == 0:
driver.refresh()
except Exception as exc:
pass
#print("find .btn-next fail:", exc)
# PS: auto refresh condition 1: no keyword + no hyperlink.
# PS: auto refresh condition 2: with keyword + no hyperlink.
def area_auto_select(url):
if 'tixcraft.com/ticket/area/' in url:
#driver.switch_to.default_content()
el = None
try:
el = driver.find_element(By.CSS_SELECTOR, '.zone')
except Exception as exc:
print("find .zone fail, do nothing.")
if el is not None:
areas = None
is_need_refresh = False
if len(area_keyword) == 0:
try:
areas = el.find_elements(By.TAG_NAME, "a")
except Exception as exc:
pass
if areas is not None:
if len(areas) == 0:
print("list is empty, do refresh!")
is_need_refresh = True
else:
print("list is None, do refresh!")
is_need_refresh = True
else:
# match keyword.
area_list = None
try:
area_list = el.find_elements(By.TAG_NAME, 'a')
except Exception as exc:
#print("find area list a tag fail")
pass
if area_list is not None:
if len(area_list) == 0:
print("(with keyword) list is empty, do refresh!")
is_need_refresh = True
else:
print("(with keyword) list is None, do refresh!")
is_need_refresh = True
if area_list is not None:
areas = []
for row in area_list:
row_is_enabled=False
try:
row_is_enabled = row.is_enabled()
except Exception as exc:
pass
row_text = ""
if row_is_enabled:
try:
row_text = row.text
except Exception as exc:
print("get text fail")
break
if len(row_text) > 0:
if area_keyword in row_text:
areas.append(row)
if area_auto_select_mode == CONST_FROM_TOP_TO_BOTTOM:
print("only need first item, break area list loop.")
break
#print("row_text:" + row_text)
#print("match:" + area_keyword)
area_target = None
if areas is not None:
#print("area_auto_select_mode", area_auto_select_mode)
#print("len(areas)", len(areas))
if len(areas) > 0:
target_row_index = 0
if area_auto_select_mode == CONST_FROM_TOP_TO_BOTTOM:
pass
if area_auto_select_mode == CONST_FROM_BOTTOM_TO_TOP:
target_row_index = len(areas)-1
if area_auto_select_mode == CONST_RANDOM:
target_row_index = random.randint(0,len(areas)-1)
#print("target_row_index", target_row_index)
area_target = areas[target_row_index]
if area_target is not None:
try:
print("area text", area_target.text)
area_target.click()
except Exception as exc:
print("click area a link fail, start to retry...")
time.sleep(0.2)
try:
area_target.click()
except Exception as exc:
print("click area a link fail, after reftry still fail.")
print(exc)
pass
# auto refresh for area list page.
if is_need_refresh:
try:
driver.refresh()
except Exception as exc:
pass
'''
el_selectSeat_iframe = None
try:
el_selectSeat_iframe = driver.find_element_by_xpath("//iframe[contains(@src,'/ticket/selectSeat/')]")
except Exception as exc:
#print("find seat iframe fail")
pass
if el_selectSeat_iframe is not None:
driver.switch_to.frame(el_selectSeat_iframe)
# click one seat
el_seat = None
try:
el_seat = driver.find_element(By.CSS_SELECTOR, '.empty')
if el_seat is not None:
try:
el_seat.click()
except Exception as exc:
#print("click area button fail")
pass
except Exception as exc:
print("find empty seat fail")
# click submit button
el_confirm_seat = None
try:
el_confirm_seat = driver.find_element(By.ID, 'submitSeat')
if el_confirm_seat is not None:
try:
el_confirm_seat.click()
except Exception as exc:
#print("click area button fail")
pass
except Exception as exc:
print("find submitSeat fail")
'''
def ticket_number_auto_fill(url, form_select):
# check agree
form_checkbox = None
try:
form_checkbox = driver.find_element(By.ID, 'TicketForm_agree')
if form_checkbox is not None:
try:
form_checkbox.click()
except Exception as exc:
print("click TicketForm_agree fail")
pass
except Exception as exc:
print("find TicketForm_agree fail")
# 使用 plan B.
try:
#driver.execute_script("$(\"input[type='checkbox']\").prop('checked', true);")
driver.execute_script("document.getElementById(\"TicketForm_agree\").checked;")
except Exception as exc:
print("javascript check TicketForm_agree fail")
print(exc)
pass
# select options
select = None
try:
#select = driver.find_element(By.TAG_NAME, 'select')
select = Select(form_select)
#select = driver.find_element(By.CSS_SELECTOR, '.mobile-select')
except Exception as exc:
print("select fail")
if select is not None:
try:
# target ticket number
select.select_by_visible_text(ticket_number)
#select.select_by_value(ticket_number)
#select.select_by_index(int(ticket_number))
except Exception as exc:
print("select_by_visible_text ticket_number fail")
print(exc)
try:
# target ticket number
select.select_by_visible_text(ticket_number)
#select.select_by_value(ticket_number)
#select.select_by_index(int(ticket_number))
except Exception as exc:
print("select_by_visible_text ticket_number fail...2")
print(exc)
# try buy one ticket
try:
select.select_by_visible_text("1")
#select.select_by_value("1")
#select.select_by_index(int(ticket_number))
except Exception as exc:
print("select_by_visible_text 1 fail")
pass
# because click cause click wrong row.
if select is not None:
try:
# target ticket number
#select.select_by_visible_text(ticket_number)
print("assign ticker number by jQuery:",ticket_number)
driver.execute_script("$(\"input[type='select']\").val(\""+ ticket_number +"\");")
except Exception as exc:
print("jQuery select_by_visible_text ticket_number fail (after click.)")
print(exc)
# click again.
try:
form_select.click()
except Exception as exc:
print("click select fail")
pass
form_verifyCode = None
try:
form_verifyCode = driver.find_element(By.ID, 'TicketForm_verifyCode')
if form_verifyCode is not None:
try:
form_verifyCode.click()
except Exception as exc:
print("click form_verifyCode fail")
pass
except Exception as exc:
print("find form_verifyCode fail")
def tixcraft_verify(url):
ret = False
captcha_password_string = None
form_select = None
try:
form_select = driver.find_element(By.CSS_SELECTOR, '.zone-verify')
if form_select is not None:
html_text = ""
try:
html_text = form_select.text
html_text = html_text.replace(u'「',u'【')
html_text = html_text.replace(u'〔',u'【')
html_text = html_text.replace(u'[',u'【')
html_text = html_text.replace(u'〖',u'【')
html_text = html_text.replace(u'[',u'【')
html_text = html_text.replace(u'」',u'】')
html_text = html_text.replace(u'〕',u'】')
html_text = html_text.replace(u']',u'】')
html_text = html_text.replace(u'〗',u'】')
html_text = html_text.replace(u']',u'】')
#print("html_text:", html_text)
if u'【' in html_text and u'】' in html_text:
#captcha_password_string = find_between(html_text, u"【", u"】")
pass
except Exception as exc:
print("get text fail")
except Exception as exc:
print("find verify fail")
pass
if not captcha_password_string is None:
form_input = None
try:
form_input = driver.find_element(By.CSS_SELECTOR, '#checkCode')
if form_input is not None:
default_value = form_input.get_attribute('value')
if not default_value is None:
if len(default_value) == 0:
form_input.send_keys(captcha_password_string)
print("send captcha keys:" + captcha_password_string)
time.sleep(0.2)
ret = True
else:
print("find captcha input field fail")
except Exception as exc:
print("find verify fail")
pass
if ret:
# retry
for i in range(5):
form_input = None
try:
form_input = driver.find_element(By.CSS_SELECTOR, '#submitButton')
if form_input is not None:
if form_input.is_enabled():
form_input.click()
break