33"""
44
55# import modules
6- import os , json , gevent , datetime , redis
6+ import os
7+ import json
8+ import gevent
9+ import redis
10+ import logging
711from steam .client import SteamClient
812from deta import Deta
913
1014
1115def app_info (app_id ):
12- connect_retries = 3
13- connect_timeout = 5
14- current_time = str (datetime .datetime .now ())
16+ connect_retries = 2
17+ connect_timeout = 3
18+
19+ logging .info ("Started requesting app info" , extra = {"app_id" : app_id })
1520
1621 try :
1722 # Sometimes it hangs for 30+ seconds. Normal connection takes about 500ms
1823 for _ in range (connect_retries ):
19- count = _ + 1
20- count = str (count )
24+ count = str (_ )
2125
2226 try :
2327 with gevent .Timeout (connect_timeout ):
24- print ("Connecting via steamclient" )
25- print (
26- "Retrieving app info for: "
27- + str (app_id )
28- + ", retry count: "
29- + count
28+ logging .info (
29+ "Retrieving app info from steamclient" ,
30+ extra = {"app_id" : app_id , "retry_count" : count },
3031 )
3132
33+ logging .debug ("Connecting via steamclient to steam api" )
3234 client = SteamClient ()
3335 client .anonymous_login ()
3436 client .verbose_debug = False
37+
38+ logging .debug ("Requesting app info from steam api" )
3539 info = client .get_product_info (apps = [app_id ], timeout = 1 )
3640
3741 return info
3842
3943 except gevent .timeout .Timeout :
44+ logging .warning (
45+ "Encountered timeout when trying to connect to steam api. Retrying.."
46+ )
4047 client ._connecting = False
4148
4249 else :
43- print ("Succesfully retrieved app info for app id: " + str ( app_id ) )
50+ logging . info ("Succesfully retrieved app info" , extra = { "app_id" : app_id } )
4451 break
4552 else :
53+ logging .error (
54+ "Max connect retries exceeded" ,
55+ extra = {"connect_retries" : connect_retries },
56+ )
4657 raise Exception (f"Max connect retries ({ connect_retries } ) exceeded" )
4758
4859 except Exception as err :
49- print ("Failed in retrieving app info for app id: " + str ( app_id ) )
50- print (err )
60+ logging . error ("Failed in retrieving app info" , extra = { "app_id" : app_id } )
61+ logging . error (err , extra = { "app_id" : app_id } )
5162
5263
5364def cache_read (app_id ):
@@ -61,7 +72,10 @@ def cache_read(app_id):
6172 return deta_read (app_id )
6273 else :
6374 # print query parse error and return empty dict
64- print ("Incorrect set cache type: " + os .environ ["CACHE_TYPE" ])
75+ logging .error (
76+ "Set incorrect cache type" ,
77+ extra = {"app_id" : app_id , "cache_type" : os .environ ["CACHE_TYPE" ]},
78+ )
6579
6680 # return failed status
6781 return False
@@ -78,7 +92,10 @@ def cache_write(app_id, data):
7892 return deta_write (app_id , data )
7993 else :
8094 # print query parse error and return empty dict
81- print ("Incorrect set cache type: " + os .environ ["CACHE_TYPE" ])
95+ logging .error (
96+ "Set incorrect cache type" ,
97+ extra = {"app_id" : app_id , "cache_type" : os .environ ["CACHE_TYPE" ]},
98+ )
8299
83100 # return failed status
84101 return False
@@ -130,13 +147,13 @@ def redis_read(app_id):
130147 # return cached data
131148 return data
132149
133- except Exception as read_error :
150+ except Exception as redis_error :
134151 # print query parse error and return empty dict
135- print (
136- "The following error occured while trying to read and decode "
137- + "from Redis cache: \n > "
138- + str (read_error )
152+ logging .error (
153+ "An error occured while trying to read and decode from Redis cache" ,
154+ extra = {"app_id" : app_id , "error_msg" : redis_error },
139155 )
156+
140157 # return failed status
141158 return False
142159
@@ -162,15 +179,35 @@ def redis_write(app_id, data):
162179
163180 except Exception as redis_error :
164181 # print query parse error and return empty dict
165- print (
166- "The following error occured while trying to write to Redis cache: \n > "
167- + str ( redis_error )
182+ logging . error (
183+ "An error occured while trying to write to Redis cache" ,
184+ extra = { "app_id" : app_id , "error_msg" : redis_error },
168185 )
169186
170187 # return fail status
171188 return False
172189
173190
191+ def log_level (level ):
192+ """
193+ Sets lowest level to log.
194+ """
195+
196+ match level :
197+ case "debug" :
198+ logging .getLogger ().setLevel (logging .DEBUG )
199+ case "info" :
200+ logging .getLogger ().setLevel (logging .INFO )
201+ case "warning" :
202+ logging .getLogger ().setLevel (logging .WARNING )
203+ case "error" :
204+ logging .getLogger ().setLevel (logging .ERROR )
205+ case "critical" :
206+ logging .getLogger ().setLevel (logging .CRITICAL )
207+ case _:
208+ logging .getLogger ().setLevel (logging .WARNING )
209+
210+
174211def deta_read (app_id ):
175212 """
176213 Read app info from Deta base cache.
0 commit comments