initial
This commit is contained in:
commit
7a9a41ccc9
3
env.example
Normal file
3
env.example
Normal file
@ -0,0 +1,3 @@
|
||||
QUOTES_ACCESS_TOKEN=your_api_access_token
|
||||
QUOTES_LIBRARY=link_to_your_quote_library # Can be a URL or a file
|
||||
ENDPOINT=your_api_endpoint_to_post
|
45
random_quotes.py
Normal file
45
random_quotes.py
Normal file
@ -0,0 +1,45 @@
|
||||
from json import loads
|
||||
from dotenv import load_dotenv
|
||||
from os import getenv
|
||||
from random import randint
|
||||
from requests import get, post
|
||||
|
||||
def load_env():
|
||||
api_key = getenv('QUOTES_ACCESS_TOKEN')
|
||||
quotes_library = getenv('QUOTES_LIBRARY')
|
||||
if quotes_library.startswith('https',0):
|
||||
quote_file = loads(get(quotes_library).text)
|
||||
else:
|
||||
quote_file = loads(open(quotes_library).read())
|
||||
return api_key, quote_file
|
||||
|
||||
def build_post(api_key, quote_file, choice=0):
|
||||
if choice == 0:
|
||||
rand = randint(0, len(quote_file) - 1)
|
||||
else:
|
||||
rand = choice
|
||||
quote = quote_file[rand]['quote']
|
||||
author = quote_file[rand]['author']
|
||||
toot = "'" + quote + "'\n\n - " + author + "\n\n#RandomQuote #quotes #quote #bot"
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Bearer": api_key,
|
||||
"Authorization": "Bearer " + api_key
|
||||
}
|
||||
payload = {
|
||||
"status": toot,
|
||||
"visibility": "public"
|
||||
}
|
||||
return headers, payload
|
||||
|
||||
def make_post(headers, payload):
|
||||
post_endpoint = getenv('ENDPOINT')
|
||||
try:
|
||||
post(post_endpoint, json=payload, headers=headers)
|
||||
except Exception as e:
|
||||
print('An error occurred: ', e)
|
||||
|
||||
if __name__ == '__main__':
|
||||
api_key, quote_file = load_env()
|
||||
headers, payload = build_post(api_key, quote_file)
|
||||
make_post(headers, payload)
|
Loading…
Reference in New Issue
Block a user