masto-bot/random-image.py

68 lines
2.0 KiB
Python
Executable File

#!/usr/bin/python3
# coding: utf-8
# -*- coding: utf-8 -*-
from PIL import Image
from io import BytesIO
from mastodon import Mastodon
import requests
import os
import sys
import time
import json
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--sex", help="Sexe of image [man | woman ]")
parser.add_argument("--no-upload", help="Don't upload image")
args = parser.parse_args()
def get_parameter( parameter, file_path ):
# Check if secrets file exists
if not os.path.isfile(file_path):
print("File %s not found, exiting."%file_path)
sys.exit(0)
# Find parameter in file
with open( file_path ) as f:
for line in f:
if line.startswith( parameter ):
return line.replace(parameter + ":", "").strip()
# Cannot find parameter, exit
print(file_path + " Missing parameter %s "%parameter)
sys.exit(0)
#https://source.unsplash.com/collection/1053828/1920x1080
if args.sex == "man":
collection = "1523783"
secrets_filepath = "secrets/secrets-man.txt"
elif args.sex == "women":
collection = "1523809"
secrets_filepath = "secrets/secrets-women.txt"
uc_client_id = get_parameter("client_id", secrets_filepath)
uc_client_secret = get_parameter("client_secret", secrets_filepath)
uc_access_token = get_parameter("access_token", secrets_filepath)
mastodon_hostname = get_parameter("mastodon_hostname", secrets_filepath)
mastodon = Mastodon(
client_id = uc_client_id,
client_secret = uc_client_secret,
access_token = uc_access_token,
api_base_url = 'https://' + mastodon_hostname,
)
response = requests.get("https://source.unsplash.com/collection/"+collection+"/")
pattern = Image.open(BytesIO(response.content), "r").convert('RGB')
pattern.save('output.jpg')
if args.no_upload:
print("don't upload this image")
else:
media_dict = mastodon.media_post("output.jpg")
#text.encode('utf-8').strip()
mastodon.status_post("Image du site Unsplash", in_reply_to_id=None, media_ids=[media_dict])