complete overhaul of the bot
This commit is contained in:
48
.archive/README.md
Normal file
48
.archive/README.md
Normal file
@ -0,0 +1,48 @@
|
||||
# @quotesbot
|
||||
|
||||
This is the code that posts to https://bots.fyrfli.social/@quotesbot.
|
||||
|
||||
## Requirements:
|
||||
- some knowledge of the linux command line
|
||||
- python installed
|
||||
- a Mastodon-compatible API token for your instance
|
||||
- a json-formatted file of quotes to use formatted thus:
|
||||
```
|
||||
[
|
||||
{
|
||||
"quote": "quote",
|
||||
"author": "author"
|
||||
},
|
||||
...
|
||||
{
|
||||
"quote": "quote",
|
||||
"author": "author"
|
||||
}
|
||||
]
|
||||
```
|
||||
Your quotes file can either be in the local directory or accessible at a url.
|
||||
|
||||
## To install:
|
||||
|
||||
- Download the release package
|
||||
- Extract into your chosen directory:
|
||||
- `mkdir quotesbot && cd quotesbot`
|
||||
- `tar xfz | gunzip source.[zip|tar.gz]`
|
||||
- If you have other projects, you can create a virtual environment to run this code:
|
||||
- `python3 -m venv .venv`
|
||||
- `source .venv/bin/activate`
|
||||
- `python3 -m pip install -r requirements.txt`
|
||||
- Otherwise, just install from the requirements.txt: `python3 -m pip install -r requirements.txt`
|
||||
- Copy the env.example to .env and change the values to your own
|
||||
|
||||
## To use:
|
||||
- You can either run the code without any arguments: `python3 random_quotes.py` or you can pass **one** argument through to the code to select a specific quote to post:
|
||||
- `python3 random_quotes.py 1`
|
||||
|
||||
## Caveats
|
||||
This is code is *very* rudimentary. It serves a specific purpose:
|
||||
- To post my favourite quotes to my timeline every few hours
|
||||
- To practice my python coding skills
|
||||
|
||||
## Going forward
|
||||
It is possible that I may continue to work on this to a point where it's a more sophisticated module, but I can almost guarantee that there are packages out there that do this and more with far more finesse.
|
3
.archive/env.example
Normal file
3
.archive/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
|
51
.archive/random_quotes.py
Normal file
51
.archive/random_quotes.py
Normal file
@ -0,0 +1,51 @@
|
||||
from json import loads
|
||||
from dotenv import load_dotenv
|
||||
from os import getenv
|
||||
from random import randint
|
||||
from requests import get, post
|
||||
from sys import argv
|
||||
|
||||
def load_env():
|
||||
load_dotenv()
|
||||
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__':
|
||||
if len(argv) > 0:
|
||||
choice = int(argv[1])
|
||||
else:
|
||||
choice = int(0)
|
||||
api_key, quote_file = load_env()
|
||||
headers, payload = build_post(api_key, quote_file, choice)
|
||||
make_post(headers, payload)
|
6
.archive/requirements.txt
Normal file
6
.archive/requirements.txt
Normal file
@ -0,0 +1,6 @@
|
||||
certifi==2024.2.2
|
||||
charset-normalizer==3.3.2
|
||||
idna==3.6
|
||||
python-dotenv==1.0.1
|
||||
requests==2.31.0
|
||||
urllib3==2.2.1
|
Reference in New Issue
Block a user