From 89c3eff9d42831141bb4f60704e399bc96785924 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Fri, 15 Apr 2022 13:52:22 +0300 Subject: [PATCH] Rewrite from python to bash --- post.py | 48 ------------------------------------------------ post.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 48 deletions(-) delete mode 100755 post.py create mode 100644 post.sh diff --git a/post.py b/post.py deleted file mode 100755 index cc70897..0000000 --- a/post.py +++ /dev/null @@ -1,48 +0,0 @@ -#!/home/naiji/mastodon/maid-bot/venv/bin/python -import requests -import sys -import random -import os -import os.path as op - -from mastodon import Mastodon -from datetime import datetime - -# -------------------------------------------------- - -URL_FILE = "sources.dat" -ERROR_FILE = "errors.log" -def main(): - - mastodon = Mastodon( - access_token = 'token.dat', - api_base_url = 'https://udongein.xyz/' - ) - - with open(URL_FILE, 'r', encoding='utf-8') as file: - data = file.readlines() - - pair = data[random.randint(0, len(data))] - metadata = pair.split() - - print('\nsource:' + metadata[0] + '\nlink to attach:' + metadata[1]) - - img_data = requests.get(metadata[0]).content - #with open('temp_' + metadata[0], 'wb') as handler: - # handler.write(img_data) - - fformat = op.splitext(metadata[0])[1][1:] - if (fformat == 'jpg'): - fformat = 'jpeg' - try: - media = mastodon.media_post(img_data, f'image/{fformat}') - except: - with open(ERROR_FILE, 'a', encoding='utf-8') as file: - data = file.readlines() - return - - toot = f' :azrn_shiratsuyu_maid: \nSource: ' + metadata[1] - mastodon.status_post(toot, media_ids=[media], visibility='unlisted', sensitive=False) - -if __name__ == '__main__': - sys.exit(main()) diff --git a/post.sh b/post.sh new file mode 100644 index 0000000..a092f64 --- /dev/null +++ b/post.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +if [ -z $(which jq) ] +then + echo "Missing jq package, please install" + exit 1 +fi + +token_dat="./token.dat" + +if [ ! -f $token_dat ] +then + echo "Missing ./token.dat" + exit 1 +fi + +sources_dat="./sources.dat" + +if [ ! -f $sources_dat ] +then + echo "Missing ./sources.dat" + exit 1 +fi + +api_base_url="https://udongein.xyz/" +access_token="$(cat ${token_dat})" + +pair_to_post=($(cat ${sources_dat} | shuf -n 1)) +source_url=${pair_to_post[0]} +hyperlink_url=${pair_to_post[1]} + +mkdir source +cd source +wget "${source_url}" 2> /dev/null || (echo "Error with ${source_url}" && exit 1) + +media_json=$(curl -X POST "${api_base_url}/api/v1/media" -H "Authorization: Bearer ${access_token}" -F "file=@`ls *`" -F "description=${source_url}") +media_id=$(jq -r ".id" <<< ${media_json}) + +echo $media_json +cd .. +rm -rf source/ + +curl -X POST -d '{"status":" :azrn_shiratsuyu_maid: '${hyperlink_url}'", "visibility":"'unlisted'", "media_ids":'[\"${media_id}\"]'}' \ + -H "Authorization: Bearer ${access_token}" \ + -H "Content-Type: application/json" \ + "${api_base_url}/api/v1/statuses"