mirror of https://github.com/devine-dl/devine.git
feat(Basic): Allow proxy selection by index (one-indexed)
This commit is contained in:
parent
b36befb296
commit
491a0b3a5a
|
@ -302,7 +302,9 @@ us:
|
||||||
de: "https://127.0.0.1:8080"
|
de: "https://127.0.0.1:8080"
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that if multiple proxies are defined for a region, then it will randomly choose which one to use.
|
Note that if multiple proxies are defined for a region, then by default one will be randomly chosen.
|
||||||
|
You can choose a specific one by specifying it's number, e.g., `--proxy basic:us2` will choose the
|
||||||
|
second proxy of the US list.
|
||||||
|
|
||||||
### nordvpn (dict)
|
### nordvpn (dict)
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import random
|
import random
|
||||||
|
import re
|
||||||
from typing import Optional, Union
|
from typing import Optional, Union
|
||||||
|
|
||||||
from requests.utils import prepend_scheme_if_needed
|
from requests.utils import prepend_scheme_if_needed
|
||||||
|
@ -25,12 +26,27 @@ class Basic(Proxy):
|
||||||
"""Get a proxy URI from the config."""
|
"""Get a proxy URI from the config."""
|
||||||
query = query.lower()
|
query = query.lower()
|
||||||
|
|
||||||
servers = self.countries.get(query)
|
match = re.match(r"^([a-z]{2})(\d+)?$", query, re.IGNORECASE)
|
||||||
|
if not match:
|
||||||
|
raise ValueError(f"The query \"{query}\" was not recognized...")
|
||||||
|
|
||||||
|
country_code = match.group(1)
|
||||||
|
entry = match.group(2)
|
||||||
|
|
||||||
|
servers: Optional[Union[str, list[str]]] = self._data.get(country_code)
|
||||||
if not servers:
|
if not servers:
|
||||||
return
|
raise ValueError(f"There's no proxies configured for \"{country_code}\"...")
|
||||||
|
|
||||||
if isinstance(servers, str):
|
if isinstance(servers, str):
|
||||||
proxy = servers
|
proxy = servers
|
||||||
|
elif entry:
|
||||||
|
try:
|
||||||
|
proxy = servers[int(entry) - 1]
|
||||||
|
except IndexError:
|
||||||
|
raise ValueError(
|
||||||
|
f"There's only {len(servers)} prox{'y' if len(servers) == 1 else 'ies'} "
|
||||||
|
f"for \"{country_code}\"..."
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
proxy = random.choice(servers)
|
proxy = random.choice(servers)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue