forked from oRastor/offvariance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
131 lines (101 loc) · 5.19 KB
/
service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import pandas
import telebot
from xgclient.client import create_fixtures_dataframe, create_events_dataframe
import os
from dotenv import dotenv_values
import sys
import settings
from modules import notification
from modules.aggregate import aggregate_season, populate_metrics
from modules.factory import create_client
from modules.storage import get_season_fixtures_path, get_season_events_path, update_final_storage, \
get_final_storage, get_upcoming_path, get_finished_path, get_fixtures_odds
from modules.strategy import find_games_by_strategies, find_new_games_by_strategies
from settings import game_aggregations_metrics, season_aggregations_metrics
config = dotenv_values('.env')
telegram_user_id = config.get('TELEGRAM_USER_ID')
client = create_client(config)
bot = telebot.TeleBot(config.get('TELEGRAM_API_KEY'), parse_mode=None)
if len(sys.argv) < 2:
print('command expected')
exit(1)
command = sys.argv[1]
if command == 'download':
for country in client.countries():
for tournament in client.tournaments(country['id']):
for season in client.seasons(tournament['id']):
print(country['name'], tournament['name'], season['name'], '#' + str(season['id']))
fixtures_file_path = get_season_fixtures_path(season['id'])
events_file_path = get_season_events_path(season['id'])
season_fixtures = client.fixtures(season['id'])
fixtures_df = create_fixtures_dataframe(season_fixtures)
if os.path.exists(fixtures_file_path):
available_fixtures_df = pandas.read_csv(fixtures_file_path)
if fixtures_df['update_time'].max() == available_fixtures_df['update_time'].max():
print('Season database not updated')
continue
events_df = create_events_dataframe(season_fixtures)
if fixtures_df.size > 0:
fixtures_df.to_csv(fixtures_file_path, index=False)
if events_df.size > 0:
events_df.to_csv(events_file_path, index=False)
print('Season database updated')
elif command == 'aggregate-seasons':
for country in client.countries():
for tournament in client.tournaments(country['id']):
for season in client.seasons(tournament['id']):
print(country['name'], tournament['name'], season['name'], '#' + str(season['id']))
fixtures_file_path = get_season_fixtures_path(season['id'])
events_file_path = get_season_events_path(season['id'])
if not os.path.exists(fixtures_file_path) or not os.path.exists(events_file_path):
print('Data not exists')
continue
aggregate_season(season['id'], game_aggregations_metrics, season_aggregations_metrics)
print('Season aggregation updated')
elif command == 'aggregate-season':
if len(sys.argv) < 3:
print('Season id expected')
exit(1)
aggregate_season(int(sys.argv[2]), game_aggregations_metrics, season_aggregations_metrics)
elif command == 'update-core':
for country in client.countries():
for tournament in client.tournaments(country['id']):
for season in client.seasons(tournament['id']):
print(country['name'], tournament['name'], season['name'], '#' + str(season['id']))
fixtures_file_path = get_season_fixtures_path(season['id'])
events_file_path = get_season_events_path(season['id'])
if not os.path.exists(fixtures_file_path) or not os.path.exists(events_file_path):
print('Data not exists')
continue
update_final_storage(season['id'])
print('Updated')
elif command == 'update-finished':
final_df = get_final_storage()
final_df = final_df.loc[(final_df['home_score_first_half'] >= 0) & (final_df['away_score_first_half'] >= 0)]
populate_metrics(final_df, settings.udi_metrics)
populate_metrics(final_df, settings.composite_metrics)
populate_metrics(final_df, settings.composite_finished_metrics)
final_df.to_csv(get_finished_path())
elif command == 'update-upcoming':
fixtures_odds_df = get_fixtures_odds(client)
final_df = get_final_storage()
keys = []
for index in fixtures_odds_df.index:
if index in final_df.index:
final_df.loc[index, fixtures_odds_df.columns.tolist()] = fixtures_odds_df.loc[index].values
keys.append(index)
final_df = final_df.loc[keys]
populate_metrics(final_df, settings.udi_metrics)
populate_metrics(final_df, settings.composite_metrics)
final_df.to_csv(get_upcoming_path())
elif command == 'find-games':
filtered_games_df = find_games_by_strategies(settings.strategies)
print(filtered_games_df.sort_values(by=['start_time']))
elif command == 'find-new-games':
if len(telegram_user_id) > 0:
telegram_user_id = int(telegram_user_id)
else:
print("Please specify telegram user id")
exit(0)
filtered_games_df = find_new_games_by_strategies(settings.strategies)
notification.notify_games_list(bot, filtered_games_df, telegram_user_id)