ChenyuRabbitLove commited on
Commit
eca6f3a
1 Parent(s): 6a301ce
Files changed (3) hide show
  1. app.py +1 -1
  2. utils/mes_player_model.py +1 -2
  3. utils/utils.py +14 -9
app.py CHANGED
@@ -17,7 +17,7 @@ seafoam = Seafoam()
17
 
18
 
19
  def get_player_info(player_backend_user_id):
20
- with open("data.json", "r", encoding="utf-8") as file:
21
  player_info = json.load(file)
22
  return player_info[player_backend_user_id]
23
 
 
17
 
18
 
19
  def get_player_info(player_backend_user_id):
20
+ with open("current_data.json", "r", encoding="utf-8") as file:
21
  player_info = json.load(file)
22
  return player_info[player_backend_user_id]
23
 
utils/mes_player_model.py CHANGED
@@ -22,7 +22,6 @@ class Player:
22
  created_at_date: datetime.date = field(default_factory=date.today)
23
  updated_at_date: datetime.date = field(default_factory=date.today)
24
 
25
-
26
  def __post_init__(self):
27
  self.badges = self.badges.tolist()
28
  self.partners = self.partners.tolist()
@@ -40,4 +39,4 @@ class Player:
40
  Player: A Player instance.
41
  """
42
  data = series.copy()
43
- return Player(**data)
 
22
  created_at_date: datetime.date = field(default_factory=date.today)
23
  updated_at_date: datetime.date = field(default_factory=date.today)
24
 
 
25
  def __post_init__(self):
26
  self.badges = self.badges.tolist()
27
  self.partners = self.partners.tolist()
 
39
  Player: A Player instance.
40
  """
41
  data = series.copy()
42
+ return Player(**data)
utils/utils.py CHANGED
@@ -18,10 +18,10 @@ SCOPES = ["https://www.googleapis.com/auth/bigquery"]
18
  SERVICE_ACCOUNT_INFO = os.getenv("GBQ_TOKEN")
19
  service_account_info_dict = json.loads(SERVICE_ACCOUNT_INFO)
20
 
21
- creds = Credentials.from_service_account_info(
22
- service_account_info_dict, scopes=SCOPES
23
- )
24
- client = bigquery.Client(credentials=creds, project=service_account_info_dict['project_id'])
25
 
26
 
27
  def get_content(file_name: str) -> str:
@@ -154,6 +154,7 @@ def query_bq_table(client, sql):
154
  logging.error(f"Query Failed: {e}")
155
  raise
156
 
 
157
  def load_player_statuses(client, date_str):
158
  table_name = f"mes_report_{date_str}"
159
  sql = f"SELECT * FROM `data_mart.{table_name}`"
@@ -162,19 +163,23 @@ def load_player_statuses(client, date_str):
162
  for _, row in query_bq_table(client, sql).iterrows()
163
  }
164
 
 
165
  def get_date_strs(delta_days=0):
166
  target_date = datetime.now().date() - timedelta(days=delta_days)
167
  return target_date.strftime("%Y%m%d")
168
 
 
169
  def save_latest_player_data():
170
- latest_player_data = load_player_statuses(client, '20231113')
171
- latest_player_data_as_dict = {key: asdict(value) for key, value in latest_player_data.items()}
 
 
172
 
173
  def date_serializer(obj):
174
  if isinstance(obj, date):
175
  return obj.isoformat()
176
  raise TypeError("Type not serializable")
177
-
178
- with open('latest_player_data.json', 'w') as fp:
179
  print("Saving latest player data...")
180
- json.dump(latest_player_data_as_dict, fp, default=date_serializer)
 
18
  SERVICE_ACCOUNT_INFO = os.getenv("GBQ_TOKEN")
19
  service_account_info_dict = json.loads(SERVICE_ACCOUNT_INFO)
20
 
21
+ creds = Credentials.from_service_account_info(service_account_info_dict, scopes=SCOPES)
22
+ client = bigquery.Client(
23
+ credentials=creds, project=service_account_info_dict["project_id"]
24
+ )
25
 
26
 
27
  def get_content(file_name: str) -> str:
 
154
  logging.error(f"Query Failed: {e}")
155
  raise
156
 
157
+
158
  def load_player_statuses(client, date_str):
159
  table_name = f"mes_report_{date_str}"
160
  sql = f"SELECT * FROM `data_mart.{table_name}`"
 
163
  for _, row in query_bq_table(client, sql).iterrows()
164
  }
165
 
166
+
167
  def get_date_strs(delta_days=0):
168
  target_date = datetime.now().date() - timedelta(days=delta_days)
169
  return target_date.strftime("%Y%m%d")
170
 
171
+
172
  def save_latest_player_data():
173
+ latest_player_data = load_player_statuses(client, "20231113")
174
+ latest_player_data_as_dict = {
175
+ key: asdict(value) for key, value in latest_player_data.items()
176
+ }
177
 
178
  def date_serializer(obj):
179
  if isinstance(obj, date):
180
  return obj.isoformat()
181
  raise TypeError("Type not serializable")
182
+
183
+ with open("latest_player_data.json", "w") as fp:
184
  print("Saving latest player data...")
185
+ json.dump(latest_player_data_as_dict, fp, default=date_serializer)