Skip to content

Commit 3884b8f

Browse files
committed
added retries for api
1 parent 9b2cb75 commit 3884b8f

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

banner.png

-12.9 KB
Loading

script.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,26 @@
7575
api_comments = cache[user_name]['response']
7676
else:
7777
# Make API request for user comments
78-
url = f'https://api.pushshift.io/reddit/comment/search?html_decode=true&after=0&author={user_name}&size=500'
79-
try:
80-
with requests.Session() as session:
81-
response = session.get(url)
82-
# Add a sleep of 7 seconds after each API request
83-
time.sleep(7)
84-
api_comments = response.json()['data']
85-
except (requests.exceptions.RequestException, json.JSONDecodeError) as e:
86-
print(
87-
f"Error occurred while making the API request: {e}")
88-
api_comments = []
78+
url = f'https://api.pushshift.io/reddit/comment/search?author={user_name}&size=250'
79+
retries = 4
80+
for i in range(retries):
81+
try:
82+
with requests.Session() as session:
83+
response = session.get(url)
84+
# Add a sleep of 7 seconds after each API request
85+
time.sleep(7)
86+
api_comments = response.json()['data']
87+
break
88+
# Add 3 retries
89+
except (requests.exceptions.RequestException, json.JSONDecodeError) as e:
90+
if i == retries - 1:
91+
print(
92+
f"Error occurred while making the API request: {e}")
93+
api_comments = []
94+
else:
95+
print(
96+
f"Retrying API request. Attempt {i+1} of {retries}.")
97+
time.sleep(10)
8998

9099
# Store response in cache
91100
cache[user_name] = {

0 commit comments

Comments
 (0)