From 0c6ae92866f3b6091d57c6a681f0d1ec89321ce8 Mon Sep 17 00:00:00 2001 From: Erik Kristofer Anderson Date: Sat, 21 May 2022 14:56:17 -0500 Subject: [PATCH 1/7] refactor main and load_env plus just print for now We can't use tweepy.API, because the twitter bot account is only allowed to use twitter API v2, which is compatible with tweepy.Client(). Also the loadenv file needed to be refactored because it wasn't working. Also changed the logic in main.py to suit the new reminders.txt format. Also created an example .env-example file to show the shape of what the .env files needs to be, and added .env to .gitignore. --- .env-example | 4 ++++ .gitignore | 1 + __pycache__/load_env.cpython-39.pyc | Bin 0 -> 526 bytes load_env.py | 12 ++++++------ main.py | 20 ++++++++++---------- requirements.txt | 2 +- 6 files changed, 22 insertions(+), 17 deletions(-) create mode 100644 .env-example create mode 100644 .gitignore create mode 100644 __pycache__/load_env.cpython-39.pyc diff --git a/.env-example b/.env-example new file mode 100644 index 0000000..15addac --- /dev/null +++ b/.env-example @@ -0,0 +1,4 @@ +API_KEY=something +API_SECRET=something +ACCESS_TOKEN=something +ACCESS_TOKEN_SECRET=something diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/__pycache__/load_env.cpython-39.pyc b/__pycache__/load_env.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..96cb625aec94480f41fc1acf4aaa964b4d01011f GIT binary patch literal 526 zcmYjOu};G<5Vf7qmNpeg{RgBhq(rbGL=|N~l?tL|AWM|SE{dAOmF-eh3R7A53mEw& zuT1;`6X&#`o^|KB*z7b2u6%- z5JuFZ_A81kipR)3_=a3Ga zWgs?9zv_wM_HKy|Xs#_R*rLX^*TRggKB$Y@3+AqFT^5`!b9Mz2N3sE-g&nIwegU%Y BgdhL_ literal 0 HcmV?d00001 diff --git a/load_env.py b/load_env.py index ff2747c..7bebf83 100644 --- a/load_env.py +++ b/load_env.py @@ -1,12 +1,12 @@ import os -from dotenv import load_dotenv +from dotenv import dotenv_values # takes environment variables from .env -load_dotenv() +config = dotenv_values(".env") def load_twitter_env(): - consumer_key = os.getenv("API_KEY") - consumer_secret = os.getenv("API_SECRET") - access_token = os.getenv("ACCESS_TOKEN") - access_token_secret = os.getenv("ACCESS_TOKEN_SECRET") + consumer_key = config["API_KEY"] + consumer_secret = config["API_SECRET"] + access_token = config["ACCESS_TOKEN"] + access_token_secret = config["ACCESS_TOKEN_SECRET"] return consumer_key, consumer_secret, access_token, access_token_secret diff --git a/main.py b/main.py index bbf110c..f42b0aa 100644 --- a/main.py +++ b/main.py @@ -8,24 +8,24 @@ # Loading twitter credentials consumer_key, consumer_secret, access_token, access_token_secret = load_twitter_env() -# Authenticate to Twitter using Tweepy -auth = tweepy.OAuthHandler(consumer_key, consumer_secret) -auth.set_access_token(access_token, access_token_secret) - -# Connect to the TWITTER API -api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True) - +# Connect to the TWITTER Client +client = tweepy.Client(bearer_token='enter-token', + consumer_key=consumer_key, + consumer_secret=consumer_secret, + access_token=access_token, + access_token_secret=access_token_secret) def reminder(): while True: with open('reminders.txt') as f: lines = f.readlines() - + # Select a random line from the reminders file. - lines = [line[1:] for line in lines if line[0] == "-"] + lines = [line[3:] for line in lines if len(line) > 2 and line[2] == "-"] line = lines[random.randint(0, len(lines) - 1)] remind = line.strip() # Random reminder tweets - api.update_status(status=remind) + # client.create_tweet(text=remind) + print(remind) # sleeps for 4 hours sleep(14400) diff --git a/requirements.txt b/requirements.txt index 391f3d3..2b2a797 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ python-dotenv==0.20.0 -tweepy==3.10.0 +tweepy~=4.0 From 90c1ec2655eb52e235128c9ab1181375ce6b28fa Mon Sep 17 00:00:00 2001 From: Erik Kristofer Anderson Date: Sat, 21 May 2022 16:17:19 -0500 Subject: [PATCH 2/7] delete __pycache__ --- __pycache__/load_env.cpython-39.pyc | Bin 526 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 __pycache__/load_env.cpython-39.pyc diff --git a/__pycache__/load_env.cpython-39.pyc b/__pycache__/load_env.cpython-39.pyc deleted file mode 100644 index 96cb625aec94480f41fc1acf4aaa964b4d01011f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 526 zcmYjOu};G<5Vf7qmNpeg{RgBhq(rbGL=|N~l?tL|AWM|SE{dAOmF-eh3R7A53mEw& zuT1;`6X&#`o^|KB*z7b2u6%- z5JuFZ_A81kipR)3_=a3Ga zWgs?9zv_wM_HKy|Xs#_R*rLX^*TRggKB$Y@3+AqFT^5`!b9Mz2N3sE-g&nIwegU%Y BgdhL_ From 37f3264b712f548de7a138ed1476f32de702bc33 Mon Sep 17 00:00:00 2001 From: Erik Kristofer Anderson Date: Sat, 21 May 2022 16:17:58 -0500 Subject: [PATCH 3/7] update .gitignore to ignore __pycach__/ --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 4c49bd7..d50a09f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .env +__pycache__/ From ea76b5ed6ab714dce225529735b680dec155143e Mon Sep 17 00:00:00 2001 From: Erik Kristofer Anderson Date: Sat, 21 May 2022 16:18:14 -0500 Subject: [PATCH 4/7] add bearer-token --- .env-example | 1 + load_env.py | 3 ++- main.py | 6 +++--- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.env-example b/.env-example index 15addac..8f57a2d 100644 --- a/.env-example +++ b/.env-example @@ -2,3 +2,4 @@ API_KEY=something API_SECRET=something ACCESS_TOKEN=something ACCESS_TOKEN_SECRET=something +BEARER_TOKEN=something diff --git a/load_env.py b/load_env.py index 7bebf83..8b6e511 100644 --- a/load_env.py +++ b/load_env.py @@ -9,4 +9,5 @@ def load_twitter_env(): consumer_secret = config["API_SECRET"] access_token = config["ACCESS_TOKEN"] access_token_secret = config["ACCESS_TOKEN_SECRET"] - return consumer_key, consumer_secret, access_token, access_token_secret + bearer_token = config["BEARER_TOKEN"] + return consumer_key, consumer_secret, access_token, access_token_secret, bearer_token diff --git a/main.py b/main.py index f42b0aa..eb8fc5c 100644 --- a/main.py +++ b/main.py @@ -6,10 +6,10 @@ import tweepy # Loading twitter credentials -consumer_key, consumer_secret, access_token, access_token_secret = load_twitter_env() +consumer_key, consumer_secret, access_token, access_token_secret, bearer_token = load_twitter_env() # Connect to the TWITTER Client -client = tweepy.Client(bearer_token='enter-token', +client = tweepy.Client(bearer_token=bearer_token, consumer_key=consumer_key, consumer_secret=consumer_secret, access_token=access_token, @@ -24,8 +24,8 @@ def reminder(): remind = line.strip() # Random reminder tweets - # client.create_tweet(text=remind) print(remind) + client.create_tweet(text=remind) # sleeps for 4 hours sleep(14400) From 7fda952da9cb6963a9dfc2f22ad22c6f48aeb7f6 Mon Sep 17 00:00:00 2001 From: Erik Kristofer Anderson Date: Sun, 22 May 2022 11:55:18 -0500 Subject: [PATCH 5/7] change line processing logic --- main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index eb8fc5c..2f89c8f 100644 --- a/main.py +++ b/main.py @@ -19,7 +19,8 @@ def reminder(): with open('reminders.txt') as f: lines = f.readlines() # Select a random line from the reminders file. - lines = [line[3:] for line in lines if len(line) > 2 and line[2] == "-"] + lines = [line.strip() for line in lines if line.strip()] + lines = [line[2:] for line in lines if line[0] == '-'] line = lines[random.randint(0, len(lines) - 1)] remind = line.strip() From 7062828602ad5c5ca2ddce75674f3842a43f2255 Mon Sep 17 00:00:00 2001 From: Erik Kristofer Anderson Date: Mon, 23 May 2022 14:58:19 -0500 Subject: [PATCH 6/7] refactor to use yaml --- main.py | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/main.py b/main.py index 2f89c8f..d5ba6b2 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,15 @@ # Task: Open reminders.txt, which contains a list of reminders, and print a random one out. -import os from load_env import load_twitter_env # function for loading keys! from time import sleep import random import tweepy +from yaml import load, dump +try: + from yaml import CLoader as Loader +except ImportError: + from yaml import Loader + # Loading twitter credentials consumer_key, consumer_secret, access_token, access_token_secret, bearer_token = load_twitter_env() @@ -14,21 +19,26 @@ consumer_secret=consumer_secret, access_token=access_token, access_token_secret=access_token_secret) -def reminder(): - while True: - with open('reminders.txt') as f: lines = f.readlines() - - # Select a random line from the reminders file. - lines = [line.strip() for line in lines if line.strip()] - lines = [line[2:] for line in lines if line[0] == '-'] - line = lines[random.randint(0, len(lines) - 1)] - remind = line.strip() - - # Random reminder tweets - print(remind) - client.create_tweet(text=remind) - # sleeps for 4 hours - sleep(14400) + + +def remind(): + while True: + with open('reminders.txt') as f: + categories = load(f.read(), Loader=Loader) + + reminders_to_print = [] + for subcategory in categories.values(): + for reminders in subcategory.values(): + for reminder_raw_text in reminders: + reminders_to_print.append(reminder_raw_text.strip()) + + # Random reminder tweets + reminder = random.choice(reminders_to_print) + print(reminder) + client.create_tweet(text=reminder) + # sleeps for 4 hours + sleep(14400) + if __name__ == "__main__": - reminder() + remind() From 7d24446ee2d3bc2835bae397063b225d63147570 Mon Sep 17 00:00:00 2001 From: Erik Kristofer Anderson Date: Mon, 23 May 2022 15:00:22 -0500 Subject: [PATCH 7/7] include PyYAML in requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 2b2a797..9c943c6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ python-dotenv==0.20.0 tweepy~=4.0 +PyYAML==6.0 \ No newline at end of file