33import json
44import threading
55import time
6- import random
76import secrets
87import string
98import hashlib
10- import timentp
9+ import ntplib
10+ import configparser
11+ import os
1112# 定义一个全局锁用于线程同步
1213thread_dict = {}
1314cookie_file_path = 'cookie.txt'
1415config_file_path = 'config.txt'
15- num_threads_per_ticket = 3
1616headers = {
1717 'authority' : 'www.allcpp.cn' ,
1818 'accept' : 'application/json, text/plain, */*' ,
2929 'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36' ,
3030 }
3131
32+ # 读取配置文件
33+ def getConfig (filename , section , option ):
34+ conf = configparser .ConfigParser ()
35+ conf .read (filename )
36+ config = conf .get (section , option )
37+ return config
38+
39+ def timeconvey (ntp ):
40+ chec = ntplib .NTPClient ()
41+ response = chec .request (ntp )
42+ timestamp = response .tx_time
43+ timestamp_local = time .time ()
44+ #print(timestamp_local)
45+ #print(timestamp)
46+ differ = timestamp - timestamp_local
47+ return differ
48+
3249def sign_for_post (ticketid ):
3350 timestamp = str (int (time .time ())) ##"1682074579"
3451 ## 貌似并不校验 sign: a()(t + r + i + e + n)
@@ -183,13 +200,13 @@ def process_thread(ticketid,cookie_str):
183200 thread_to_close .join ()
184201 return True
185202 else :
186- with open (f"output_ticket_{ ticketid } _{ ids_str } _attempt_ { i } .txt" , "a" ) as output_file :
203+ with open (f"output_ticket_{ ticketid } _{ ids_str } _attempt .txt" , "a" ) as output_file :
187204 output_file .write (resp )
188- url_wx = 'https://www.allcpp.cn/allcpp/ticket/buyTicketWeixin .do?ticketTypeId=' + str (ticketid ) + '&count=' + str (
189- id_count ) + '&' + retn_params + '&payType=0' + '&purchaserIds=' + ids_str
190- print (url_wx )
205+ url = 'https://www.allcpp.cn/allcpp/ticket/buyTicketAliWapPay .do?ticketTypeId=' + str (ticketid ) + '&count=' + str (
206+ id_count ) + '&' + retn_params + '&purchaserIds=' + ids_str
207+ print (url )
191208 response = requests .post (
192- url = url_wx ,
209+ url = url ,
193210 cookies = cookies ,
194211 headers = headers ,
195212 json = json_data ,
@@ -200,20 +217,18 @@ def process_thread(ticketid,cookie_str):
200217 if is_success == True :
201218 i = 3
202219 print (f"Thread for ticket { ticketid } succeeded" )
203- with open (f"output_ticket_{ ticketid } _{ ids_str } _wx .txt" , "a" ) as output_file :
220+ with open (f"output_ticket_{ ticketid } _{ ids_str } .txt" , "a" ) as output_file :
204221 output_file .write (resp )
205222 threads_to_close = [thread for thread in threads if thread ._target == process_thread and thread ._args [0 ] == ticketid and thread ._args [1 ] == cookies ]
206223 for thread_to_close in threads_to_close [:2 ]: # 关闭同类型的前两个线程
207224 thread_to_close .join ()
208225 return True
209226 else :
210- with open (f"output_ticket_{ ticketid } _{ ids_str } _wx_attempt_ { i } .txt" , "a" ) as output_file :
227+ with open (f"output_ticket_{ ticketid } _{ ids_str } _attempt .txt" , "a" ) as output_file :
211228 output_file .write (resp )
212229 print (resp )
213230 print (type (resp ))
214- t = random .random ()
215- print (t )
216- time .sleep (t )
231+ time .sleep (sleep_time )
217232
218233
219234
@@ -222,10 +237,11 @@ def start(cookies, ticket_ids):
222237
223238 for i in range (len (cookies )):
224239 for j in range (len (ticket_ids [i ])):
225- ticket = ticket_ids [i ][j ]
226- cook = cookies [i ]
227- thread = threading .Thread (target = process_thread , args = (ticket , cook ))
228- thread .start ()
240+ for _ in range (num_thread ):
241+ ticket = ticket_ids [i ][j ]
242+ cook = cookies [i ]
243+ thread = threading .Thread (target = process_thread , args = (ticket , cook ))
244+ thread .start ()
229245
230246
231247
@@ -247,7 +263,13 @@ def delayed_execution():
247263
248264
249265def main ():
250- differ = timentp .timeconvey ()
266+ ntp = getConfig ("config.txt" , 'time' , 'ntp' )
267+ global sleep_time
268+ sleep_time = float (getConfig ("config.txt" , 'ticket' , 'sleep' ))
269+ global num_thread
270+ num_thread = int (getConfig ("config.txt" , 'ticket' , 'num_thread' ))
271+
272+ differ = timeconvey (ntp )
251273 if differ > 0 :
252274 print (f"\033 [1;31;47m主人你的时间慢了{ abs (differ )} 秒\033 [0m" )
253275 print ("\033 [1;31;47m主人你的时间滞后了哦,请同步时间\033 [0m" )
@@ -261,17 +283,16 @@ def main():
261283 print (ifn )
262284 print (ticket_ids [i ])
263285 i += 1
264- if input ('T or F \n ' ) == 'T' :
286+ if input ('正确(回复T) or 错误(回复F) \n ' ) == 'T' :
265287 print ("1.定时 2.捡漏\n " )
266288 if input ()== '1' :
267- target_timestamp_ms = int (input ('输入开始时间戳:' ))
289+ target_timestamp_ms = int (input ('输入开始时间戳(毫秒) :' ))
268290 schedule_script_at_timestamp (target_timestamp_ms ,cookies , ticket_ids )
269291 else :
270292 start (cookies , ticket_ids )
271293 else :
272294 exit
273295
274- print ("主人好了哦" )
275296
276297if __name__ == "__main__" :
277298 main ()
0 commit comments