From f2cec8521fef6571596e6f8c3ad32b4409e28b71 Mon Sep 17 00:00:00 2001 From: Dylan Date: Thu, 15 Jun 2023 09:16:41 +0100 Subject: [PATCH] Very very quick fix for API changes --- .github/workflows/deploy.yml | 3 ++- configHandler.py | 1 + serverless.yml | 1 + twExtract/__init__.py | 43 ++++++++++++++++++------------------ 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index c3b53b2..5d8849e 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -68,4 +68,5 @@ jobs: VXTWITTER_WORKAROUND_CONSUMER_KEY: ${{ secrets.VXTWITTER_WORKAROUND_CONSUMER_KEY }} VXTWITTER_WORKAROUND_CONSUMER_SECRET: ${{ secrets.VXTWITTER_WORKAROUND_CONSUMER_SECRET }} VXTWITTER_WORKAROUND_TOKEN: ${{ secrets.VXTWITTER_WORKAROUND_TOKEN }} - VXTWITTER_WORKAROUND_TOKEN_SECRET: ${{ secrets.VXTWITTER_WORKAROUND_TOKEN_SECRET }} \ No newline at end of file + VXTWITTER_WORKAROUND_TOKEN_SECRET: ${{ secrets.VXTWITTER_WORKAROUND_TOKEN_SECRET }} + VXTWITTER_WORKAROUND_TOKENS: ${{ secrets.VXTWITTER_WORKAROUND_TOKENS }} \ No newline at end of file diff --git a/configHandler.py b/configHandler.py index c0ae984..0b59d3a 100644 --- a/configHandler.py +++ b/configHandler.py @@ -15,6 +15,7 @@ elif ('RUNNING_SERVERLESS' in os.environ and os.environ['RUNNING_SERVERLESS'] == "url": os.environ["VXTWITTER_URL"], "combination_method": os.environ["VXTWITTER_COMBINATION_METHOD"], # can either be 'local' or a URL to a server handling requests in the same format "gifConvertAPI":os.environ["VXTWITTER_GIF_CONVERT_API"], + "workaroundTokens":os.environ["VXTWITTER_WORKAROUND_TOKENS"], "workaroundKeys":{ "consumerKey":os.environ["VXTWITTER_WORKAROUND_CONSUMER_KEY"], "consumerSecret":os.environ["VXTWITTER_WORKAROUND_CONSUMER_SECRET"], diff --git a/serverless.yml b/serverless.yml index d318e91..4474592 100644 --- a/serverless.yml +++ b/serverless.yml @@ -31,6 +31,7 @@ provider: VXTWITTER_WORKAROUND_CONSUMER_SECRET: ${env:VXTWITTER_WORKAROUND_CONSUMER_SECRET, ''} VXTWITTER_WORKAROUND_TOKEN: ${env:VXTWITTER_WORKAROUND_TOKEN, ''} VXTWITTER_WORKAROUND_TOKEN_SECRET: ${env:VXTWITTER_WORKAROUND_TOKEN_SECRET, ''} + VXTWITTER_WORKAROUND_TOKENS: ${env:VXTWITTER_WORKAROUND_TOKENS, ''} package: patterns: diff --git a/twExtract/__init__.py b/twExtract/__init__.py index 0c86b36..8229581 100644 --- a/twExtract/__init__.py +++ b/twExtract/__init__.py @@ -1,26 +1,17 @@ import yt_dlp from yt_dlp.extractor import twitter +import uuid import json import requests import re +import random from . import twExtractError -import twitter from configHandler import config bearer="Bearer AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw" guestToken=None pathregex = r"\w{1,15}\/(status|statuses)\/(\d{2,20})" userregex = r"^https?:\/\/(?:www\.)?twitter\.com\/(?:#!\/)?@?([^/?#]*)(?:[?#/].*)?$" userIDregex = r"\/i\/user\/(\d+)" -try: - auth = twitter.oauth.OAuth( - config['config']['workaroundKeys']["accessToken"], - config['config']['workaroundKeys']["accessTokenSecret"], - config['config']['workaroundKeys']["consumerKey"], - config['config']['workaroundKeys']["consumerSecret"] - ) - api = twitter.Twitter(auth=auth) -except Exception as e: - api = None def getGuestToken(): global guestToken @@ -30,17 +21,25 @@ def getGuestToken(): return guestToken def extractStatus_fallback(url): - if api is None: - raise twExtractError.TwExtractError(500, "Could not extract tweet.") - print(" ➤ [ I ] Using fallback method to extract tweet") - # get tweet ID - m = re.search(pathregex, url) - if m is None: - raise twExtractError.TwExtractError(400, "Invalid URL") - twid = m.group(2) - # get tweet - tweet = api.statuses.show(_id=twid, tweet_mode="extended") - return tweet + try: + # get tweet ID + m = re.search(pathregex, url) + if m is None: + raise twExtractError.TwExtractError(400, "Extract error") + twid = m.group(2) + # get tweet + + authToken=random.choice(config["config"]["workaroundTokens"].split(",")) + csrfToken=str(uuid.uuid4()).replace('-', '') + tweet = requests.get("https://api.twitter.com/1.1/statuses/show/" + twid + ".json?tweet_mode=extended&cards_platform=Web-12&include_cards=1&include_reply_count=1&include_user_entities=0", headers={"Authorization":bearer,"Cookie":f"auth_token={authToken}; ct0={csrfToken}; ","x-twitter-active-user":"yes","x-twitter-auth-type":"OAuth2Session","x-twitter-client-language":"en","x-csrf-token":csrfToken,"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0"}) + output = tweet.json() + if "errors" in output: + # pick the first error and create a twExtractError + error = output["errors"][0] + raise twExtractError.TwExtractError(error["code"], error["message"]) + return output + except Exception as e: + raise twExtractError.TwExtractError(400, "Extract error") def extractStatus(url):