def _dump_it(g_result_dic_, filename='result_finetune.json', limit=20): """ dump and shrink result dictionary """ ele_list = [ 'home_correct', 'visitor_correct', 'winloss_correct', 'result_correct' ] # new_result_dic = {'home_correct': {}, 'visitor_correct': {}, 'winloss_correct': {}, 'result_correct': {}} new_result_dic = {} for ele in ele_list: new_result_dic[ele] = [] cnt = 0 # pylint: disable=W0640 for result in sorted( g_result_dic_[ele], key=lambda i: (i[ele], i['result_correct'], i['winloss_correct'], i['g_cnt']), reverse=True): cnt += 1 new_result_dic[ele].append(result) if cnt >= limit: break # dump dictionary to store data # print('########## dump-disabled ##############') json_store(filename, new_result_dic) return new_result_dic
SAVE_LIST = [] for tweet in sorted(TWEET_LIST, key=lambda i: i['uts'], reverse=False): # check if message is within buffer if match_uts - BUFFER <= tweet['uts'] and tweet[ 'uts'] <= match_uts + BUFFER: # skip tweets from tippspiel if '#bot1337' not in tweet['text_raw']: # push tweet to dictionalry in case we have to sore it if SAVE_DIR: SAVE_LIST.append(tweet) data_dic = { 'match_id': match_id, 'source': 'twitter', 'name': tweet['name'], 'name_alternate': tweet['screen_name'], 'identifier': tweet['id'], 'created_at': tweet['created_at'], 'created_uts': tweet['uts'], 'text_raw': tweet['text_raw'], 'tag': tweet['hashtag'] } socialnetworkevent_add(LOGGER, 'identifier', tweet['id'], data_dic) # store tweet_list if SAVE_DIR: MATCH_DIR = '{0}/matches/{1}'.format(SAVE_DIR, match_id) path_check_create(LOGGER, MATCH_DIR) json_store('{0}/{1}'.format(MATCH_DIR, 'twitter.json'), SAVE_LIST)
# DEBUG mode on/off DEBUG = True # get season_id to analyse SEASON_ID = 1 # initialize logger LOGGER = logger_setup(DEBUG) # get list of shots from 2019/2020 season - they will be used as base for our model # MATCH_LIST = match_list_get(LOGGER, 'season_id', SEASON_ID, ['match_id']) VLIST = [ 'shot_id', 'match_id', 'match_shot_resutl_id', 'player_id', 'team_id', 'coordinate_x', 'coordinate_y', 'match__home_team_id', 'match__visitor_team_id', 'timestamp' ] SHOT_LIST = shot_list_get(LOGGER, 'match__season_id', SEASON_ID, VLIST) # get player_dictionary to determine left/right hand player PLAYER_DIC = player_dic_get(LOGGER) # build the model MODEL_DIC = xgmodel_build(LOGGER, SHOT_LIST, PLAYER_DIC, REBOUND_INTERVAL, BREAK_INTERVAL) # add percentage values to model MODEL_DIC = pctg_calculate(MODEL_DIC) # result = xgmodel_add(LOGGER, 'id', 1, {'xg_data': MODEL_DIC}) # print(result) json_store('model_data.json', MODEL_DIC)
# check full score if xgf_dic['home'] == g_goal_dic[match_id]['home'] and xgf_dic['visitor'] == g_goal_dic[match_id]['visitor']: RESULT_CORRECT += 1 # check for winloss if xgf_dic['home'] < xgf_dic['visitor'] and g_goal_dic[match_id]['home'] < g_goal_dic[match_id]['visitor']: WINLOSS_CORRECT += 1 elif xgf_dic['home'] > xgf_dic['visitor'] and g_goal_dic[match_id]['home'] > g_goal_dic[match_id]['visitor']: WINLOSS_CORRECT += 1 elif xgf_dic['home'] == xgf_dic['visitor'] and g_goal_dic[match_id]['home'] == g_goal_dic[match_id]['visitor']: WINLOSS_CORRECT += 1 tmp_dic = {'g_cnt': G_CNT, 'match_cnt': MATCH_CNT, 'home_correct': HOME_CORRECT, 'visitor_correct': VISITOR_CORRECT, 'winloss_correct': WINLOSS_CORRECT, 'result_correct': RESULT_CORRECT, 'quantifier_dic': quantifier_dic} # store results in different trees g_result_dic['home_correct'].append(tmp_dic) g_result_dic['visitor_correct'].append(tmp_dic) g_result_dic['winloss_correct'].append(tmp_dic) g_result_dic['result_correct'].append(tmp_dic) print('{0}/{4}: dump shots_pctg: {1}, handness_pctg: {2}, handness_shots_pctg: {3}'.format(uts_now()-uts_start, round(shots_pctg, 1), round(handness_pctg, 1), round(handness_shots_pctg, 1), G_CNT)) # dump here g_result_dic = dump_it(g_result_dic, RESULT_FILE) # store quantifier dic json_store(PARAMETER_FILE, quantifier_dic['home']) # print status print('g_cnt', G_CNT, 'match_cnt', MATCH_CNT, 'home_correct', HOME_CORRECT, 'visitor_correct', VISITOR_CORRECT, 'winloss_correct', WINLOSS_CORRECT, 'result_correct', RESULT_CORRECT) # dump with uts at the end uts = uts_now() g_result_dic = dump_it(g_result_dic, 'result_handness_{0}.json'.format(uts))
shift_dic = del_app_helper.shifts_get(match_id) shift_add(LOGGER, 'match_id', match_id, {'match_id': match_id, 'shift': shift_dic}) try: # delete shots for the match to cope with renumbering (rember EBBvBHV in 12/20) shot_delete(LOGGER, 'match_id', match_id) # get shots shots_dic = del_app_helper.shots_get(match_id) shots_process(LOGGER, shots_dic['match']) except BaseException: LOGGER.debug('ERROR: shots_get() failed.') if SAVE_DIR: MATCH_DIR = '{0}/matches/{1}'.format(SAVE_DIR, match_id) _path_check_create(LOGGER, MATCH_DIR) json_store('{0}/{1}'.format(MATCH_DIR, 'game-header.json'), gameheader_dic) json_store('{0}/{1}'.format(MATCH_DIR, 'player-stats-home.json'), home_dic) json_store('{0}/{1}'.format(MATCH_DIR, 'player-stats-guest.json'), visitor_dic) json_store('{0}/{1}'.format(MATCH_DIR, 'period-events.json'), event_dic) json_store('{0}/{1}'.format(MATCH_DIR, 'roster.json'), roster_dic) json_store('{0}/{1}'.format(MATCH_DIR, 'team-stats-home.json'), thome_dic) json_store('{0}/{1}'.format(MATCH_DIR, 'team-stats-guest.json'), tvisitor_dic) json_store('{0}/{1}'.format(MATCH_DIR, 'shots.json'), shots_dic) if ADDSHIFTS: json_store('{0}/{1}'.format(MATCH_DIR, 'shifts.json'), shift_dic) if HGS_DATA and not os.path.exists('{0}/shifts/2020/1/{1}.json'.format(HGS_DATA, match_id)): LOGGER.debug('fetch shifts') json_store('{0}/shifts/2020/1/{1}.json'.format(HGS_DATA, match_id), shift_dic) if GITREPO and SAVE: # check changes into repo
(DEBUG, TOKEN, FILENAME) = arg_parse() # initialize logger LOGGER = logger_setup(DEBUG) # load rebound and break interval from config file (APP_NAME, APP_ID, APP_SECRET) = _config_load(LOGGER) # unix timestamp UTS = uts_now() URL = "https://graph.facebook.com/v10.0/oauth/access_token" data_dic = { 'grant_type': 'fb_exchange_token', 'client_id': APP_ID, 'client_secret': APP_SECRET, 'fb_exchange_token': TOKEN } try: req = requests.get(URL, params=data_dic) result = req.json() # add current uts and date result['uts'] = UTS result['DATE_HUMAN'] = uts_to_date_utc(UTS) # store result json_store(FILENAME, result) except BaseException as err_: print(err_)