2022-08-26 05:22:51 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2022-08-29 02:42:43 +00:00
|
|
|
from typing import List, Tuple
|
2022-08-26 05:22:51 +00:00
|
|
|
import tweepy
|
|
|
|
from time import sleep
|
|
|
|
from random import shuffle
|
|
|
|
|
2022-08-29 02:42:43 +00:00
|
|
|
# Return our credentials from environment variables as UTF-8 bytes.
|
2022-08-26 06:52:09 +00:00
|
|
|
def get_arguments() -> Tuple[bytes, bytes, bytes, bytes]:
|
2022-08-29 02:42:43 +00:00
|
|
|
consumer_key: bytes = os.environ['CONSUMER_KEY'].encode('utf8')
|
|
|
|
consumer_secret: bytes = os.environ['CONSUMER_SECRET'].encode('utf8')
|
|
|
|
access_token: bytes = os.environ['ACCESS_TOKEN'].encode('utf8')
|
|
|
|
access_token_secret: bytes = os.environ['ACCESS_TOKEN_SECRET'].encode('utf8')
|
2022-08-26 05:22:51 +00:00
|
|
|
|
|
|
|
return consumer_key, consumer_secret, access_token, access_token_secret
|
|
|
|
|
|
|
|
|
2022-08-29 02:42:43 +00:00
|
|
|
# Copy the file lines to a new List[str] and shuffle it.
|
|
|
|
def populate_array(file_lines: List[str]) -> List[str]:
|
|
|
|
array: List[str] = file_lines.copy()
|
2022-08-26 05:22:51 +00:00
|
|
|
|
2022-08-29 02:42:43 +00:00
|
|
|
# Scramble like an egg.
|
2022-08-26 05:22:51 +00:00
|
|
|
shuffle(array)
|
|
|
|
|
|
|
|
return array
|
|
|
|
|
|
|
|
|
2022-08-29 02:42:43 +00:00
|
|
|
# Main function.
|
2022-08-26 05:22:51 +00:00
|
|
|
def main() -> None:
|
2022-08-29 02:42:43 +00:00
|
|
|
consumer_key: bytes
|
|
|
|
consumer_secret: bytes
|
|
|
|
access_token: bytes
|
|
|
|
access_token_secret: bytes
|
2022-08-26 05:22:51 +00:00
|
|
|
|
2022-08-29 02:42:43 +00:00
|
|
|
# Access and set authentication method to our Twitter credentials.
|
|
|
|
consumer_key, consumer_secret, access_token, access_token_secret = get_arguments()
|
|
|
|
auth: tweepy.OAuth1UserHandler = tweepy.OAuth1UserHandler(consumer_key=consumer_key, consumer_secret=consumer_secret,
|
|
|
|
access_token=access_token, access_token_secret=access_token_secret)
|
|
|
|
|
|
|
|
# Creating an API object with our authentication method.
|
|
|
|
api: tweepy.API = tweepy.API(auth=auth)
|
2022-08-26 05:22:51 +00:00
|
|
|
|
2022-08-29 02:42:43 +00:00
|
|
|
array: List[str]
|
|
|
|
with open('./everybody.txt', 'r', encoding='utf-8') as my_file:
|
|
|
|
# Read lines one by one from my_file and assign to file_lines variable
|
|
|
|
file_lines: List[str] = [x.strip() for x in my_file.readlines()]
|
2022-08-26 05:22:51 +00:00
|
|
|
|
2022-08-29 02:42:43 +00:00
|
|
|
# Close file.
|
|
|
|
my_file.close()
|
2022-08-26 05:22:51 +00:00
|
|
|
|
2022-08-29 02:42:43 +00:00
|
|
|
# Create List[str] from file_lines.
|
|
|
|
array = populate_array(file_lines)
|
2022-08-26 05:22:51 +00:00
|
|
|
|
|
|
|
# Driving loop.
|
|
|
|
while True:
|
|
|
|
while array:
|
2022-08-29 02:42:43 +00:00
|
|
|
# Grab last element and then remove it.
|
|
|
|
tweet: str = array.pop()
|
2022-08-26 05:22:51 +00:00
|
|
|
|
|
|
|
try:
|
2022-08-29 02:42:43 +00:00
|
|
|
# Output the last element, which will be the tweet.
|
2022-08-26 05:22:51 +00:00
|
|
|
print(tweet)
|
2022-08-29 02:42:43 +00:00
|
|
|
|
|
|
|
# Create a tweet through the API.
|
2022-08-26 05:22:51 +00:00
|
|
|
api.update_status(tweet)
|
2022-08-29 02:42:43 +00:00
|
|
|
|
|
|
|
# Chill out for 1800 seconds.
|
2022-08-26 05:22:51 +00:00
|
|
|
sleep(1800)
|
|
|
|
except tweepy.Forbidden as fe:
|
2022-08-29 02:42:43 +00:00
|
|
|
# We probably tried to create a duplicate tweet, but say what happened.
|
|
|
|
print(fe.api_errors)
|
|
|
|
|
|
|
|
# Chill out for 30 seconds.
|
2022-08-26 05:22:51 +00:00
|
|
|
sleep(30)
|
|
|
|
except tweepy.BadRequest as be:
|
2022-08-29 02:42:43 +00:00
|
|
|
# Something is wrong with our client, so say what happened.
|
|
|
|
print(be.api_errors)
|
|
|
|
|
|
|
|
# Bail. We shouldn't keep trying.
|
2022-08-26 05:22:51 +00:00
|
|
|
sys.exit(1)
|
|
|
|
except tweepy.TweepyException as te:
|
2022-08-29 02:42:43 +00:00
|
|
|
# Something very unexpected happened, so say what happened.
|
2022-08-26 05:22:51 +00:00
|
|
|
print(te)
|
2022-08-29 02:42:43 +00:00
|
|
|
|
|
|
|
# Bail. We shouldn't keep trying.
|
2022-08-26 05:22:51 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2022-08-29 02:42:43 +00:00
|
|
|
# Let's do the time warp again.
|
2022-08-26 05:22:51 +00:00
|
|
|
array = populate_array(file_lines)
|
|
|
|
|
|
|
|
|
2022-08-29 02:42:43 +00:00
|
|
|
# Do not delete.
|
2022-08-26 05:22:51 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|