fixed a silly mistake, fixed " and ' stuff

This commit is contained in:
Lynne 2019-01-11 22:56:35 +10:00
parent de3449ae56
commit c2997ae0d1
No known key found for this signature in database
GPG Key ID: FB7B970303ACE499
2 changed files with 15 additions and 28 deletions

View File

@ -62,6 +62,8 @@ def make_toot_markov(query = None):
} }
def extract_toot(toot): def extract_toot(toot):
toot = toot.replace("'", "'") #convert HTML stuff to normal stuff
toot = toot.replace(""", '"') #ditto
soup = BeautifulSoup(toot, "html.parser") soup = BeautifulSoup(toot, "html.parser")
for lb in soup.select("br"): #replace <br> with linebreak for lb in soup.select("br"): #replace <br> with linebreak
lb.insert_after("\n") lb.insert_after("\n")
@ -78,7 +80,7 @@ def extract_toot(toot):
link.insert_after(link["href"]) link.insert_after(link["href"])
link.decompose() 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://([^/]+)/(@[^ ]+)", 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 = re.sub("https://([^/]+)/users/([^ ]+)", r"@\2@\1", text) #put pleroma-style mentions back in
text = text.rstrip("\n") #remove trailing newline text = text.rstrip("\n") #remove trailing newline

View File

@ -17,40 +17,25 @@ client = mastodon.Mastodon(
api_base_url=cfg['site']) api_base_url=cfg['site'])
def extract_toot(toot): def extract_toot(toot):
#copied from main.py, see there for comments text = functions.extract_toot(toot)
soup = BeautifulSoup(toot, "html.parser") text = re.sub(r"^@[^@]+@[^ ]+\s*", r"", text) #remove the initial mention
for lb in soup.select("br"): text = text.lower() #treat text as lowercase for easier keyword matching (if this bot uses it)
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
return text return text
class ReplyListener(mastodon.StreamListener): class ReplyListener(mastodon.StreamListener):
def on_notification(self, notification): def on_notification(self, notification): #listen for notifications
if notification['type'] == 'mention': if notification['type'] == 'mention': #if we're mentioned:
acct = "@" + notification['account']['acct'] acct = "@" + notification['account']['acct'] #get the account's @
post_id = notification['status']['id'] post_id = notification['status']['id']
mention = extract_toot(notification['status']['content']) mention = extract_toot(notification['status']['content'])
toot = functions.make_toot(True)['toot'] toot = functions.make_toot(True)['toot'] #generate a toot
toot = acct + " " + toot toot = acct + " " + toot #prepend the @
print(acct + " says " + mention) print(acct + " says " + mention) #logging
visibility = notification['status']['visibility'] visibility = notification['status']['visibility']
if visibility == "public": if visibility == "public":
visibility = "unlisted" visibility = "unlisted"
client.status_post(toot, post_id, visibility=visibility, spoiler_text = cfg['cw']) client.status_post(toot, post_id, visibility=visibility, spoiler_text = cfg['cw']) #send toost
print("replied with " + toot) print("replied with " + toot) #logging
rl = ReplyListener() rl = ReplyListener()
client.stream_user(rl) client.stream_user(rl) #go!