From a1f37805d35c51f22bb77d3e9c159405277115d6 Mon Sep 17 00:00:00 2001 From: shibao Date: Thu, 15 Dec 2022 19:31:12 -0500 Subject: [PATCH] initial commit --- .dockerignore | 6 ++++++ .drone.yml | 44 ++++++++++++++++++++++++++++++++++++++++++++ .gitignore | 1 + .tool-versions | 1 + Dockerfile | 9 +++++++++ crontab.example | 1 + docker-compose.yml | 9 +++++++++ entrypoint.sh | 4 ++++ misskey-szuru-bot.py | 37 +++++++++++++++++++++++++++++++++++++ requirements.txt | 3 +++ 10 files changed, 115 insertions(+) create mode 100644 .dockerignore create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 .tool-versions create mode 100644 Dockerfile create mode 100644 crontab.example create mode 100644 docker-compose.yml create mode 100644 entrypoint.sh create mode 100644 misskey-szuru-bot.py create mode 100644 requirements.txt diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..254e5c6 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +.drone.yml +.gitignore +.tool-versions +crontab +crontab.example +docker-compose.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..ded925f --- /dev/null +++ b/.drone.yml @@ -0,0 +1,44 @@ +kind: pipeline +type: docker +name: misskey-szuru-bot + +steps: +- name: build and publish stable + image: thegeeklab/drone-docker-buildx + privileged: true + settings: + repo: shibaobun/misskey-szuru-bot + purge: true + compress: true + platforms: linux/amd64,linux/arm/v7 + username: + from_secret: docker_username + password: + from_secret: docker_password + tags: latest + when: + branch: + - stable + +- name: build and publish tagged version + image: thegeeklab/drone-docker-buildx + privileged: true + settings: + repo: shibaobun/misskey-szuru-bot + purge: true + compress: true + platforms: linux/amd64,linux/arm/v7 + username: + from_secret: docker_username + password: + from_secret: docker_password + tags: + - ${DRONE_TAG} + when: + event: + - tag + +volumes: + - name: docker_sock + host: + path: /var/run/docker.sock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..78e21d1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +crontab diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..bc91fdd --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +python 3.11.1 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f2fc65b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.11.1-alpine3.17 + +WORKDIR /usr/src/app +COPY . . + +RUN pip install -r requirements.txt + +RUN chmod +x entrypoint.sh +CMD ["entrypoint.sh"] diff --git a/crontab.example b/crontab.example new file mode 100644 index 0000000..70220c5 --- /dev/null +++ b/crontab.example @@ -0,0 +1 @@ +0 * * * * python misskey-szuru-bot.py --instance misskey.io --token token_here --booru szurubooru.com --username my-username --apiKey szuru-login-token --query "sort:random" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..44f152e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: "3" + +services: + misskey-szuru-bot: + image: shibaobun/misskey-szuru-bot + container_name: misskey-szuru-bot + restart: always + volumes: + - ./crontab:/usr/src/app/crontab diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..9394211 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env sh +set -ou pipefail + +crontab crontab && crond -f diff --git a/misskey-szuru-bot.py b/misskey-szuru-bot.py new file mode 100644 index 0000000..e2f9f99 --- /dev/null +++ b/misskey-szuru-bot.py @@ -0,0 +1,37 @@ +#!/usr/bin/python3 +import argparse +import pyszuru +import requests +from misskey import Misskey + +# 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', '--apiKey', metavar='apiKey', type=str, + help='API key for szurubooru account', required=True) +parser.add_argument('-q', '--query', metavar='query', type=str, + help='Query to search szurubooru', default="sort:random") + +args = parser.parse_args() + +# get post +booru = pyszuru.API(f"https://{args.booru}", username=args.username, token=args.apiKey) +post = next(booru.search_post(args.query)) + +# compose note +text = f"link: {post.content}" +if post.safety == "unsafe": + text += " (nsfw)" +text += f"source: https://{args.booru}/post/{post.id_}" + +# post a note :D +Misskey(args.instance, i=args.token).notes_create(text=text) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..802c02f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +Misskey.py +requests +pyszuru