|
|
@ -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") |
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
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(): |
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|