Skip to content

Commit

Permalink
Reuse proxies after timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Jul 13, 2024
1 parent bfcfb5e commit 1616417
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/mmdiary/video/uploader/dailymotion.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

FILE_SIZE_LIMIT = 4 * 1024 * 1024 * 1024

PROXY_REUSE_TTL = 60 * 30 # 30 minutes

DAILYMOTION_EMBED_URL = "https://www.dailymotion.com/embed/video/"
DAILYMOTION_METADATA_URL = "https://www.dailymotion.com/player/metadata/video/"

Expand All @@ -36,6 +38,22 @@ def generate_video_url(provider, pos=None):
return url + f"&start={int(pos)}"


class TimedSet:
def __init__(self, ttl):
self.__ttl = ttl
self.__items = {}

def add(self, key):
self.__items[key] = time.time()

def __contains__(self, key):
if key in self.__items:
if time.time() - self.__items[key] < self.__ttl:
return True
del self.__items[key]
return False


class DailymotionAccounts:
def __init__(self, accounts_file):
self.__accounts_file = accounts_file
Expand Down Expand Up @@ -92,7 +110,7 @@ def __init__(self, use_proxy=None):
if self.__use_proxy is None:
self.__use_proxy = self.__accounts.count() > 1

self.__already_used_proxy = set()
self.__already_used_proxy = TimedSet(PROXY_REUSE_TTL)
self.__reset_proxy()

def __reset_proxy(self):
Expand Down

0 comments on commit 1616417

Please sign in to comment.