Define base of script
This commit is contained in:
parent
e45ce2aef7
commit
ce5b17a99a
|
@ -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)
|
||||
|
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue