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 4 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
22 changes: 11 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@
import tweepy

# 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)
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 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] == "-"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like there might be a better solution here.

Maybe check if the first non-whitespace character is the hyphen or not, and if it is, remove all the whitespaces around it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So strip the line of white space and then check if the first character is a hyphen? That would work. But I'm not sure what you mean by "remove all the whitespaces around it". Still, I'll try making an edit.

Alternately, would it be better to use Python's yaml parser?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it might be better to use Python's YAML parser!

line = lines[random.randint(0, len(lines) - 1)]
remind = line.strip()

# Random reminder tweets
api.update_status(status=remind)
print(remind)
client.create_tweet(text=remind)
# sleeps for 4 hours
sleep(14400)

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
python-dotenv==0.20.0
tweepy==3.10.0
tweepy~=4.0