finish streaming
This commit is contained in:
parent
02b34b8012
commit
16b83665a1
58
bot.py
58
bot.py
|
@ -10,7 +10,14 @@ 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
|
||||
import requests, os, random, sys, time, json, logging, argparse, re
|
||||
|
||||
config_file = "config.txt"
|
||||
secrets_filepath = "secrets/secrets.txt"
|
||||
log_filepath = "activity.log"
|
||||
log = init_log(log_filepath)
|
||||
bot_name = "nosafe"
|
||||
mastodon = init_mastodon(config_file, secrets_filepath)
|
||||
|
||||
def post_img(mastodon, text, visibility, log, config):
|
||||
img_path = get_parameter("img_path", config)
|
||||
|
@ -28,40 +35,45 @@ def post_img(mastodon, text, visibility, log, config):
|
|||
# pass
|
||||
|
||||
media_dict = mastodon.media_post(image_byte, mime)
|
||||
mastodon.status_post("", None, media_ids=[media_dict], sensitive=True, visibility='public', spoiler_text="#NSFW")
|
||||
return media_dict;
|
||||
|
||||
def cleanhtml(raw_html):
|
||||
cleanr = re.compile('<.*?>')
|
||||
cleantext = re.sub(cleanr, '', raw_html)
|
||||
return cleantext
|
||||
|
||||
class BotListener(StreamListener):
|
||||
|
||||
def __init__(self, mastodon, log):
|
||||
self.mastodon = mastodon;
|
||||
self.log = log;
|
||||
self.log.debug("init success")
|
||||
|
||||
def on_notification(self, notification):
|
||||
self.log.debug("notif")
|
||||
|
||||
def on_update(self, status):
|
||||
self.log.debug("update");
|
||||
|
||||
#def handle_heartbeat():
|
||||
# self.log.debug("heartbeat")
|
||||
if notification['type'] == 'mention':
|
||||
log.debug("Got a mention")
|
||||
id = notification['status']['id']
|
||||
sender = notification['account']['acct']
|
||||
visibility = notification['status']['visibility']
|
||||
mentions = notification['status']['mentions']
|
||||
text = "@" + notification['status']["account"]["acct"] + " "
|
||||
for mention in mentions:
|
||||
if mention["acct"] != bot_name:
|
||||
text = text + "@" + mention["acct"] + " "
|
||||
|
||||
media_dict = post_img(mastodon, "NSFW", 1, log, config_file)
|
||||
mastodon.status_post(text, None, media_ids=[media_dict], sensitive=True, visibility=visibility, spoiler_text="#NSFW")
|
||||
else:
|
||||
log.debug("Nevermind")
|
||||
|
||||
|
||||
def main():
|
||||
config_file = "config.txt"
|
||||
secrets_filepath = "secrets/secrets.txt"
|
||||
log_filepath = "activity.log"
|
||||
log = init_log(log_filepath)
|
||||
mastodon = init_mastodon(config_file, secrets_filepath)
|
||||
def main():
|
||||
|
||||
parser = argparse.ArgumentParser(description='Choose between image or streaming')
|
||||
parser.add_argument("-i", "--img", action='store_true', help="post image")
|
||||
parser.add_argument("-s", "--stream", action="store_true", help="stream user profile")
|
||||
args = parser.parse_args()
|
||||
if args.img:
|
||||
post_img(mastodon, "NSFW", 1, log, config_file)
|
||||
media_dict = post_img(mastodon, "NSFW", 1, log, config_file)
|
||||
mastodon.status_post("", None, media_ids=[media_dict], sensitive=True, visibility='public', spoiler_text="#NSFW")
|
||||
sys.exit()
|
||||
elif args.stream:
|
||||
stream = BotListener(mastodon, log);
|
||||
stream = BotListener();
|
||||
while True:
|
||||
try:
|
||||
log.info("Start listening...")
|
||||
|
@ -69,8 +81,6 @@ def main():
|
|||
except Exception as error:
|
||||
log.warning('General exception caught: ' + str(error))
|
||||
time.sleep(0.5)
|
||||
|
||||
|
||||
else:
|
||||
print("Require an argument. Use --help to display help")
|
||||
|
||||
|
|
|
@ -21,16 +21,17 @@ def get_parameter( parameter, file_path ):
|
|||
|
||||
def init_log(log_filepath):
|
||||
log = logging.getLogger()
|
||||
log.setLevel(logging.ERROR)
|
||||
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.ERROR)
|
||||
file_handler.setLevel(logging.INFO)
|
||||
file_handler.setFormatter(formatter)
|
||||
log.addHandler(file_handler)
|
||||
|
||||
stream_handler = logging.StreamHandler()
|
||||
stream_handler.setLevel(logging.ERROR)
|
||||
stream_handler.setLevel(logging.INFO)
|
||||
stream_handler.setFormatter(formatter)
|
||||
log.addHandler(stream_handler)
|
||||
|
||||
return log
|
||||
|
|
Reference in New Issue