Files
bots/dog_facts.py

32 lines
962 B
Python

from requests import get, post
from json import loads
from os import getenv
from collections import namedtuple as nt
from globals import *
def notify(priority, message):
r = post("https://api.pushover.net/1/messages.json", data = {
"token": po_bots_token,
"user": po_user_token,
"message": "dog facts: " + message,
"priority": priority
})
post_endpoint = f"{bots_endpoint}/api/v1/statuses"
api_url = 'https://dogapi.dog/api/v2/facts'
headers = {
"Authorization": f"Bearer {dogfacts_token}"
}
response = get(api_url)
if response.reason == "OK":
dog_fact = loads(response.text, object_hook=lambda d: nt('DogFact', d.keys())(*d.values()))
payload = {"status": f"didja know: \n {dog_fact.data[0].attributes.body}\n#RandomDogFacts", "visibility": "public"}
response = post(post_endpoint, headers=headers, json=payload)
if response.reason == "OK":
notify(-1, loads(response.text)['uri'])
else:
notify(1, response.reason)