Commit 87dc9b18 authored by nanahira's avatar nanahira

add redis

parent 966024be
Pipeline #19674 passed with stage
in 27 seconds
...@@ -7,7 +7,18 @@ import json ...@@ -7,7 +7,18 @@ import json
from get_graphql_endpoint import endpoint from get_graphql_endpoint import endpoint
import threading import threading
import random import random
import redis
redisClient = None
if os.getenv('REDIS_HOST'):
redisPool = redis.ConnectionPool(
host= os.getenv('REDIS_HOST'),
port= os.getenv('REDIS_PORT', 6379),
password= os.getenv('REDIS_PASSWORD', None),
db = os.getenv('REDIS_DB', 0)
)
redisClient = redis.Redis(connection_pool=redisPool)
app = Flask(__name__) app = Flask(__name__)
CORS(app) CORS(app)
...@@ -125,6 +136,13 @@ def healthy(): ...@@ -125,6 +136,13 @@ def healthy():
@app.route("/<screen_name>") @app.route("/<screen_name>")
def searchban(screen_name): def searchban(screen_name):
if redisClient:
cached = redisClient.get('shadowban:{}'.format(screen_name))
if cached:
print('Using cached result for {}: {}'.format(screen_name, cached))
cachedDict = json.loads(cached)
return cachedDict
print("Checking {}".format(screen_name)) print("Checking {}".format(screen_name))
returnjson = { returnjson = {
"timestamp": time.time(), "timestamp": time.time(),
...@@ -233,6 +251,7 @@ def searchban(screen_name): ...@@ -233,6 +251,7 @@ def searchban(screen_name):
"ghost": {"ban": None}, "ghost": {"ban": None},
"more_replies": {"ban": False, "tweet": "-1", "in_reply_to": "-1"} "more_replies": {"ban": False, "tweet": "-1", "in_reply_to": "-1"}
} }
hasFailure = False
# searchurl = "https://api.twitter.com/1.1/users/search.json" # searchurl = "https://api.twitter.com/1.1/users/search.json"
# params = {"q": "from:@{}".format(screen_name), "count": 1} # params = {"q": "from:@{}".format(screen_name), "count": 1}
...@@ -244,7 +263,7 @@ def searchban(screen_name): ...@@ -244,7 +263,7 @@ def searchban(screen_name):
# else: # else:
# return returnjson # return returnjson
print("Checking search suggestion banned of {}".format(screen_name))
suggestions = twitter_b.get("https://api.twitter.com/1.1/search/typeahead.json?src=search_box&result_type=users&q=@" + screen_name).json() suggestions = twitter_b.get("https://api.twitter.com/1.1/search/typeahead.json?src=search_box&result_type=users&q=@" + screen_name).json()
try: try:
returnjson["tests"]["typeahead"] = len([1 for user in suggestions["users"] if user["screen_name"].lower() == screen_name.lower()]) > 0 returnjson["tests"]["typeahead"] = len([1 for user in suggestions["users"] if user["screen_name"].lower() == screen_name.lower()]) > 0
...@@ -252,16 +271,21 @@ def searchban(screen_name): ...@@ -252,16 +271,21 @@ def searchban(screen_name):
returnjson["tests"]["typeahead"] = False returnjson["tests"]["typeahead"] = False
if returnjson["tests"]["typeahead"] == False: if returnjson["tests"]["typeahead"] == False:
print("{} is search suggestion banned, checking search ban.".format(screen_name))
search_tweets = doSearch(screen_name, twitter_b) search_tweets = doSearch(screen_name, twitter_b)
if search_tweets is None: if search_tweets is None:
returnjson["tests"]["search"] = '_error' returnjson["tests"]["search"] = '_error'
hasFailure = True
elif search_tweets == {}: elif search_tweets == {}:
returnjson["tests"]["search"] = False returnjson["tests"]["search"] = False
print("{} is search banned.".format(screen_name))
# returnjson["tests"]["typeahead"] = False # returnjson["tests"]["typeahead"] = False
else: else:
returnjson["tests"]["search"] = str(search_tweets[list(search_tweets.keys())[0]]["id"]) returnjson["tests"]["search"] = str(search_tweets[list(search_tweets.keys())[0]]["id"])
print("{} is not search banned.".format(screen_name))
else: else:
returnjson["tests"]["search"] = "_implied_good" returnjson["tests"]["search"] = "_implied_good"
print("{} is not search suggestion banned, skipped search ban check.".format(screen_name))
## get replies ## get replies
## Start GraphQL ## Start GraphQL
...@@ -413,15 +437,18 @@ def searchban(screen_name): ...@@ -413,15 +437,18 @@ def searchban(screen_name):
print("Response: " + replies.text) print("Response: " + replies.text)
returnjson["tests"]["ghost"] = {} returnjson["tests"]["ghost"] = {}
returnjson["tests"]["more_replies"] = {} returnjson["tests"]["more_replies"] = {}
hasFailure = True
#print("ban" in returnjson["tests"]["more_replies"]) #print("ban" in returnjson["tests"]["more_replies"])
#print("ban" in returnjson["tests"]["ghost"]) #print("ban" in returnjson["tests"]["ghost"])
#print(returnjson["tests"]["ghost"]) #print(returnjson["tests"]["ghost"])
plainJson = json.dumps(returnjson)
print("Result of {}: {}".format(screen_name, plainJson))
print("Result of {}: {}".format(screen_name, json.dumps(returnjson))) if redisClient and not hasFailure:
redisClient.set("shadowban:{}".format(screen_name), plainJson, ex=600)
return returnjson return returnjson
if __name__ == '__main__': if __name__ == '__main__':
......
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