33from ..ticketscraping import constants
44
55
6- def store_seats (data , scheduler_config ):
6+ def store_seats (data , subscriber_id ):
77 # prune top-picks data structure
88 pruned_picks = prune_pick_attributes (data )
99
@@ -12,7 +12,7 @@ def store_seats(data, scheduler_config):
1212 append_scraping_config_ref ,
1313 map_prices_to_seats ,
1414 remove_embedded_field
15- ], pruned_picks , scheduler_config )
15+ ], pruned_picks , subscriber_id )
1616
1717 # store in db
1818 # print(res)
@@ -28,79 +28,119 @@ def pipe(fns: list, *args):
2828 out = fn (out )
2929 return out
3030
31+ def get_value_from_map (map : dict , * args , ** kwargs ):
32+ # input validation
33+ if type (map ) is not dict :
34+ return kwargs .get ('default' , None )
35+ res = kwargs .get ('default' , None )
36+ for attr in args :
37+ res = map .get (attr )
38+ if res is not None :
39+ break
40+ return res
41+
42+ def get_value_from_nested_map (map : dict , * args , ** kwargs ):
43+ # input validation
44+ if type (map ) is not dict :
45+ return kwargs .get ('default' , None )
46+ res = None
47+ m = map
48+ count = 0
49+ for attr in args :
50+ res = m .get (attr )
51+ count += 1
52+ if res is None :
53+ break
54+ elif type (res ) is dict :
55+ m = res
56+ else :
57+ break
58+ return res if res is not None and count == len (args ) else kwargs .get ('default' , None )
59+
60+ def get_fn_return (fn , * args , ** kwargs ):
61+ res = kwargs .get ('default' , None )
62+ try :
63+ res = fn (* args )
64+ except :
65+ pass
66+ finally :
67+ return res
3168
3269def prune_pick_attributes (data ):
33- def prune_pick_offer_attributes (pick ):
70+ def prune_pick_offer_attributes (pick : dict ):
3471 return {
35- 'type' : pick [ 'type' ] ,
36- 'selection' : pick [ 'selection' ] ,
37- 'quality' : pick [ 'quality' ] ,
38- 'section' : pick [ 'section' ] ,
39- 'row' : pick [ 'row' ] ,
40- 'offerGroups' : pick [ 'offerGroups' ] ,
41- 'area' : pick [ 'area' ] ,
42- 'maxQuantity' : pick [ 'maxQuantity' ] ,
72+ 'type' : get_value_from_map ( pick , 'type' ) ,
73+ 'selection' : get_value_from_map ( pick , 'selection' ) ,
74+ 'quality' : get_value_from_map ( pick , 'quality' ) ,
75+ 'section' : get_value_from_map ( pick , 'section' ) ,
76+ 'row' : get_value_from_map ( pick , 'row' ) ,
77+ 'offerGroups' : get_value_from_map ( pick , 'offerGroups' , 'offers' ) ,
78+ 'area' : get_value_from_map ( pick , 'area' ) ,
79+ 'maxQuantity' : get_value_from_map ( pick , 'maxQuantity' ) ,
4380 }
4481
45- def prune_pick_embedded_attributes (embedded ):
82+ def prune_pick_embedded_attributes (embedded : dict ):
4683 def prune_pick_embedded_offer_attributes (item ):
4784 return {
48- 'expired_date' : parser .parse (item [ 'meta' ][ 'expires' ] ),
49- 'offerId' : item [ 'offerId' ] ,
50- 'rank' : item [ 'rank' ] ,
51- 'online' : item [ 'online' ] ,
52- 'protected' : item [ 'protected' ] ,
53- 'rollup' : item [ 'rollup' ] ,
54- 'inventoryType' : item [ 'inventoryType' ] ,
55- 'offerType' : item [ 'offerType' ] ,
56- 'currency' : item [ 'currency' ] ,
57- 'listPrice' : item [ 'listPrice' ] ,
58- 'faceValue' : item [ 'faceValue' ] ,
59- 'totalPrice' : item [ 'totalPrice' ] ,
60- 'noChargesPrice' : item [ 'noChargesPrice' ] ,
61- # 'listingId': item[ 'listingId'] ,
62- # 'listingVersionId': item[ 'listingVersionId'] ,
63- # 'charges': item[ 'charges'] ,
64- # 'sellableQuantities': item[ 'sellableQuantities'] ,
65- # 'section': item[ 'section'] ,
66- # 'row': item[ 'row'] ,
67- # 'seatFrom': item[ 'seatFrom'] ,
68- # 'seatTo': item[ 'seatTo'] ,
69- # 'ticketTypeId': item[ 'ticketTypeId']
85+ 'expired_date' : get_fn_return ( parser .parse , get_value_from_nested_map (item , 'meta' , 'expires' ), default = None ),
86+ 'offerId' : get_value_from_map ( item , 'offerId' ) ,
87+ 'rank' : get_value_from_map ( item , 'rank' ) ,
88+ 'online' : get_value_from_map ( item , 'online' ) ,
89+ 'protected' : get_value_from_map ( item , 'protected' ) ,
90+ 'rollup' : get_value_from_map ( item , 'rollup' ) ,
91+ 'inventoryType' : get_value_from_map ( item , 'inventoryType' ) ,
92+ 'offerType' : get_value_from_map ( item , 'offerType' ) ,
93+ 'currency' : get_value_from_map ( item , 'currency' ) ,
94+ 'listPrice' : get_value_from_map ( item , 'listPrice' ) ,
95+ 'faceValue' : get_value_from_map ( item , 'faceValue' ) ,
96+ 'totalPrice' : get_value_from_map ( item , 'totalPrice' ) ,
97+ 'noChargesPrice' : get_value_from_map ( item , 'noChargesPrice' ) ,
98+ # 'listingId': get_value_from_map( item, 'listingId') ,
99+ # 'listingVersionId': get_value_from_map( item, 'listingVersionId') ,
100+ # 'charges': get_value_from_map( item, 'charges') ,
101+ # 'sellableQuantities': get_value_from_map( item, 'sellableQuantities') ,
102+ # 'section': get_value_from_map( item, 'section') ,
103+ # 'row': get_value_from_map( item, 'row') ,
104+ # 'seatFrom': get_value_from_map( item, 'seatFrom') ,
105+ # 'seatTo': get_value_from_map( item, 'seatTo') ,
106+ # 'ticketTypeId': get_value_from_map( item, 'ticketTypeId')
70107 }
71108 return {
72- 'offer' : list (map (prune_pick_embedded_offer_attributes , embedded [ 'offer' ] ))
109+ 'offer' : list (map (prune_pick_embedded_offer_attributes , get_value_from_map ( embedded , 'offer' , default = dict ()) ))
73110 }
74111 return {
75- 'expired_date' : parser .parse (data [ 'meta' ][ 'expires' ] ),
76- 'eventId' : data [ 'eventId' ] ,
77- 'offset' : data [ 'offset' ] ,
78- 'total' : data [ 'total' ] ,
79- 'picks' : list (map (prune_pick_offer_attributes , data [ 'picks' ] )),
80- '_embedded' : prune_pick_embedded_attributes (data [ '_embedded' ] )
112+ 'expired_date' : get_fn_return ( parser .parse , get_value_from_nested_map (data , 'meta' , 'expires' ), default = None ),
113+ 'eventId' : get_value_from_map ( data , 'eventId' ) ,
114+ 'offset' : get_value_from_map ( data , 'offset' ) ,
115+ 'total' : get_value_from_map ( data , 'total' ) ,
116+ 'picks' : list (map (prune_pick_offer_attributes , get_value_from_map ( data , 'picks' , default = dict ()) )),
117+ '_embedded' : prune_pick_embedded_attributes (get_value_from_map ( data , '_embedded' , default = dict ()) )
81118 }
82119
83120
84- def append_scraping_config_ref (data , scheduler_config ):
85- data ['scraping_config_ref' ] = scheduler_config
121+ def append_scraping_config_ref (data , config_id ):
122+ data ['scraping_config_ref' ] = config_id
86123 return data
87124
88125
89126def map_prices_to_seats (data ):
90127 def map_prices_to_seat_helper (offer_table : dict ):
91128 def __map_prices_to_seat_helper (pick ):
92129 offerGroups = pick ['offerGroups' ]
130+ if offerGroups is None or len (offerGroups ) == 0 :
131+ return {'offer_available' : False }
93132 offerGroup = offerGroups [0 ]
94- offerIds = offerGroup [ 'offers' ]
95- offerSeatCols = offerGroup [ 'seats' ]
96- if len (offerGroups ) == 0 or len ( offerIds ) == 0 :
133+ offerIds = get_value_from_map ( offerGroup , 'offers' , default = [ offerGroup ])
134+ offerSeatCols = get_value_from_map ( offerGroup , 'seats' )
135+ if len (offerIds ) == 0 :
97136 return {'offer_available' : False }
98137 offerId = offerIds [0 ]
99138 offerObj = offer_table .get (offerId )
100139 res = {** pick , 'offer' : offerObj , 'seat_columns' : offerSeatCols }
101140 del res ['offerGroups' ]
102141 return res
103142 return __map_prices_to_seat_helper
143+
104144 offer_dict = {offer ['offerId' ]: offer for offer in data ['_embedded' ]['offer' ]}
105145 picks_list = list (
106146 map (map_prices_to_seat_helper (offer_dict ), data ['picks' ]))
0 commit comments