From c2997ae0d17a18461fbc473a2fdc0c975b1b840f Mon Sep 17 00:00:00 2001 From: Lynne Date: Fri, 11 Jan 2019 22:56:35 +1000 Subject: [PATCH] fixed a silly mistake, fixed " and ' stuff --- functions.py | 4 +++- reply.py | 39 ++++++++++++--------------------------- 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/functions.py b/functions.py index 31caf36..8a11466 100755 --- a/functions.py +++ b/functions.py @@ -62,6 +62,8 @@ def make_toot_markov(query = None): } def extract_toot(toot): + toot = toot.replace("'", "'") #convert HTML stuff to normal stuff + toot = toot.replace(""", '"') #ditto soup = BeautifulSoup(toot, "html.parser") for lb in soup.select("br"): #replace
with linebreak lb.insert_after("\n") @@ -78,7 +80,7 @@ def extract_toot(toot): link.insert_after(link["href"]) link.decompose() - toot = soup.get_text() + text = soup.get_text() text = re.sub("https://([^/]+)/(@[^ ]+)", r"\2@\1", text) #put mastodon-style mentions back in text = re.sub("https://([^/]+)/users/([^ ]+)", r"@\2@\1", text) #put pleroma-style mentions back in text = text.rstrip("\n") #remove trailing newline diff --git a/reply.py b/reply.py index 233ef0c..2e60de1 100755 --- a/reply.py +++ b/reply.py @@ -17,40 +17,25 @@ client = mastodon.Mastodon( api_base_url=cfg['site']) def extract_toot(toot): - #copied from main.py, see there for comments - soup = BeautifulSoup(toot, "html.parser") - for lb in soup.select("br"): - lb.insert_after("\n") - lb.decompose() - for p in soup.select("p"): - p.insert_after("\n") - p.unwrap() - for ht in soup.select("a.hashtag"): - ht.unwrap() - for link in soup.select("a"): - link.insert_after(link["href"]) - link.decompose() - text = map(lambda a: a.strip(), soup.get_text().strip().split("\n")) - text = "\n".join(list(text)) - text = re.sub("https?://([^/]+)/(@[^ ]+)", r"\2@\1", text) #put mentions back in - text = re.sub("^@[^@]+@[^ ]+ *", r"", text) #...but remove the initial one - text = text.lower() #for easier matching + text = functions.extract_toot(toot) + text = re.sub(r"^@[^@]+@[^ ]+\s*", r"", text) #remove the initial mention + text = text.lower() #treat text as lowercase for easier keyword matching (if this bot uses it) return text class ReplyListener(mastodon.StreamListener): - def on_notification(self, notification): - if notification['type'] == 'mention': - acct = "@" + notification['account']['acct'] + def on_notification(self, notification): #listen for notifications + if notification['type'] == 'mention': #if we're mentioned: + acct = "@" + notification['account']['acct'] #get the account's @ post_id = notification['status']['id'] mention = extract_toot(notification['status']['content']) - toot = functions.make_toot(True)['toot'] - toot = acct + " " + toot - print(acct + " says " + mention) + toot = functions.make_toot(True)['toot'] #generate a toot + toot = acct + " " + toot #prepend the @ + print(acct + " says " + mention) #logging visibility = notification['status']['visibility'] if visibility == "public": visibility = "unlisted" - client.status_post(toot, post_id, visibility=visibility, spoiler_text = cfg['cw']) - print("replied with " + toot) + client.status_post(toot, post_id, visibility=visibility, spoiler_text = cfg['cw']) #send toost + print("replied with " + toot) #logging rl = ReplyListener() -client.stream_user(rl) +client.stream_user(rl) #go!