commit 153a5ea20197a415fe917647cba29e46c43d224d Author: Anson Date: Thu Apr 23 21:08:07 2020 -0700 gave tweeter its own function diff --git a/.funcignore b/.funcignore new file mode 100644 index 0000000..414df2f --- /dev/null +++ b/.funcignore @@ -0,0 +1,4 @@ +.git* +.vscode +local.settings.json +test \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e938af2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,131 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +keys.* +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don’t work, or not +# install all needed dependencies. +#Pipfile.lock + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# Azure Functions artifacts +bin +obj +appsettings.json +local.settings.json +.python_packages \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..cbbad0f --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + "recommendations": [ + "ms-azuretools.vscode-azurefunctions", + "ms-python.python" + ] +} diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..0e43bd3 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,12 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Attach to Python Functions", + "type": "python", + "request": "attach", + "port": 9091, + "preLaunchTask": "func: host start" + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b8dad2e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "azureFunctions.deploySubpath": ".", + "azureFunctions.scmDoBuildDuringDeployment": true, + "azureFunctions.projectLanguage": "Python", + "azureFunctions.projectRuntime": "~3", + "debug.internalConsoleOptions": "neverOpen" +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..13be064 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,11 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "func", + "command": "host start", + "problemMatcher": "$func-watch", + "isBackground": true + } + ] +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8f7cead --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 2 Chainz REST API + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..46fe7c7 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# 2 Chainz Rest + diff --git a/host.json b/host.json new file mode 100644 index 0000000..67f179d --- /dev/null +++ b/host.json @@ -0,0 +1,8 @@ +{ + "version": "2.0", + "extensionBundle": { + "id": "Microsoft.Azure.Functions.ExtensionBundle", + "version": "[1.*, 2.0.0)" + }, + "extensions": { "http": { "routePrefix": "" } } +} diff --git a/proxies.json b/proxies.json new file mode 100644 index 0000000..b385252 --- /dev/null +++ b/proxies.json @@ -0,0 +1,4 @@ +{ + "$schema": "http://json.schemastore.org/proxies", + "proxies": {} +} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..bc56305 --- /dev/null +++ b/requirements.txt @@ -0,0 +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..a71778f --- /dev/null +++ b/tweet/__init__.py @@ -0,0 +1,40 @@ +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 + tries += 10 + 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..0033971 --- /dev/null +++ b/tweet/function.json @@ -0,0 +1,12 @@ +{ + "scriptFile": "__init__.py", + "bindings": [ + { + "name": "mytimer", + "type": "timerTrigger", + "direction": "in", + "schedule": "0 0 10 * * MON", + "runOnStartup": false + } + ] +} 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", +} +```