#!/usr/bin/env python3 import os import sys from typing import Tuple import tweepy from time import sleep from random import shuffle def get_arguments() -> Tuple[str, str, str, str]: consumer_key = os.environ['CONSUMER_KEY'] consumer_secret = os.environ['CONSUMER_SECRET'] access_token = os.environ['ACCESS_TOKEN'] access_token_secret = os.environ['ACCESS_TOKEN_SECRET'] return consumer_key, consumer_secret, access_token, access_token_secret def populate_array(file_lines) -> list: length = len(file_lines) array = [] for i in range(length): array.append(i) shuffle(array) return array def main() -> None: # Access and authorize our Twitter credentials consumer_key, consumer_secret, access_token, access_token_secret = get_arguments() auth = tweepy.OAuth1UserHandler(consumer_key=consumer_key, consumer_secret=consumer_secret, access_token=access_token, access_token_secret=access_token_secret) api = tweepy.API(auth=auth) my_file = open('./everybody.txt', 'r', encoding='utf-8') # Read lines one by one from my_file and assign to file_lines variable file_lines = [x.strip() for x in my_file.readlines()] # Close file. my_file.close() array = populate_array(file_lines) # Driving loop. while True: while array: tweet = file_lines[array.pop()] try: print(tweet) api.update_status(tweet) sleep(1800) except tweepy.Forbidden as fe: print(f"{fe.api_errors}") sleep(30) except tweepy.BadRequest as be: print(f"{be.api_errors}") sys.exit(1) except tweepy.TweepyException as te: print(te) sys.exit(1) array = populate_array(file_lines) if __name__ == '__main__': main()