From 491a0b3a5ae06239193791a265e97ada7aa2cefa Mon Sep 17 00:00:00 2001 From: rlaphoenix Date: Mon, 1 Apr 2024 17:50:08 +0100 Subject: [PATCH] feat(Basic): Allow proxy selection by index (one-indexed) --- CONFIG.md | 4 +++- devine/core/proxies/basic.py | 20 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CONFIG.md b/CONFIG.md index 7e0d247..8b4d450 100644 --- a/CONFIG.md +++ b/CONFIG.md @@ -302,7 +302,9 @@ us: 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) diff --git a/devine/core/proxies/basic.py b/devine/core/proxies/basic.py index 144e638..0972a2c 100644 --- a/devine/core/proxies/basic.py +++ b/devine/core/proxies/basic.py @@ -1,4 +1,5 @@ import random +import re from typing import Optional, Union from requests.utils import prepend_scheme_if_needed @@ -25,12 +26,27 @@ class Basic(Proxy): """Get a proxy URI from the config.""" 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: - return + raise ValueError(f"There's no proxies configured for \"{country_code}\"...") if isinstance(servers, str): 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: proxy = random.choice(servers)