quotesbot/app.py

22 lines
758 B
Python

from requests import get, post
from json import loads, dumps
from os import getenv
from random import randint
import csv, io
post_token = getenv('POST_TOKEN')
post_endpoint = getenv('POST_ENDPOINT')
source_token = getenv('SOURCE_TOKEN')
source_endpoint = getenv('SOURCE_ENDPOINT')
source_object = get(source_endpoint)
if source_object.reason == "OK":
reader = csv.DictReader(io.StringIO(source_object.text))
source_data = dumps(list(reader))
choices = loads(source_data)
print(choices[0])
choice = choices[randint(0, len(choices) - 1)]
headers = {'Authorization': 'Bearer ' + post_token}
payload = {'status': f"{choice['quote']}\n\n - {choice['author']}", 'privacy': 'public'}
response = post(post_endpoint, headers=headers, data=payload)