Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor main to use YAML parser and test with personal twitter bot #50

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
API_KEY=something
API_SECRET=something
ACCESS_TOKEN=something
ACCESS_TOKEN_SECRET=something
BEARER_TOKEN=something
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
__pycache__/
15 changes: 8 additions & 7 deletions load_env.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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")
return consumer_key, consumer_secret, access_token, 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"]
bearer_token = config["BEARER_TOKEN"]
return consumer_key, consumer_secret, access_token, access_token_secret, bearer_token
59 changes: 35 additions & 24 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
# 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 = 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)

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] == "-"]
line = lines[random.randint(0, len(lines) - 1)]
remind = line.strip()

# Random reminder tweets
api.update_status(status=remind)
# sleeps for 4 hours
sleep(14400)
consumer_key, consumer_secret, access_token, access_token_secret, bearer_token = load_twitter_env()

# Connect to the TWITTER Client
client = tweepy.Client(bearer_token=bearer_token,
consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token=access_token,
access_token_secret=access_token_secret)


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()
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
python-dotenv==0.20.0
tweepy==3.10.0
tweepy~=4.0
PyYAML==6.0