Define base of script

This commit is contained in:
Tristan Le Chanony 2020-01-03 07:38:36 +01:00
parent e45ce2aef7
commit ce5b17a99a
2 changed files with 70 additions and 0 deletions

17
drop-account.py Normal file
View File

@ -0,0 +1,17 @@
#!/usr/bin/env python3
# coding: utf-8
# -*- coding: utf-8 -*-
# A Fediverse (decentralized social network, for instance using Mastodon) bot
from mastodon import StreamListener
from logging.handlers import RotatingFileHandler
from pprint import pprint
from utils.config import get_parameter, init_log, init_mastodon
import requests, os, random, sys, time, json, logging, argparse, re, shutil
config_file = "config.txt"
secrets_filepath = get_parameter("secrets_filepath", config_file)
log_filepath = get_parameter("log_filepath", config_file)
log = init_log(log_filepath)
mastodon = init_mastodon(config_file, secrets_filepath)

53
utils/config.py Normal file
View File

@ -0,0 +1,53 @@
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