diff --git a/twExtract/Dockerfile b/twExtract/Dockerfile new file mode 100644 index 0000000..7771573 --- /dev/null +++ b/twExtract/Dockerfile @@ -0,0 +1,9 @@ +FROM public.ecr.aws/lambda/python:3.8 +RUN pip install yt-dlp + + +# Copy function code +COPY __init__.py ${LAMBDA_TASK_ROOT}/app.py + +# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile) +CMD [ "app.lambda_handler" ] \ No newline at end of file diff --git a/twExtract.py b/twExtract/__init__.py similarity index 56% rename from twExtract.py rename to twExtract/__init__.py index 811808a..baf5172 100644 --- a/twExtract.py +++ b/twExtract/__init__.py @@ -1,4 +1,3 @@ -import imp import yt_dlp from yt_dlp.extractor import twitter import json @@ -15,4 +14,16 @@ def extractStatus(url): 'include_user_entities': 0, 'tweet_mode': 'extended', }) - return status \ No newline at end of file + return status + +def lambda_handler(event, context): + if ("queryStringParameters" not in event): + return { + "statusCode": 400, + "body": "Invalid request." + } + url = event["queryStringParameters"].get("url","") + return { + 'statusCode': 200, + 'body': extractStatus(url) + } \ No newline at end of file diff --git a/twitfix.py b/twitfix.py index d8398c7..817933d 100644 --- a/twitfix.py +++ b/twitfix.py @@ -1,3 +1,4 @@ +from random import Random, random from weakref import finalize from flask import Flask, render_template, request, redirect, abort, Response, send_from_directory, url_for, send_file, make_response, jsonify from flask_cors import CORS @@ -13,10 +14,10 @@ import combineImg from datetime import date,datetime, timedelta from io import BytesIO import msgs -import twExtract +import twExtract as twExtract from configHandler import config from cache import addVnfToLinkCache,getVnfFromLinkCache - +import random app = Flask(__name__) CORS(app) @@ -341,7 +342,14 @@ def link_to_vnf_from_tweet_data(tweet,video_link): def link_to_vnf_from_unofficial_api(video_link): print(" ➤ [ + ] Attempting to download tweet info from UNOFFICIAL Twitter API") - tweet = twExtract.extractStatus(video_link) + try: + tweet = twExtract.extractStatus(video_link) + except Exception as e: + print('print(" ➤ [ !!! ] Local UNOFFICIAL API Failed")') + if ('apiMirrors' in config['config'] and len(config['config']['apiMirrors']) > 0): + mirror = random.choice(config['config']['apiMirrors']) + print(" ➤ [ + ] Using API Mirror: "+mirror) + tweet = requests.get(mirror+"?url="+video_link).json() print (" ➤ [ ✔ ] Unofficial API Success") return link_to_vnf_from_tweet_data(tweet,video_link)