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):
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 <br> 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

View File

@ -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!