def load_tournament_file(): path = os.environ['APP_PATH'] with open(path + '/src/jsons/tournaments.json') as f: data = json.load(f) # casino cache so not to request for same casinos path_cache = os.environ['APP_PATH'] + '/src/jsons/casinos.json' if os.path.exists(path_cache): with open(path_cache) as f: cache = json.load(f) else: cache = {} for r in data: # Do not add these to Swap Profit if r['Tournament'].strip() == '' or \ 'satelite' in r['Tournament'].lower() or \ r['Results Link'] == False: continue trmnt = Tournaments.query.get(r['Tournament ID']) trmnt_name, flight_day = utils.resolve_name_day(r['Tournament']) start_at = datetime.strptime(r['Date'][:10] + r['Time'], '%Y-%m-%d%H:%M:%S') trmntjson = { 'id': r['Tournament ID'], 'name': trmnt_name, 'start_at': start_at, 'results_link': str(r['Results Link']).strip() } flightjson = {'start_at': start_at, 'day': flight_day} if trmnt is None: casino = cache.get(r['Casino ID']) trmntjson = { **trmntjson, 'address': casino['address'].strip(), 'city': casino['city'].strip(), 'state': casino['state'].strip(), 'zip_code': str(casino['zip_code']).strip(), 'longitude': float(casino['longitude']), 'latitude': float(casino['latitude']) } # Create tournament trmnt = Tournaments(**trmntjson) db.session.add(trmnt) db.session.flush() # Create flight db.session.add(Flights(tournament_id=trmnt.id, **flightjson)) else: # Create flight db.session.add(Flights(tournament_id=trmnt.id, **flightjson)) db.session.commit() return True
def load_tournament_file(): path = os.environ['APP_PATH'] with open( path + '/src/jsons/tournaments.json' ) as f: data = json.load( f ) # casino cache so not to request for same casinos path_cache = os.environ['APP_PATH'] + '/src/jsons/casinos.json' if os.path.exists( path_cache ): with open( path_cache ) as f: cache = json.load(f) else: cache = {} for r in data: # Do not add these to Swap Profit if r['Tournament'].strip() == '' or \ 'satelite' in r['Tournament'].lower() or \ r['Results Link'] == False: continue trmnt = Tournaments.query.get( r['Tournament ID'] ) trmnt_name, flight_day = utils.resolve_name_day( r['Tournament'] ) start_at = datetime.strptime( r['Date'][:10] + r['Time'], '%Y-%m-%d%H:%M:%S' ) trmntjson = { 'id': r['Tournament ID'], 'name': trmnt_name, 'start_at': start_at, 'results_link': str( r['Results Link'] ).strip() } flightjson = { 'start_at':start_at, 'day': flight_day } if trmnt is None: casino = cache.get( r['Casino ID'] ) print("THIS CASINO", f'{casino}') trmntjson = { **trmntjson, 'casino': casino['name'], 'address': casino['address'].strip(), 'city': casino['city'].strip(), 'state': casino['state'].strip(), 'zip_code': str( casino['zip_code'] ).strip(), 'longitude': float( casino['longitude'] ), 'latitude': float( casino['latitude'] ) } # Create tournament trmnt = Tournaments( **trmntjson ) db.session.add( trmnt ) db.session.flush() # Create flight db.session.add( Flights( tournament_id=trmnt.id, **flightjson )) else: # Create flight db.session.add( Flights( tournament_id=trmnt.id, **flightjson )) db.session.commit() return True ''' { "api_token": 1 "tournament_id": 45, "tournament_buyin": 150, "users": { "*****@*****.**": { "place": 11, "winnings": 200 } } } ''' trmnt_data = {} print('hello') for index, r in df.iterrows(): # print('r', r) # Get the trmnt data that's in the first row if index == 0: # Check trmnt existance trmnt = Tournaments.query.get( r['Tournament ID'] ) print('trmnt.buy_in', trmnt.buy_in) if trmnt is None: return None, { 'error':'This tournament ID was not found: '+ str(r['Tournament ID']) } print('tournament', trmnt) trmnt.results_link = (os.environ['API_HOST'] + '/results/tournament/' + str(r['Tournament ID']) ) # Check to see if file was uploaded already entry = Results.query.filter_by( tournament_id = r['Tournament ID'] ).first() if entry is not None: return None, { 'error':'This tournament ID has already been uploaded: '+ str(trmnt.id) } # Swap Profit JSON trmnt_data = { 'api_token': utils.sha256( os.environ['API_TOKEN'] ), 'tournament_id': trmnt.id, 'tournament_buyin': trmnt.buy_in, 'users': {} } user_id = r['User ID'] or None # Add user to the Swap Profit JSON if user_id: user = Users.query.get( user_id ) if user is None: db.session.rollback() return None, { 'error':'Couldn\'t find user with ID: '+ str(user_id) } # Swap Profit JSON trmnt_data['users'][user.email] = { 'place': r['Place'], 'winnings': r['Winnings'] # 'user_id': user.id } # Add to PokerSociety database db.session.add( Results( tournament_id = trmnt_data['tournament_id'], user_id = user_id, full_name = r['Full Name'], place = r['Place'], nationality = r['Nationality'], winnings = r['Winnings'] )) # If no errors, commit all data db.session.commit() # swapprofit = Subscribers.query.filter_by(company_name='Swap Profit').first() # if swapprofit is None: # return 'Swap Profit not a subscriber' # resp = requests.post( # os.environ['SWAPPROFIT_API_HOST'] + '/results/update', # json=trmnt_data ) # print('resp', resp) return trmnt_data, { 'message': 'Results excel processed successfully' }
def add_tournaments(): # casino cache so not to request for same casinos path_cache = os.environ['APP_PATH'] + '/src/files/tournaments.json' if os.path.exists(path_cache): with open(path_cache) as f: cache = json.load(f) else: cache = {} # data comes in as a string data = json.loads(request.get_json()) for r in data: # Do not add these to Swap Profit if r['Tournament'].strip() == '' or \ 'satelite' in r['Tournament'].lower() or \ r['Results Link'] == False: continue trmnt = Tournaments.query.get(r['Tournament ID']) trmnt_name, flight_day = utils.resolve_name_day(r['Tournament']) start_at = datetime.strptime(r['Date'][:10] + r['Time'], '%Y-%m-%d%H:%M:%S') trmntjson = { 'id': r['Tournament ID'], 'name': trmnt_name, 'start_at': start_at, 'results_link': str(r['Results Link']).strip() } flightjson = {'start_at': start_at, 'day': flight_day} if trmnt is None: casino = cache.get(r['Casino ID']) if casino is None: rsp = requests.get( f"{os.environ['POKERSOCIETY_HOST']}/casinos/{r['Casino ID']}" ) if not rsp.ok: raise APIException( f'Casino with id "{r["Casino ID"]}" not found', 404) casino = rsp.json() cache[r['Casino ID']] = casino trmntjson = { **trmntjson, 'address': casino['address'].strip, 'city': casino['city'].strip, 'state': casino['state'].strip, 'zip_code': str(casino['zip_code']).strip, 'longitude': float(casino['longitude']), 'latitude': float(casino['latitude']) } # Create tournament trmnt = Tournaments(**trmntjson) db.session.add(trmnt) db.session.flush() # Create flight db.session.add(Flights(tournament_id=trmnt.id, **flightjson)) else: # Update tournament for db_col, val in trmntjson.items(): if getattr(trmnt, db_col) != val: setattr(trmnt, db_col, val) flight = Flights.query.filter_by( tournament_id=trmnt.id ) \ .filter( or_( Flights.day == flight_day, Flights.start_at == start_at )) \ .first() # Create flight if flight is None: db.session.add( Flights(tournament_id=trmnt.id, **flightjson)) # Update flight else: for db_col, val in flightjson.items(): if getattr(flight, db_col) != val: setattr(flight, db_col, val) db.session.commit() # Save cache if cache != {}: with open(path_cache, 'w') as f: json.dump(cache, f, indent=2) return jsonify({'message': 'Tournaments have been updated'}), 200