61 lines
2.3 KiB
Python
61 lines
2.3 KiB
Python
#!/usr/bin/python3
|
|
|
|
"""Bot that posts a link to a random szurubooru image"""
|
|
|
|
import argparse
|
|
import random
|
|
import requests
|
|
import ipfshttpclient
|
|
from misskey import Misskey
|
|
from misskey import NoteVisibility
|
|
import pyszuru
|
|
|
|
# arguments
|
|
parser = argparse.ArgumentParser(prog='misskey-szuru-bot',
|
|
description='Bot that posts a link to a random szurubooru image')
|
|
|
|
parser.add_argument('-i', '--instance', metavar='instance', type=str,
|
|
help='Domain of misskey instance i.e: misskey.io', required=True)
|
|
parser.add_argument('-t', '--token', metavar='token', type=str,
|
|
help='Token used for posting to misskey instance', required=True)
|
|
parser.add_argument('-b', '--booru', metavar='booru', type=str,
|
|
help='Domain of szurubooru instance to query i.e: szurubooru.com', required=True)
|
|
parser.add_argument('-u', '--username', metavar='username', type=str,
|
|
help='Username for szurubooru account', required=True)
|
|
parser.add_argument('-a', '--api-key', metavar='api_key', type=str,
|
|
help='API key for szurubooru account', required=True)
|
|
parser.add_argument('--ipfs', metavar='ipfs', type=str,
|
|
help='connection string for an ipfs instance, like /dns/ipfs-api.example.com/tcp/443/https')
|
|
parser.add_argument('--ipfs-username', metavar='ipfs_username', type=str,
|
|
help='username to connect to ipfs instance')
|
|
parser.add_argument('--ipfs-password', metavar='ipfs_password', type=str,
|
|
help='username to connect to ipfs instance')
|
|
|
|
args = parser.parse_args()
|
|
|
|
# get truly random post
|
|
booru = pyszuru.API(f"https://{args.booru}", username=args.username, token=args.api_key)
|
|
highest_post = next(booru.search_post("sort:id", page_size=1))
|
|
post = booru.getPost(random.randint(0, highest_post.id_))
|
|
|
|
# compose note
|
|
text = f"link: {post.content}"
|
|
if post.safety == "unsafe":
|
|
text += " (nsfw)"
|
|
|
|
# get ipfs hash if ipfs is available
|
|
if args.ipfs is not None:
|
|
with ipfshttpclient.connect(args.ipfs, auth=(args.ipfs_username, args.ipfs_password)) as client:
|
|
r = requests.get(post.content, allow_redirects=True, timeout=60)
|
|
FILE_HASH = client.add_bytes(r.content)
|
|
text += f"\nipfs: ipfs://{FILE_HASH}"
|
|
text += f" [(gateway)](https://ipfs.bubbletea.dev/ipfs/{FILE_HASH})"
|
|
|
|
text += f"\nsource: https://{args.booru}/post/{post.id_}"
|
|
|
|
# post a note :D
|
|
Misskey(args.instance, i=args.token).notes_create(
|
|
text=text,
|
|
visibility=NoteVisibility.HOME
|
|
)
|