2
2
3
3
from collections import defaultdict
4
4
from datetime import datetime
5
- from typing import TYPE_CHECKING , DefaultDict , Dict , List
5
+ from functools import lru_cache
6
+ from typing import TYPE_CHECKING , Any , DefaultDict , Dict , List
6
7
7
- import requests
8
+ import httpx
9
+ from loguru import logger
8
10
from sqlalchemy .orm import Session
9
11
12
+ from app import config
10
13
from app .database .models import UserSettings
11
14
12
15
if TYPE_CHECKING :
@@ -17,16 +20,14 @@ def is_user_signed_up_for_game_releases(
17
20
session : Session ,
18
21
current_user_id : int ,
19
22
) -> bool :
20
- is_signed_up = (
23
+ is_signed_up = bool (
21
24
session .query (UserSettings )
22
25
.filter (UserSettings .user_id == current_user_id )
23
26
.filter (UserSettings .video_game_releases .is_ (True ))
24
- .first ()
27
+ .first (),
25
28
)
26
29
27
- if is_signed_up :
28
- return True
29
- return False
30
+ return is_signed_up
30
31
31
32
32
33
def add_game_events_to_weeks (
@@ -41,11 +42,20 @@ def add_game_events_to_weeks(
41
42
last_day : Day = last_week .days [- 1 ]
42
43
first_day_str = datetime .strptime (first_day .set_id (), "%d-%B-%Y" )
43
44
last_day_str = datetime .strptime (last_day .set_id (), "%d-%B-%Y" )
44
- games_by_dates = get_games_data_separated_by_days (
45
+
46
+ output = get_games_data_by_dates_from_api (
45
47
start_date = first_day_str .strftime ("%Y-%m-%d" ),
46
48
end_date = last_day_str .strftime ("%Y-%m-%d" ),
47
49
)
48
- formatted_games = get_formatted_games_in_days (games_by_dates )
50
+ if not output ["success" ]:
51
+ logger .exception ("Unsuccessful RAWG API call" )
52
+ return weeks
53
+ games_by_dates = output ["results" ]
54
+
55
+ unformatted_games_by_dates = get_games_data_separated_by_dates (
56
+ games_by_dates ,
57
+ )
58
+ formatted_games = get_formatted_games_in_days (unformatted_games_by_dates )
49
59
50
60
return insert_formatted_games_to_weeks (weeks , formatted_games )
51
61
@@ -67,18 +77,47 @@ def insert_formatted_games_to_weeks(
67
77
return weeks
68
78
69
79
70
- def get_games_data_separated_by_days (
71
- start_date : datetime ,
72
- end_date : datetime ,
73
- ) -> DefaultDict [List [Dict ]]:
80
+ @lru_cache (maxsize = 128 )
81
+ def get_games_data_by_dates_from_api (
82
+ start_date : str ,
83
+ end_date : str ,
84
+ ) -> Dict [str , Any ]:
74
85
API = "https://api.rawg.io/api/games"
75
-
76
- current_day_games = requests .get (
77
- f"{ API } ?dates={ start_date } ,{ end_date } " ,
78
- )
79
- current_day_games = current_day_games .json ()["results" ]
86
+ NO_API_RESPONSE = "The RAWG server did not response"
87
+ input_query_string = {
88
+ "dates" : f"{ start_date } ,{ end_date } " ,
89
+ "key" : config .RAWG_API_KEY ,
90
+ }
91
+
92
+ output : Dict [str , Any ] = {}
93
+ try :
94
+ response = httpx .get (
95
+ API ,
96
+ params = input_query_string ,
97
+ )
98
+ except httpx .HTTPError :
99
+ output ["success" ] = False
100
+ output ["error" ] = NO_API_RESPONSE
101
+ return output
102
+
103
+ if response .status_code != httpx .codes .OK :
104
+ output ["success" ] = False
105
+ output ["error" ] = NO_API_RESPONSE
106
+ return output
107
+
108
+ output ["success" ] = True
109
+ try :
110
+ output .update (response .json ())
111
+ return output
112
+ except KeyError :
113
+ output ["success" ] = False
114
+ output ["error" ] = response .json ()["error" ]["message" ]
115
+ return output
116
+
117
+
118
+ def get_games_data_separated_by_dates (api_data ):
80
119
games_data = defaultdict (list )
81
- for result in current_day_games :
120
+ for result in api_data :
82
121
current = {
83
122
"name" : result ["name" ],
84
123
"platforms" : [],
@@ -98,8 +137,9 @@ def get_formatted_games_in_days(
98
137
formatted_games = defaultdict (list )
99
138
100
139
for date , game_data in separated_games_dict .items ():
101
- formatted_game_str = format_single_game (game_data , with_platforms )
102
- formatted_games [date ].append (formatted_game_str )
140
+ for game in game_data :
141
+ formatted_game_str = format_single_game (game , with_platforms )
142
+ formatted_games [date ].append (formatted_game_str )
103
143
return formatted_games
104
144
105
145
@@ -110,6 +150,7 @@ def format_single_game(raw_game, with_platforms=False):
110
150
formatted_game_str += "-Platforms-<br>"
111
151
for platform in raw_game ["platforms" ]:
112
152
formatted_game_str += f"{ platform } ,"
153
+ return formatted_game_str
113
154
114
155
115
156
def translate_ymd_date_to_dby (ymd_str : str ) -> str :
0 commit comments