Rewrite from python to bash

This commit is contained in:
NaiJi ✨ 2022-04-15 13:52:22 +03:00
parent 35e930fbd7
commit 89c3eff9d4
2 changed files with 46 additions and 48 deletions

48
post.py
View File

@ -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())

46
post.sh Normal file
View File

@ -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"