maid-bot/post.py

49 lines
1.3 KiB
Python
Executable File

#!/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())