diff --git a/.gitignore b/.gitignore index a10127b..e938af2 100644 --- a/.gitignore +++ b/.gitignore @@ -69,6 +69,7 @@ instance/ # Sphinx documentation docs/_build/ +keys.* # PyBuilder target/ diff --git a/requirements.txt b/requirements.txt index 75db2c4..bc56305 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,3 @@ azure-functions +tweepy==3.8.0 +requests==2.23.0 diff --git a/tweet/__init__.py b/tweet/__init__.py new file mode 100644 index 0000000..57bc6e8 --- /dev/null +++ b/tweet/__init__.py @@ -0,0 +1,39 @@ +import datetime +import logging +import tweepy +from tweepy import TweepError +import azure.functions as func +import requests as r +from . import keys + + +def main(mytimer: func.TimerRequest) -> None: + # Authenticate to Twitter + auth = tweepy.OAuthHandler(keys.keys["API"], keys.keys["API_SECRET"]) + auth.set_access_token( + keys.keys["TOKEN"], keys.keys["TOKEN_SECRET"], + ) + + # Create API object + api = tweepy.API(auth) + tweeted = False + tries = 0 + while tweeted == False and tries < 10: + try: + quote = r.get("https://api.chainz.rest/quote").json()["quote"] + alias = r.get("https://api.chainz.rest/alias").json()["alias"] + + api.update_status(status=f"{quote} - {alias}\n") + tweeted = True + except TweepError: + tries += 1 + logging.warning(f"{quote} - {alias} - Has already been tweeted") + + utc_timestamp = ( + datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat() + ) + + if mytimer.past_due: + logging.info("The timer is past due!") + + logging.info("Python timer trigger function ran at %s", utc_timestamp) diff --git a/tweet/function.json b/tweet/function.json new file mode 100644 index 0000000..1b6cc18 --- /dev/null +++ b/tweet/function.json @@ -0,0 +1,12 @@ +{ + "scriptFile": "__init__.py", + "bindings": [ + { + "name": "mytimer", + "type": "timerTrigger", + "direction": "in", + "schedule": "0 0 * * * 1", + "runOnStartup": true + } + ] +} diff --git a/tweet/readme.md b/tweet/readme.md new file mode 100644 index 0000000..f02f92e --- /dev/null +++ b/tweet/readme.md @@ -0,0 +1,10 @@ +You need to add your twitter api keys to a `keys.py` file in this directory with the following format: + +```python +keys = { + "API": "API_KEY_HERE", + "API_SECRET": "API_SECRET_KEY_HERE", + "TOKEN": "TOKEN_KEY_HERE", + "TOKEN_SECRET": "TOKEN_SECRET_KEY_HERE", +} +```