33import contextlib
44import json
55import secrets
6+ import time
67from collections .abc import Callable , Coroutine , Mapping
78from functools import wraps
89from pathlib import Path
910from typing import Any , cast
1011
1112import requests .exceptions
13+ import urllib3 .exceptions
1214from cryptography .fernet import Fernet , InvalidToken
1315from instagrapi import Client # type: ignore[reportMissingTypeStubs]
1416from instagrapi .exceptions import ( # type: ignore[reportMissingTypeStubs]
@@ -70,9 +72,9 @@ async def wrapper(*args: Any, **kwargs: Any) -> T:
7072
7173
7274class NoFallbackProxyAdapter (HTTPAdapter ):
73- """Забороняє fallback на локальний IP, але дозволяє retry на SSL помилки ."""
75+ """Забороняє fallback на локальний IP, дозволяє retry на SSL та обриви з'єднання ."""
7476
75- MAX_SSL_RETRIES = 3
77+ MAX_RETRIES = 3
7678
7779 def send (
7880 self ,
@@ -85,7 +87,7 @@ def send(
8587 ) -> Response :
8688 last_error : Exception | None = None
8789
88- for attempt in range (1 , self .MAX_SSL_RETRIES + 1 ):
90+ for attempt in range (1 , self .MAX_RETRIES + 1 ):
8991 try :
9092 return super ().send (
9193 request ,
@@ -95,22 +97,25 @@ def send(
9597 cert = cert ,
9698 proxies = proxies ,
9799 )
98- except requests .exceptions .SSLError as e :
100+ except (
101+ requests .exceptions .SSLError ,
102+ requests .exceptions .ProxyError ,
103+ requests .exceptions .ConnectionError ,
104+ urllib3 .exceptions .ProtocolError ,
105+ ) as e :
99106 last_error = e
100- logger .warning (f"SSL error on attempt { attempt } /{ self .MAX_SSL_RETRIES } : { e } " )
101- if attempt == self .MAX_SSL_RETRIES :
107+ logger .warning (f"Network/Proxy error on attempt { attempt } /{ self .MAX_RETRIES } : { e } " )
108+ if attempt == self .MAX_RETRIES :
102109 raise RuntimeError (
103- f"Proxy SSL failed after { self .MAX_SSL_RETRIES } attempts: { e } "
110+ f"Proxy connection failed after { self .MAX_RETRIES } attempts: { e } "
104111 ) from e
105112
106- import time
107-
108113 time .sleep (1.0 * attempt )
109114
110115 except Exception as e :
111- raise RuntimeError (f"Proxy connection failed mid-request: { e } " ) from e
116+ raise RuntimeError (f"Unexpected connection error mid-request: { e } " ) from e
112117
113- raise RuntimeError (f"Proxy SSL failed: { last_error } " )
118+ raise RuntimeError (f"Proxy SSL/Connection failed: { last_error } " )
114119
115120
116121class InstagramClient :
0 commit comments