Skip to content

Commit

Permalink
multiple accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
KTachibanaM committed Oct 5, 2024
1 parent 3952e2f commit 92249a9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ nitter.conf
guest_accounts.json*
dump.rdb
.env
twitter-credentials.json
7 changes: 7 additions & 0 deletions docker-compose.self-contained.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ services:
volumes:
# should map this in PaaS
- nitter-data:/nitter-data
# optional mapping for twitter-credentials.json file
# - ./twitter-credentials.json:/nitter-data/twitter-credentials.json
environment:
# shuold be included for custom paths
- NITTER_ACCOUNTS_FILE=/nitter-data/guest_accounts.json
# optional twitter-credentials.json custom path
# - TWITTER_CREDENTIALS_FILE=/nitter-data/twitter-credentials.json
# optional instance custmizations from env
- INSTANCE_TITLE=Custom title
- INSTANCE_THEME=Twitter Dark
Expand All @@ -27,6 +31,9 @@ services:
# - REDIS_HOST=nitter-redis
# - REDIS_PORT=6379
# - REDIS_PASSWORD=
# optional debugging flags
# - DEBUG=1
# - RESET_NITTER_ACCOUNTS_FILE=1
env_file:
# should require from env
# TWITTER_USERNAME
Expand Down
56 changes: 41 additions & 15 deletions scripts/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


def auth(username: str, password: str, mfa_code: Optional[str]) -> Optional[dict]:
logging.debug("start auth")
logging.debug(f"start authenticating @{username}")

bearer_token_req = requests.post("https://api.twitter.com/oauth2/token",
headers={
Expand Down Expand Up @@ -163,11 +163,12 @@ def parse_auth_file(auth_file: str) -> bool:
return False
for i, r in enumerate(res):
if "oauth_token" not in r:
logging.error(f"Expecting 'oauth_token' in auth item #{i}")
logging.error(f"Expecting 'oauth_token' in auth item #{i + 1}")
return False
if "oauth_token_secret" not in r:
logging.error(f"Expecting 'oauth_token_secret' in auth item #{i}")
logging.error(f"Expecting 'oauth_token_secret' in auth item #{i + 1}")
return False
logging.debug(f"There are {len(res)} accounts")
return True


Expand All @@ -177,6 +178,11 @@ def parse_auth_file(auth_file: str) -> bool:
sys.exit(1)

output_file = sys.argv[1]

if os.getenv("RESET_NITTER_ACCOUNTS_FILE", "0") == "1" and os.path.exists(output_file):
print(f"Resetting auth file {output_file}")
os.remove(output_file)

if os.path.exists(output_file):
print(f"Validating auth file {output_file}")
if parse_auth_file(output_file):
Expand All @@ -186,21 +192,41 @@ def parse_auth_file(auth_file: str) -> bool:
print(f"Auth file {output_file} is invalid. Please remove and rerun.")
sys.exit(1)

username = os.getenv("TWITTER_USERNAME")
if not username:
print("Please set environment variable TWITTER_USERNAME")
twitter_credentials_file = os.getenv("TWITTER_CREDENTIALS_FILE", None)
username = os.getenv("TWITTER_USERNAME", None)
password = os.getenv("TWITTER_PASSWORD", None)

if not twitter_credentials_file and not (username and password):
print("Please set environment variable TWITTER_CREDENTIALS_FILE, or both TWITTER_USERNAME and TWITTER_PASSWORD")
sys.exit(1)
password = os.getenv("TWITTER_PASSWORD")
if not password:
print("Please set environment variable TWITTER_PASSWORD")

twitter_credentials = []
if twitter_credentials_file:
with open(twitter_credentials_file, "r") as f:
twitter_credentials = json.loads(f.read())
else:
mfa_code = os.getenv("TWITTER_MFA_CODE", None)
twitter_credentials = [{"username": username, "password": password, "mfa_code": mfa_code}]

auth_results = []
for credential in twitter_credentials:
username = credential["username"]
password = credential["password"]
mfa_code = credential.get("mfa_code", None)
auth_result = auth(username, password, mfa_code)
auth_results.append(auth_result)

if len(list(filter(lambda x: x is not None, auth_results))) == 0:
print("Failed authentication for any account. Did you enter the right username/password? Please rerun with environment variable DEBUG=1 for debugging, e.g. uncomment the DEBUG=1 in docker-compose.self-contained.yml file.")
sys.exit(1)
mfa_code = os.getenv("TWITTER_MFA_CODE", None)

auth_res = auth(username, password, mfa_code)
valid_auth_results = []
for i, auth_result in enumerate(auth_results):
if auth_result is None:
print(f"Failed authentication for account #{i}, but still proceeding.")
else:
valid_auth_results.append(auth_result)

if auth_res is None:
print("Failed authentication. You might have entered the wrong username/password. Please rerun with environment variable DEBUG=1 for debugging, e.g. docker compose -e DEBUG=1 run.")
sys.exit(1)
with open(output_file, "w") as f:
f.write(json.dumps([auth_res]))
f.write(json.dumps(valid_auth_results))
print(f"Auth file {output_file} created successfully")
2 changes: 2 additions & 0 deletions scripts/dump_env_and_procfile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ set -e
echo Dumping auth env...
echo TWITTER_USERNAME=$TWITTER_USERNAME > /src/.env
echo TWITTER_PASSWORD=$TWITTER_PASSWORD >> /src/.env
echo TWITTER_CREDENTIALS_FILE=$TWITTER_CREDENTIALS_FILE >> /src/.env
echo RESET_NITTER_ACCOUNTS_FILE=$RESET_NITTER_ACCOUNTS_FILE >> /src/.env
echo DEBUG=$DEBUG >> /src/.env

echo Dumping custom path env...
Expand Down
10 changes: 10 additions & 0 deletions twitter-credentials.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"username": "",
"password": ""
},
{
"username": "",
"password": ""
}
]

0 comments on commit 92249a9

Please sign in to comment.