From efe1584ab2c4528bcd5dd07aed0ef9d25b0e8980 Mon Sep 17 00:00:00 2001 From: shibao Date: Mon, 18 Sep 2023 19:40:10 -0400 Subject: [PATCH] add ipfs link support --- crontab.example | 2 +- misskey-szuru-bot.py => misskey_szuru_bot.py | 27 +++++++++++++++++--- requirements.txt | 7 ++--- 3 files changed, 28 insertions(+), 8 deletions(-) rename misskey-szuru-bot.py => misskey_szuru_bot.py (58%) diff --git a/crontab.example b/crontab.example index 7e5b9a3..1dcabb5 100644 --- a/crontab.example +++ b/crontab.example @@ -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 diff --git a/misskey-szuru-bot.py b/misskey_szuru_bot.py similarity index 58% rename from misskey-szuru-bot.py rename to misskey_szuru_bot.py index 8b316c3..585cc89 100644 --- a/misskey-szuru-bot.py +++ b/misskey_szuru_bot.py @@ -1,10 +1,14 @@ #!/usr/bin/python3 + +"""Bot that posts a link to a random szurubooru image""" + import argparse -import pyszuru -import requests import random +import requests +import ipfshttpclient from misskey import Misskey from misskey import NoteVisibility +import pyszuru # arguments 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) parser.add_argument('-u', '--username', metavar='username', type=str, 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) +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.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)) 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}" 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 diff --git a/requirements.txt b/requirements.txt index 802c02f..3f7e8fa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ -Misskey.py -requests -pyszuru +ipfshttpclient==0.8.0a2 +Misskey.py==4.1.0 +pyszuru==0.3.1 +Requests==2.31.0