From ee5dd508c33c27d2988b93118b6e48bb8e6ac8da Mon Sep 17 00:00:00 2001 From: Dylan Date: Tue, 15 Aug 2023 10:20:58 +0100 Subject: [PATCH] New extract method: proxies (WIP) --- twExtract/Dockerfile | 2 +- twExtract/__init__.py | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/twExtract/Dockerfile b/twExtract/Dockerfile index 7771573..18afb78 100644 --- a/twExtract/Dockerfile +++ b/twExtract/Dockerfile @@ -1,5 +1,5 @@ FROM public.ecr.aws/lambda/python:3.8 -RUN pip install yt-dlp +RUN pip install requests==2.31.0 # Copy function code diff --git a/twExtract/__init__.py b/twExtract/__init__.py index 9629e88..a583509 100644 --- a/twExtract/__init__.py +++ b/twExtract/__init__.py @@ -6,7 +6,6 @@ import os import random from . import twExtractError import urllib.parse -from configHandler import config bearer="Bearer AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw" v2Bearer="Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA" guestToken=None @@ -106,6 +105,23 @@ def extractStatus_syndication(url): return output +def extractStatus_twExtractProxy(url): + proxies = os.getenv("VXTWITTER_PROXIES",None) + if proxies is None: + raise twExtractError.TwExtractError(400, "Extract error") + proxies = proxies.split(',') + random.shuffle(proxies) + for proxy in proxies: + try: + tweet = requests.get(f"{proxy}?url={urllib.parse.quote(url)}") + output = tweet.json() + if "errors" in output: + # try another token + continue + except Exception as e: + continue + return output + def extractStatusV2(url,workaroundTokens): global usedTokens # get tweet ID @@ -181,7 +197,7 @@ def extractStatusV2Legacy(url,workaroundTokens): return tweet['legacy'] def extractStatus(url,workaroundTokens=None): - methods=[extractStatus_syndication,extractStatusV2Legacy] + methods=[extractStatus_syndication,extractStatusV2Legacy,extractStatus_twExtractProxy] for method in methods: try: return method(url,workaroundTokens)