Commit 79b0651f authored by nanahira's avatar nanahira

update status check

parent e236b8db
Pipeline #18300 passed with stage
in 38 seconds
......@@ -6,4 +6,4 @@ RUN pip install -r requirements.txt
COPY ./backend_requests.py ./app.py
COPY . ./
CMD ["gunicorn", "--workers=2", "-b", "0.0.0.0:80", "app:app"]
CMD ["gunicorn", "--workers=1", "-b", "0.0.0.0:80", "app:app"]
from flask import Flask
from flask import Flask, Response
from flask_cors import CORS
from requests_oauthlib import OAuth1Session, OAuth2Session
import os
import time
import json
from get_graphql_endpoint import endpoint
import threading
app = Flask(__name__)
CORS(app)
TWITTER_AUTH_KEY = 'AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA'
print("Booting")
ENDPOINT = endpoint()
print("Booted")
FeaturesDict = {
"responsive_web_uc_gql_enabled": False,
......@@ -34,9 +38,41 @@ FeaturesDict = {
}
FeaturesJson = json.dumps(FeaturesDict)
healthLock = threading.Lock()
isHealthy = True
def getTwitterSession():
twitter_b = OAuth2Session()
twitter_b.headers["Authorization"] = "Bearer {}".format(TWITTER_AUTH_KEY)
return twitter_b
def doSearch(screen_name, twitter_b):
global isHealthy
searchurl_v2 = "https://api.twitter.com/2/search/adaptive.json"
params_v2 = {"q": "from:@{}".format(screen_name), "count": 1, "spelling_corrections": 0, "tweet_search_mode": "live"}
search_v2 = twitter_b.get(searchurl_v2, params=params_v2).json()
isHealthy = 'globalObjects' in search_v2
if isHealthy:
return search_v2["globalObjects"]["tweets"]
else:
print("Rate limit occurred, marking as unavailable.")
return None
@app.route('/_healthy')
def healthy():
return {"healthy": True}
result = None
if isHealthy:
return {'healthy': True}
with healthLock:
if isHealthy:
return {'healthy': True}
doSearch('Dream', getTwitterSession())
if isHealthy:
print("Rate limit passed.")
return {'healthy': True}
return Response(status=500)
return result
@app.route("/<screen_name>")
def searchban(screen_name):
......@@ -63,8 +99,7 @@ def searchban(screen_name):
# twitter = OAuth1Session(TWITTER_IPHONE_CK, TWITTER_IPHONE_CS)
twitter_b = OAuth2Session()
twitter_b.headers["Authorization"] = "Bearer {}".format(TWITTER_AUTH_KEY)
twitter_b = getTwitterSession()
# check rate limit
# response = twitter_b.get("https://api.twitter.com/1.1/application/rate_limit_status.json")
......@@ -160,11 +195,10 @@ def searchban(screen_name):
# else:
# return returnjson
searchurl_v2 = "https://api.twitter.com/2/search/adaptive.json"
params_v2 = {"q": "from:@{}".format(screen_name), "count": 1, "spelling_corrections": 0, "tweet_search_mode": "live"}
search_v2 = twitter_b.get(searchurl_v2, params=params_v2).json()
search_tweets = search_v2["globalObjects"]["tweets"]
if search_tweets == {}:
search_tweets = doSearch(screen_name, twitter_b)
if search_tweets is None:
returnjson["tests"]["search"] = '_error'
elif search_tweets == {}:
returnjson["tests"]["search"] = False
# returnjson["tests"]["typeahead"] = False
else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment