Skip to content

Commit

Permalink
Fix import loop
Browse files Browse the repository at this point in the history
  • Loading branch information
westsurname authored May 30, 2024
1 parent a426485 commit ca4dfad
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 61 deletions.
3 changes: 2 additions & 1 deletion shared/debrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from urllib.parse import urljoin
from datetime import datetime
from shared.discord import discordUpdate
from shared.shared import realdebrid, torbox, mediaExtensions, checkRequiredEnvs, retryRequest
from shared.requests import retryRequest
from shared.shared import realdebrid, torbox, mediaExtensions, checkRequiredEnvs

def validateDebridEnabled():
if not realdebrid['enabled'] and not torbox['enabled']:
Expand Down
60 changes: 60 additions & 0 deletions shared/requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import time
import requests
from typing import Callable, Optional
from shared.discord import discordError, discordUpdate


def retryRequest(
requestFunc: Callable[[], requests.Response],
print: Callable[..., None] = print,
retries: int = 1,
delay: int = 1
) -> Optional[requests.Response]:
"""
Retry a request if the response status code is not in the 200 range.
:param requestFunc: A callable that returns an HTTP response.
:param print: Optional print function for logging.
:param retries: The number of times to retry the request after the initial attempt.
:param delay: The delay between retries in seconds.
:return: The response object or None if all attempts fail.
"""
attempts = retries + 1 # Total attempts including the initial one
for attempt in range(attempts):
try:
response = requestFunc()
if 200 <= response.status_code < 300:
return response
else:
message = [
f"URL: {response.url}",
f"Status code: {response.status_code}",
f"Message: {response.reason}",
f"Response: {response.content}",
f"Attempt {attempt + 1} failed"
]
for line in message:
print(line)
if attempt == retries:
discordError("Request Failed", "\n".join(message))
else:
update_message = message + [f"Retrying in {delay} seconds..."]
discordUpdate("Retrying Request", "\n".join(update_message))
print(f"Retrying in {delay} seconds...")
time.sleep(delay)
except requests.RequestException as e:
message = [
f"URL: {response.url if 'response' in locals() else 'unknown'}",
f"Attempt {attempt + 1} encountered an error: {e}"
]
for line in message:
print(line)
if attempt == retries:
discordError("Request Exception", "\n".join(message))
else:
update_message = message + [f"Retrying in {delay} seconds..."]
discordUpdate("Retrying Request", "\n".join(update_message))
print(f"Retrying in {delay} seconds...")
time.sleep(delay)

return None
61 changes: 1 addition & 60 deletions shared/shared.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import os
import re
import time
import requests
from typing import Callable, Optional
from environs import Env
from shared.discord import discordError, discordUpdate

env = Env()
env.read_env()
Expand Down Expand Up @@ -200,59 +196,4 @@ def checkRequiredEnvs(requiredEnvs):
else:
previousSuccess = True
else:
previousSuccess = True

def retryRequest(
requestFunc: Callable[[], requests.Response],
print: Callable[..., None] = print,
retries: int = 1,
delay: int = 1
) -> Optional[requests.Response]:
"""
Retry a request if the response status code is not in the 200 range.
:param requestFunc: A callable that returns an HTTP response.
:param print: Optional print function for logging.
:param retries: The number of times to retry the request after the initial attempt.
:param delay: The delay between retries in seconds.
:return: The response object or None if all attempts fail.
"""
attempts = retries + 1 # Total attempts including the initial one
for attempt in range(attempts):
try:
response = requestFunc()
if 200 <= response.status_code < 300:
return response
else:
message = [
f"URL: {response.url}",
f"Status code: {response.status_code}",
f"Message: {response.reason}",
f"Response: {response.content}",
f"Attempt {attempt + 1} failed"
]
for line in message:
print(line)
if attempt == retries:
discordError("Request Failed", "\n".join(message))
else:
update_message = message + [f"Retrying in {delay} seconds..."]
discordUpdate("Retrying Request", "\n".join(update_message))
print(f"Retrying in {delay} seconds...")
time.sleep(delay)
except requests.RequestException as e:
message = [
f"URL: {response.url if 'response' in locals() else 'unknown'}",
f"Attempt {attempt + 1} encountered an error: {e}"
]
for line in message:
print(line)
if attempt == retries:
discordError("Request Exception", "\n".join(message))
else:
update_message = message + [f"Retrying in {delay} seconds..."]
discordUpdate("Retrying Request", "\n".join(update_message))
print(f"Retrying in {delay} seconds...")
time.sleep(delay)

return None
previousSuccess = True

0 comments on commit ca4dfad

Please sign in to comment.