summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--steamrelationships/sr.py39
1 files changed, 23 insertions, 16 deletions
diff --git a/steamrelationships/sr.py b/steamrelationships/sr.py
index 63122b4..8185ebb 100644
--- a/steamrelationships/sr.py
+++ b/steamrelationships/sr.py
@@ -2,6 +2,7 @@ import requests
2import json 2import json
3import time 3import time
4import random 4import random
5import sys
5 6
6from steamrelationships.constants import _Const 7from steamrelationships.constants import _Const
7CONST = _Const() 8CONST = _Const()
@@ -9,10 +10,7 @@ CONST = _Const()
9class SteamRelationships: 10class SteamRelationships:
10 """A class that handles the querring of Steam's web api to request a user's friends list""" 11 """A class that handles the querring of Steam's web api to request a user's friends list"""
11 12
12 session: object = requests.Session() # Session object so repeated querries to steam's api use the same TCP connection 13 def __init__(self, webapikey: str, timeout: int = 30, timeout_retries: int = 5, reqdelay: float = 0.5, delayrand: int = 25, reqsafetybuffer: float = 0.9, reqjson: str = f"{sys.path[0]}/requests.json") -> None:
13 scanlist: dict = {} # To be populated by scan functions
14
15 def __init__(self, webapikey: str, timeout: int = 30, timeout_retries: int = 5, reqdelay: float = 0.5, delayrand: int = 25, reqsafetybuffer: float = 0.9, reqjson: str = "requests.json") -> None:
16 """ 14 """
17 (str) webapikey - A Steam "web api key" for grabbing friends lists (get one at https://steamcommunity.com/dev/apikey) 15 (str) webapikey - A Steam "web api key" for grabbing friends lists (get one at https://steamcommunity.com/dev/apikey)
18 16
@@ -33,10 +31,15 @@ class SteamRelationships:
33 self.timeout: int = int(abs(timeout)) 31 self.timeout: int = int(abs(timeout))
34 self.timeout_retries: int = int(abs(timeout_retries)) 32 self.timeout_retries: int = int(abs(timeout_retries))
35 self.reqdelay: float = float(abs(reqdelay)) 33 self.reqdelay: float = float(abs(reqdelay))
36 self.delayrand: int = int(abs(delayrand) % 100) 34 self.delayrand: int = int(abs(delayrand) % 100) # clamped to 0-99
37 self.reqsafetybuffer: float = float(abs(reqsafetybuffer)) 35 self.reqsafetybuffer: float = float(abs(reqsafetybuffer))
38 self.reqjson: str = str(reqjson) 36 self.reqjson: str = str(reqjson)
39 37
38 # Not specified by user, but necessary regardless
39 self.session: requests.Session = requests.Session() # Session object so repeated querries to steam's api use the same TCP connection
40 self.scanlist: dict = {} # To be populated by scan functions
41
42
40 def __str__(self) -> str: 43 def __str__(self) -> str:
41 return f"Vals:\n\twebapikey: {len(self.webapikey) > 0} (Actual value ommitted for security)\n\n\tTimeout: {self.timeout}\n\tTimeout Retries: {self.timeout_retries}\n\tRequest Delay: {self.reqdelay}\n\tRequest Delay Randomness Factor: +/- {self.delayrand}%\n\tRequest Safety Buffer: {self.reqsafetybuffer} ({self.reqsafetybuffer * CONST.STEAMAPI_MAXREQ} out of {CONST.STEAMAPI_MAXREQ} max requests)\n\tRequests Log Filepath: \"{self.reqjson}\"\n\n\tMost Recent Scan: {self.scanlist}" 44 return f"Vals:\n\twebapikey: {len(self.webapikey) > 0} (Actual value ommitted for security)\n\n\tTimeout: {self.timeout}\n\tTimeout Retries: {self.timeout_retries}\n\tRequest Delay: {self.reqdelay}\n\tRequest Delay Randomness Factor: +/- {self.delayrand}%\n\tRequest Safety Buffer: {self.reqsafetybuffer} ({self.reqsafetybuffer * CONST.STEAMAPI_MAXREQ} out of {CONST.STEAMAPI_MAXREQ} max requests)\n\tRequests Log Filepath: \"{self.reqjson}\"\n\n\tMost Recent Scan: {self.scanlist}"
42 45
@@ -81,10 +84,12 @@ class SteamRelationships:
81 84
82 return final 85 return final
83 86
84 def __checkrequests(self, filename: str = "") -> int: 87 def __checkrequests(self, filename: str = "", increment: bool = True) -> int:
85 """ 88 """
86 __checkrequests(self, filename: str = "") -> int: Check the requests log to make sure Steam's request limit hasn't been passed 89 __checkrequests(self, filename: str = "", increment: bool = True) -> int: Check the requests log to make sure Steam's request limit hasn't been passed
87 filename: filepath to requests log 90 filename: filepath to requests log
91 increment: Whether to increment the number of requests or not
92
88 int (return): The number of requests in the last 24 hours. -1 on error, 0 on too many requests, and >1 if a valid number of requests 93 int (return): The number of requests in the last 24 hours. -1 on error, 0 on too many requests, and >1 if a valid number of requests
89 94
90 __checkrequests will create a file at filepath if it doesn't exist. It will also never go over 100,000 requests, regardless of what reqsafetybuffer is 95 __checkrequests will create a file at filepath if it doesn't exist. It will also never go over 100,000 requests, regardless of what reqsafetybuffer is
@@ -110,22 +115,24 @@ class SteamRelationships:
110 # This bullshit brought to you by: ordered dictionaries 115 # This bullshit brought to you by: ordered dictionaries
111 currentreqs = jsoncontents[list(jsoncontents.keys())[-1]][0] 116 currentreqs = jsoncontents[list(jsoncontents.keys())[-1]][0]
112 if currentreqs < (CONST.STEAMAPI_MAXREQ * self.reqsafetybuffer) and currentreqs < CONST.STEAMAPI_MAXREQ: 117 if currentreqs < (CONST.STEAMAPI_MAXREQ * self.reqsafetybuffer) and currentreqs < CONST.STEAMAPI_MAXREQ:
113 jsoncontents[list(jsoncontents.keys())[-1]][0] += 1 118 if increment == True:
114 jsoncontents[list(jsoncontents.keys())[-1]][1].append(checktime) 119 jsoncontents[list(jsoncontents.keys())[-1]][0] += 1
120 jsoncontents[list(jsoncontents.keys())[-1]][1].append(checktime)
115 121
116 else: 122 else:
117 print(f"[__checkrequests] Daily request limit reached ({currentreqs}/{CONST.STEAMAPI_MAXREQ * self.reqsafetybuffer}). Please try again tomorrow, or increase \"reqsafetybuffer\" (currently: {self.reqsafetybuffer})") 123 print(f"[__checkrequests] Daily request limit reached ({currentreqs}/{CONST.STEAMAPI_MAXREQ * self.reqsafetybuffer}). Please try again tomorrow, or increase \"reqsafetybuffer\" (currently: {self.reqsafetybuffer})")
118 return 0 124 return 0
119 125
120 # Update the json file 126 # Update the json file
121 try: 127 if increment == True:
122 with open(filename, "w+t") as jsonfile: 128 try:
123 json.dump(jsoncontents, jsonfile, indent=CONST.JSON_INDENT) 129 with open(filename, "w+t") as jsonfile:
124 jsonfile.close() 130 json.dump(jsoncontents, jsonfile, indent=CONST.JSON_INDENT)
131 jsonfile.close()
125 132
126 except Exception as e: 133 except Exception as e:
127 print(f"[__checkrequests] Could not update json file ({e})") 134 print(f"[__checkrequests] Could not update json file ({e})")
128 return -1 135 return -1
129 136
130 return jsoncontents[list(jsoncontents.keys())[-1]][0] 137 return jsoncontents[list(jsoncontents.keys())[-1]][0]
131 138