Use rich status when checking for proxy geofence

This commit is contained in:
rlaphoenix 2023-02-25 14:03:41 +00:00
parent cd0c419142
commit e9b3b3a588
1 changed files with 20 additions and 12 deletions

View File

@ -11,6 +11,7 @@ from requests.adapters import HTTPAdapter, Retry
from devine.core.cacher import Cacher from devine.core.cacher import Cacher
from devine.core.config import config from devine.core.config import config
from devine.core.console import console
from devine.core.constants import AnyTrack from devine.core.constants import AnyTrack
from devine.core.credential import Credential from devine.core.credential import Credential
from devine.core.titles import Title_T, Titles_T from devine.core.titles import Title_T, Titles_T
@ -29,6 +30,7 @@ class Service(metaclass=ABCMeta):
self.config = ctx.obj.config self.config = ctx.obj.config
self.log = logging.getLogger(self.__class__.__name__) self.log = logging.getLogger(self.__class__.__name__)
self.session = self.get_session() self.session = self.get_session()
self.cache = Cacher(self.__class__.__name__) self.cache = Cacher(self.__class__.__name__)
@ -37,18 +39,24 @@ class Service(metaclass=ABCMeta):
else: else:
self.proxy = None self.proxy = None
if not self.proxy and self.GEOFENCE: if not self.proxy:
# don't override the explicit proxy set by the user, even if they may be geoblocked
with console.status("Checking if current region is Geoblocked...", spinner="dots"):
if self.GEOFENCE:
# no explicit proxy, let's get one to GEOFENCE if needed # no explicit proxy, let's get one to GEOFENCE if needed
current_region = get_ip_info(self.session)["country"].lower() current_region = get_ip_info(self.session)["country"].lower()
if not any(x.lower() == current_region for x in self.GEOFENCE): if any(x.lower() == current_region for x in self.GEOFENCE):
console.log("Service is not Geoblocked in your region")
else:
requested_proxy = self.GEOFENCE[0] # first is likely main region requested_proxy = self.GEOFENCE[0] # first is likely main region
self.log.info(f"Current IP region is blocked by the service, getting Proxy to {requested_proxy}") console.log(f"Service is Geoblocked in your region, getting a Proxy to {requested_proxy}")
# current region is not in any of the service's supported regions
for proxy_provider in ctx.obj.proxy_providers: for proxy_provider in ctx.obj.proxy_providers:
self.proxy = proxy_provider.get_proxy(requested_proxy) self.proxy = proxy_provider.get_proxy(requested_proxy)
if self.proxy: if self.proxy:
self.log.info(f" + {self.proxy} (from {proxy_provider.__class__.__name__})") console.log(f"Got Proxy from {proxy_provider.__class__.__name__}")
break break
else:
console.log("Service has no Geofence")
if self.proxy: if self.proxy:
self.session.proxies.update({"all": self.proxy}) self.session.proxies.update({"all": self.proxy})