diff --git a/TootHTMLParser.py b/TootHTMLParser.py new file mode 100644 index 0000000..df3e5e3 --- /dev/null +++ b/TootHTMLParser.py @@ -0,0 +1,20 @@ +from html.parser import HTMLParser + +class TootHTMLParser(HTMLParser): + def __init__(self): + super().__init__() + self.txt = "" + def handle_data(self, data): + self.txt += str(data).lstrip().rstrip().lower() + " " +# +# +# content = "" +# with open("input") as f: +# content = f.readlines() +# content = set(content) +# parser = TootHTMLParser() +# for word in content: +# parser.feed(word) +# with open("output", "w+") as f: +# f.write(parser.txt) + diff --git a/TootHTMLParser.pyc b/TootHTMLParser.pyc new file mode 100644 index 0000000..f3f8a75 Binary files /dev/null and b/TootHTMLParser.pyc differ diff --git a/__pycache__/Log.cpython-35.pyc b/__pycache__/Log.cpython-35.pyc new file mode 100644 index 0000000..d3a6117 Binary files /dev/null and b/__pycache__/Log.cpython-35.pyc differ diff --git a/__pycache__/TootHTMLParser.cpython-35.pyc b/__pycache__/TootHTMLParser.cpython-35.pyc new file mode 100644 index 0000000..e4c4c6e Binary files /dev/null and b/__pycache__/TootHTMLParser.cpython-35.pyc differ diff --git a/activity.log b/activity.log new file mode 100644 index 0000000..6e18d39 --- /dev/null +++ b/activity.log @@ -0,0 +1,23 @@ + +2018-05-16 13:24:47,830 :: DEBUG :: Starting new HTTPS connection (1): miaou.drycat.fr +2018-05-16 13:24:47,875 :: DEBUG :: https://miaou.drycat.fr:443 "GET /api/v1/instance/ HTTP/1.1" 200 None +2018-05-16 13:25:56,391 :: DEBUG :: Starting new HTTPS connection (1): miaou.drycat.fr +2018-05-16 13:25:56,444 :: DEBUG :: https://miaou.drycat.fr:443 "GET /api/v1/instance/ HTTP/1.1" 200 None +2018-05-16 13:27:04,797 :: DEBUG :: Starting new HTTPS connection (1): miaou.drycat.fr +2018-05-16 13:27:04,870 :: DEBUG :: https://miaou.drycat.fr:443 "GET /api/v1/instance/ HTTP/1.1" 200 None +2018-05-16 13:27:12,527 :: DEBUG :: Starting new HTTPS connection (1): miaou.drycat.fr +2018-05-16 13:27:12,584 :: DEBUG :: https://miaou.drycat.fr:443 "GET /api/v1/instance/ HTTP/1.1" 200 None +2018-05-16 13:27:18,744 :: DEBUG :: Starting new HTTPS connection (1): miaou.drycat.fr +2018-05-16 13:27:18,807 :: DEBUG :: https://miaou.drycat.fr:443 "GET /api/v1/instance/ HTTP/1.1" 200 None +2018-05-16 13:28:19,151 :: DEBUG :: Starting new HTTPS connection (1): miaou.drycat.fr +2018-05-16 13:28:19,324 :: DEBUG :: https://miaou.drycat.fr:443 "GET /api/v1/instance/ HTTP/1.1" 200 None +2018-05-16 13:28:26,505 :: DEBUG :: Starting new HTTPS connection (1): miaou.drycat.fr +2018-05-16 13:28:26,565 :: DEBUG :: https://miaou.drycat.fr:443 "GET /api/v1/instance/ HTTP/1.1" 200 None +2018-05-16 13:28:30,291 :: DEBUG :: Starting new HTTPS connection (1): miaou.drycat.fr +2018-05-16 13:28:30,349 :: DEBUG :: https://miaou.drycat.fr:443 "GET /api/v1/instance/ HTTP/1.1" 200 None +2018-05-16 13:28:34,072 :: DEBUG :: Starting new HTTPS connection (1): miaou.drycat.fr +2018-05-16 13:28:34,134 :: DEBUG :: https://miaou.drycat.fr:443 "GET /api/v1/instance/ HTTP/1.1" 200 None +2018-05-16 13:28:42,157 :: DEBUG :: Starting new HTTPS connection (1): miaou.drycat.fr +2018-05-16 13:28:42,224 :: DEBUG :: https://miaou.drycat.fr:443 "GET /api/v1/instance/ HTTP/1.1" 200 None +2018-05-16 13:30:44,355 :: DEBUG :: Starting new HTTPS connection (1): miaou.drycat.fr +2018-05-16 13:30:44,407 :: DEBUG :: https://miaou.drycat.fr:443 "GET /api/v1/instance/ HTTP/1.1" 200 None diff --git a/bot.py b/bot.py index b70af0f..69faba7 100644 --- a/bot.py +++ b/bot.py @@ -3,12 +3,55 @@ # -*- coding: utf-8 -*- from mastodon import Mastodon, StreamListener -import requests, os, random, sys, time, json, logging, socket, multiprocessing, tempfile +from TootHTMLParser import TootHTMLParser +import requests, os, random, sys, time, json, logging, argparse + +from logging.handlers import RotatingFileHandler +from pprint import pprint + +# Mastodon Stream Listener defines functions to react when something happens on Mastodon. We inherit it. +class BotListener(StreamListener): + def __init__(self, mastodon): + print("init") + StreamListener.__init__(self) + self.mastodon = mastodon + pprint(vars(mastodon.stream_user(StreamListener))) + + def handle_mention(self, status): + log.debug('Got a mention!') + + def on_notification(self, notification): + log.debug("here") + for thread in self.threads: + if(not thread.is_alive()): + logging.info("removing thread" + str(thread)) + self.threads.remove(thread) + + #We react to mentions only + if(notification['type'] != 'mention'): + log.info("nevermind, it's not a mention") + return + + #So there is a toot ! + status = notification['status'] + + #And there is a text in this toot. But it is mixed with HTML we don't want so we get rid of it. + content = str(status["content"]) + parser = TootHTMLParser() + parser.feed(content) + content = (parser.txt).lower() + + logging.info(content) + atUser = status["account"]["acct"] + userInfo = self.associateToUser(atUser) + + #If the toot is not in answer to a drawing + answerTo = status["in_reply_to_id"] 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) + log.error("File %s not found, exiting."%file_path) sys.exit(0) # Find parameter in file @@ -18,52 +61,75 @@ def get_parameter( parameter, file_path ): return line.replace(parameter + ":", "").strip() # Cannot find parameter, exit - print(file_path + " Missing parameter %s "%parameter) + log.critical(file_path + " Missing parameter %s "%parameter) sys.exit(0) -def driver(mastodon, log, heartbeat_filename, pid_filename): - try: - listener = myListener(mastodon, log, heartbeat_filename, pid_filename) - log.info("Driver/listener starts, PID %s" % os.getpid()) - mastodon.stream_user(listener) - except Exception as error: - log.critical("Unexpected error in the driver \"%s\"" % error) +def post_img(mastodon, text, visibility, logging): + file = random.choice(os.listdir(img_path+"/")) + image_byte = open(img_path+"/"+file, "rb").read() - -secrets_filepath = "secrets/secrets.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) - -config_file = "config.txt" - -img_path = get_parameter("img_path", config_file) - -mastodon = Mastodon( - client_id = uc_client_id, - client_secret = uc_client_secret, - access_token = uc_access_token, - api_base_url = 'https://' + mastodon_hostname, -) - -file = random.choice(os.listdir(img_path+"/")) -image_byte = open(img_path+"/"+file, "rb").read() - -if file[-3:] == "jpe": - mime = "image/jpeg" -else : - if file[-3:] == "jpg": + if file[-3:] == "jpe": mime = "image/jpeg" else : - if file[-3:] == "png": - mime = "image/png" + if file[-3:] == "jpg": + mime = "image/jpeg" else : - if file[-3:] == "gif": - mime = "image/gif" + if file[-3:] == "png": + mime = "image/png" + else : + if file[-3:] == "gif": + mime = "image/gif" -media_dict = mastodon.media_post(image_byte, mime) -#text.encode('utf-8').strip() -mastodon.status_post("", None, media_ids=[media_dict], sensitive=True, visibility='', spoiler_text="#NSFW") + media_dict = mastodon.media_post(image_byte, mime) + mastodon.status_post("", None, media_ids=[media_dict], sensitive=True, visibility='', spoiler_text="#NSFW") + +def init(config, secrets): + log = logging.getLogger() + log.setLevel(logging.DEBUG) + formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s') + + file_handler = RotatingFileHandler('activity.log', 'a', 1000000, 1) + file_handler.setLevel(logging.DEBUG) + file_handler.setFormatter(formatter) + log.addHandler(file_handler) + + stream_handler = logging.StreamHandler() + stream_handler.setLevel(logging.DEBUG) + log.addHandler(stream_handler) + + 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) + + img_path = get_parameter("img_path", config) + + mastodon = Mastodon( + client_id = uc_client_id, + client_secret = uc_client_secret, + access_token = uc_access_token, + api_base_url = 'https://' + mastodon_hostname, + ) + + +def run(mastodon, log): + log.info("Start streaming") + listener = BotListener(mastodon, logging) + mastodon.stream_user(listener) +#post_img(mastodon, "NSFW", 1) + +def main(): + config_file = "config.txt" + secrets_filepath = "secrets/secrets.txt" + init(config_file, secrets_filepath) + parser = argparse.ArgumentParser() + parser.add_argument('command', type=str, choices=['img', 'stream']) + args = parser.parse_args() + + if args.command == 'img': + post_img(mastodon, "NSFW", 1, log) + elif args.command == 'stream': + run(mastodon, log) + +main() diff --git a/secrets/secrets.txt b/secrets/secrets.txt index f955aef..70e1fa3 100644 --- a/secrets/secrets.txt +++ b/secrets/secrets.txt @@ -1,4 +1,4 @@ -client_id: 3c6ae74d9d01b03a808885a4e33a0d93466153d8face26248b8ff7437abda1b7 -client_secret: 0fbfb8ce41237e40b6dac1ff56eb4e16ad2185753ad4237fc528498551bd3c32 -access_token: cf4eb940c80e262a6898cdbd39c180c1af08cac11cf5b85c7613c33ae932749e +client_id: 53b2525d5723a226f903862cf725bc60d6fb3199a8cd4b85950b53924109a17f +client_secret: 9c0c5411adea85147fa35ffbfd20f4dd78af6dd999bec6a8ca9d1d27fc0f2d78 +access_token: 55d80d7d594734393f2a0d845a1700743f531637130e729194dbe7537cb43ad4 mastodon_hostname: miaou.drycat.fr