Commit 98f2494c authored by nanahira's avatar nanahira

add cookie check

parent ae5b4c9f
import sys
from flask import Flask, Response
from flask_cors import CORS
from requests_oauthlib import OAuth1Session, OAuth2Session
......@@ -39,12 +40,12 @@ if cookie_file:
with open(cookie_file, 'r') as f:
cookies = str.strip(f.read()).split("\n")
print("Booting")
# print("Booting")
ENDPOINT = {
"UserTweetsAndReplies": "CwLU7qTfeu0doqhSr6tW4A",
"TweetDetail": "BoHLKeBvibdYDiJON1oqTg",
}
print("Booted")
# print("Booted")
FeaturesDict = {
"responsive_web_uc_gql_enabled": False,
......@@ -73,11 +74,15 @@ FeaturesJson = json.dumps(FeaturesDict)
healthLock = threading.Lock()
isHealthy = True
def getTwitterSession(useCookie):
def getTwitterSession(specifyCookie = None):
twitter_b = OAuth2Session()
twitter_b.headers["Authorization"] = "Bearer {}".format(random.choice(twitterKeys))
twitter_b.headers['user-agent'] = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
cookie = random.choice(cookies)
cookie = ''
if specifyCookie is not None:
cookie = specifyCookie
else:
cookie = random.choice(cookies)
if cookie: # and useCookie:
twitter_b.headers['cookie'] = cookie
twitter_b.headers['x-csrf-token'] = cookie.split("ct0=")[1].split(";")[0]
......@@ -85,7 +90,7 @@ def getTwitterSession(useCookie):
def doSearch(screen_name):
global isHealthy
twitter_b = getTwitterSession(True)
twitter_b = getTwitterSession()
searchurl_v2 = "https://twitter.com/i/api/graphql/nK1dw4oV3k4w5TdtcAdSww/SearchTimeline"
params_v2 = {
"features": json.dumps({
......@@ -146,6 +151,47 @@ def healthy():
# return {'healthy': True}
# return Response(status=500)
#return result
def checkCookies():
good = []
bad = []
for cookie in cookies:
print("Checking cookie: " + cookie, file=sys.stderr)
twitter_b = getTwitterSession(cookie)
response = twitter_b.get("https://api.twitter.com/1.1/guest/activate.json")
if response.status_code == 200:
return True
usertlurl = 'https://twitter.com/i/api/graphql/oUZZZ8Oddwxs8Cd3iW3UEA/UserByScreenName'
screen_name = 'Sena_n_Karin'
params = {
'variables': json.dumps({
'screen_name': screen_name,
'withSafetyModeUserFields': True,
}),
'features': json.dumps({
"hidden_profile_likes_enabled": False,
"responsive_web_graphql_exclude_directive_enabled": True,
"verified_phone_label_enabled": False,
"subscriptions_verification_info_verified_since_enabled": True,
"highlights_tweets_tab_ui_enabled": True,
"creator_subscriptions_tweet_preview_api_enabled": True,
"responsive_web_graphql_skip_user_profile_image_extensions_enabled": False,
"responsive_web_graphql_timeline_navigation_enabled": True
})
}
usertl_b = twitter_b.get(usertlurl, params=params)
usertl = usertl_b
usertl_json = usertl.json()
if usertl.status_code == 200 or not ("errors" in usertl_json and usertl_json["errors"][0]["message"] == "Could not authenticate you"):
print(f"Good cookie {usertl.status_code}: " + cookie + " " + usertl.text, file=sys.stderr)
good.append(cookie)
else:
print(f"Bad cookie {usertl.status_code}: " + cookie + " " + usertl.text, file=sys.stderr)
bad.append(cookie)
return good, bad
@app.route("/<screen_name>")
......@@ -180,7 +226,7 @@ def searchban(screen_name):
# twitter = OAuth1Session(TWITTER_IPHONE_CK, TWITTER_IPHONE_CS)
twitter_b = getTwitterSession(False)
twitter_b = getTwitterSession()
# check rate limit
# response = twitter_b.get("https://api.twitter.com/1.1/application/rate_limit_status.json")
......@@ -504,4 +550,14 @@ def searchban(screen_name):
return returnjson
if __name__ == '__main__':
app.run(debug=True, port=os.environ.get("PORT", 5000), host="0.0.0.0")
if os.getenv('CHECK_COOKIE'):
good, bad = checkCookies()
print("Good cookies:")
for g in good:
print(g)
print("Bad cookies:")
for b in bad:
print(b)
# quit
else:
app.run(debug=True, port=os.environ.get("PORT", 5000), host="0.0.0.0")
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