1+ from ...storage .storage import find_many , insert_many , delete_many
2+ from ...ticketscraping import constants
3+ from ..models .pick import Pick
4+
5+ def generate_picks_set_from_picks (picks ):
6+ def __helper (pick : dict ):
7+ return Pick (_id = pick .get ('_id' ),
8+ scraping_id = pick .get ('scraping_id' ),
9+ type = pick ['type' ],
10+ selection = pick ['selection' ],
11+ quality = pick ['quality' ],
12+ section = pick ['section' ],
13+ row = pick ['row' ],
14+ area = pick ['area' ],
15+ maxQuantity = pick ['maxQuantity' ],
16+ offer = pick ['offer' ],
17+ seat_columns = pick ['seat_columns' ])
18+
19+ if type (picks ) is dict :
20+ return set (map (__helper , picks ['picks' ]))
21+ elif type (picks ) is list :
22+ return set (map (__helper , picks ))
23+ else :
24+ raise Exception ('argument type error' )
25+
26+ def get_current_best_available (scraping_id : str ):
27+ return find_many (constants .DATABASE ['BEST_AVAILABLE_SEATS' ], {"scraping_id" : scraping_id })
28+ def remove_best_seats (seats : set [Pick ]):
29+ ids = []
30+ for seat in seats :
31+ ids .append (seat ._id )
32+ return delete_many (constants .DATABASE ['BEST_AVAILABLE_SEATS' ], {"_id" : {"$in" : ids }})
33+ def insert_best_seats (seats : set [Pick ], scraping_id : str ):
34+ for seat in seats :
35+ seat .setScrapingId (scraping_id )
36+ return insert_many (constants .DATABASE ['BEST_AVAILABLE_SEATS' ], list (map (lambda seat : vars (seat ), seats )))
37+ def insert_history_seats (seats : set [Pick ]):
38+ return insert_many (constants .DATABASE ['BEST_HISTORY_SEATS' ], list (map (lambda seat : vars (seat ), seats )))
39+
40+
41+
42+ def run_periodic_task (picks : dict , scraping_id : str ):
43+ # B the list of new best available seats
44+ new_best_avail = generate_picks_set_from_picks (picks )
45+ # A be the list of current best available seats
46+ cur_best_avail = generate_picks_set_from_picks (get_current_best_available (scraping_id ))
47+
48+ # Compute C := A-B which is the seats
49+ overwritten_seats = cur_best_avail - new_best_avail
50+
51+ # Compute D := B-A which is the new seats
52+ new_seats = new_best_avail - cur_best_avail
53+
54+ print (f"size of B is { len (new_best_avail )} " )
55+ print (f"size of A is { len (cur_best_avail )} " )
56+ print (f"size of C is { len (overwritten_seats )} " )
57+ print (f"size of D is { len (new_seats )} " )
58+
59+ # Remove C from best_available_seats
60+ remove_best_seats (overwritten_seats )
61+
62+ # Insert D to best_available_seats
63+ insert_best_seats (new_seats , scraping_id )
64+
65+ # Save C to best_history_seats.
66+ insert_history_seats (overwritten_seats )
67+
68+ # TODO
69+ # Use D to invoke a handler to analyze them against the best_history_seats asynchronously.
70+
71+ pass
0 commit comments