From 46bc95c34fa8cda0988d5eeca4338abf29c3126a Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Fri, 23 Jul 2021 16:50:12 +0200 Subject: [PATCH] add support for multiple countries per provider --- backend/stream.py | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/backend/stream.py b/backend/stream.py index 462ebd1..42f79a4 100755 --- a/backend/stream.py +++ b/backend/stream.py @@ -2,6 +2,7 @@ import json import sys import os +import re import base64 import logging import asyncio @@ -66,18 +67,41 @@ class ProxyElem(): return urls proxies = {} +new_providers = {} for key in providers: - proxies[key] = [] + expr = re.compile(f'{key}_([a-z][a-z])?[0-9]+', re.IGNORECASE) + matches = list(filter(expr.match, os.environ.keys())) current = [] - for i in range(0,9): - proxy = os.environ.get(f'{key}_proxy{i}'.upper()) + current_keys = set() + current_keys.add(key) + countries = [] + empty = True + for match in matches: + country_groups = expr.match(match.lower()).groups() + country = None + pos = len(country_groups) - 1 + if pos >= 0: + country = country_groups[pos] + current_keys.add(f'{key}_{country}') + proxy = os.environ.get(match) if proxy is not None: current.append(proxy) - if len(current) == 0: - proxies[key].append(ProxyElem(None)) - else: - for proxy in current: - proxies[key].append(ProxyElem(proxy)) + countries.append(country) + if country is None: + empty = False + for elem in current_keys: + proxies[elem] = [] + new_providers[elem] = providers[key] + print(proxies) + for proxy, country in zip(current, countries): + new_key = key + if country is not None: + new_key = f'{key}_{country}' + proxies[new_key].append(ProxyElem(proxy)) + for elem in current_keys: + if len(proxies[elem]) == 0: + proxies[elem].append(ProxyElem(None)) +providers = new_providers proxy_keys = [] for proxy_provider in proxies.values():