add ipfs link support
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
shibao 2023-09-18 19:40:10 -04:00
parent 44575f17b1
commit efe1584ab2
3 changed files with 28 additions and 8 deletions

View File

@ -1 +1 @@
0 * * * * cd /usr/src/app && /usr/local/bin/python misskey-szuru-bot.py --instance misskey.io --booru szurubooru.com --username my-username --apiKey my-api-key --token token >/proc/1/fd/1 2>&1 0 * * * * cd /usr/src/app && /usr/local/bin/python misskey_szuru_bot.py --instance misskey.io --booru szurubooru.com --username my-username --api-key my-api-key --token token --ipfs /dns/ipfs.bubbletea.dev/tcp/443/https --ipfs-username=api-username --ipfs-password=api-password >/proc/1/fd/1 2>&1

View File

@ -1,10 +1,14 @@
#!/usr/bin/python3 #!/usr/bin/python3
"""Bot that posts a link to a random szurubooru image"""
import argparse import argparse
import pyszuru
import requests
import random import random
import requests
import ipfshttpclient
from misskey import Misskey from misskey import Misskey
from misskey import NoteVisibility from misskey import NoteVisibility
import pyszuru
# arguments # arguments
parser = argparse.ArgumentParser(prog='misskey-szuru-bot', parser = argparse.ArgumentParser(prog='misskey-szuru-bot',
@ -18,13 +22,19 @@ parser.add_argument('-b', '--booru', metavar='booru', type=str,
help='Domain of szurubooru instance to query i.e: szurubooru.com', required=True) help='Domain of szurubooru instance to query i.e: szurubooru.com', required=True)
parser.add_argument('-u', '--username', metavar='username', type=str, parser.add_argument('-u', '--username', metavar='username', type=str,
help='Username for szurubooru account', required=True) help='Username for szurubooru account', required=True)
parser.add_argument('-a', '--apiKey', metavar='apiKey', type=str, parser.add_argument('-a', '--api-key', metavar='api_key', type=str,
help='API key for szurubooru account', required=True) 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() args = parser.parse_args()
# get truly random post # get truly random post
booru = pyszuru.API(f"https://{args.booru}", username=args.username, token=args.apiKey) 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)) highest_post = next(booru.search_post("sort:id", page_size=1))
post = booru.getPost(random.randint(0, highest_post.id_)) post = booru.getPost(random.randint(0, highest_post.id_))
@ -32,6 +42,15 @@ post = booru.getPost(random.randint(0, highest_post.id_))
text = f"link: {post.content}" text = f"link: {post.content}"
if post.safety == "unsafe": if post.safety == "unsafe":
text += " (nsfw)" 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_}" text += f"\nsource: https://{args.booru}/post/{post.id_}"
# post a note :D # post a note :D

View File

@ -1,3 +1,4 @@
Misskey.py ipfshttpclient==0.8.0a2
requests Misskey.py==4.1.0
pyszuru pyszuru==0.3.1
Requests==2.31.0