maid-bot/post.py

49 lines
1.3 KiB
Python
Raw Normal View History

2022-03-16 23:02:53 +01:00
#!/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"
2022-03-20 21:21:54 +01:00
ERROR_FILE = "errors.log"
2022-03-16 23:02:53 +01:00
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'
2022-03-20 21:21:54 +01:00
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
2022-03-16 23:02:53 +01:00
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())