Here's the link to the repository for my code. Here's the main file that has the issue if you don't wanna gander into the rest of the repo.
import yaml
from twitter import *
from datetime import datetime
import sys
import time
from flask import Flask
app = Flask(__name__)
# sys.path.append(".")
#the part where you figure out how many days are left
def calculate_dates():
now = datetime.now()
gradDay = datetime(2020, 5, 15)
return (gradDay - now.today()).days + 1
# new_status = 'happy ' + str(calculate_dates()) + ' days until grad'
new_status = 'test3'
with open("config.yml", 'r') as ymlfile:
cfg = yaml.safe_load(ymlfile)
#this is the method i used when it was just the flask app running locally
# while True:
# if (datetime.now().hour is 9 and datetime.now().minute is 0 and datetime.now().second is 0):
# twitter = Twitter(auth = OAuth(cfg['twitterkeys']['access_token_key'],
# cfg['twitterkeys']['access_token_secret'],
# cfg['twitterkeys']['consumer_key'],
# cfg['twitterkeys']['consumer_secret']))
# results = twitter.statuses.update(status = new_status)
# time.sleep(5)
# # print("updated status: %s" % new_status)
@app.route("/")
def main():
return "hello"
@app.route("/tweet")
def tweet():
twitter = Twitter(auth = OAuth(cfg['twitterkeys']['access_token_key'],
cfg['twitterkeys']['access_token_secret'],
cfg['twitterkeys']['consumer_key'],
cfg['twitterkeys']['consumer_secret']))
results = twitter.statuses.update(status = new_status)
if __name__ == '__main__':
app.run(host='127.0.0.1', port=5000, debug=True)
Until now, I've been tweeting how many days are left until I graduate manually every day just for fun (and to remind myself to have fun before I graduate), and a friend suggested I automate it. At first, I was running the Flask app on my computer and had something in my code that would just tweet out the days left every day at 9am, but I wanted to put it on GCP so I wouldn't have to run it locally forever. When I ran the Flask app locally, everything was fine, and it'll tweet it out.
Now, I have set up this Python Flask app so that when traveling to the /tweet route, it'll tweet out how many days are left until graduation at Virginia Tech. On GCP, I've made a cron job that'll travel to /tweet out at the right time to send out the tweet (currently in the repo it's not set to 9am because I was testing it out, and that's when I found the error).
When the time for execution for the cron job comes around, I get an error that it failed.
NameError: name 'Twitter' is not defined
at tweet (/srv/main.py:38)
at dispatch_request (/env/lib/python3.7/site-packages/flask/app.py:1935)
at full_dispatch_request (/env/lib/python3.7/site-packages/flask/app.py:1949)
at reraise (/env/lib/python3.7/site-packages/flask/_compat.py:39)
at handle_user_exception (/env/lib/python3.7/site-packages/flask/app.py:1820)
at full_dispatch_request (/env/lib/python3.7/site-packages/flask/app.py:1951)
at wsgi_app (/env/lib/python3.7/site-packages/flask/app.py:2446)
Originally, I had my virtual environment named "twitter" so through another thread online about a similar issue, I thought it might've confused the twitter package with something in there. Renaming it didn't help. I tried reinstalling the twitter package which also didn't help. Not sure what to try out next. I don't think it's anything to do with how I set up Flask because if I run this version locally and travel to /tweet, it sends out a tweet.
Any suggestions?
Edit: I realized I hadn't followed the "pip install -t lib -r requirements.txt" command to copy the libraries into a lib folder, just tried it and it didn't change my error.
P.S. it's 69 days until grad