mirror of
https://gitlab.com/2-chainz/2-chainz-twitter-bot.git
synced 2025-08-02 19:41:24 +00:00
gave tweeter its own function
This commit is contained in:
40
tweet/__init__.py
Normal file
40
tweet/__init__.py
Normal file
@@ -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)
|
Reference in New Issue
Block a user