Un bot qui récupère une image random en local et la publie
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

52 lines
1.7 KiB

from mastodon import Mastodon
from logging.handlers import RotatingFileHandler
import requests, os, random, sys, time, json, logging
def get_parameter( parameter, file_path ):
# Check if secrets file exists
if not os.path.isfile(file_path):
log.error("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
log.critical(file_path + " Missing parameter %s "%parameter)
sys.exit(0)
def init_log(log_filepath):
log = logging.getLogger()
log.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')
file_handler = RotatingFileHandler(log_filepath, 'a', 1000000, 1)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(formatter)
log.addHandler(file_handler)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
stream_handler.setFormatter(formatter)
log.addHandler(stream_handler)
return log
def init_mastodon(config, secrets):
uc_client_id = get_parameter("client_id", secrets)
uc_client_secret = get_parameter("client_secret", secrets)
uc_access_token = get_parameter("access_token", secrets)
mastodon_hostname = get_parameter("mastodon_hostname", secrets)
mastodon = Mastodon(
client_id = uc_client_id,
client_secret = uc_client_secret,
access_token = uc_access_token,
api_base_url = 'https://' + mastodon_hostname,
)
return mastodon