added extract_toot function to functions.py
This commit is contained in:
parent
85fec32c83
commit
de3449ae56
23
functions.py
23
functions.py
|
@ -60,3 +60,26 @@ def make_toot_markov(query = None):
|
|||
"toot": toot,
|
||||
"media": None
|
||||
}
|
||||
|
||||
def extract_toot(toot):
|
||||
soup = BeautifulSoup(toot, "html.parser")
|
||||
for lb in soup.select("br"): #replace <br> with linebreak
|
||||
lb.insert_after("\n")
|
||||
lb.decompose()
|
||||
|
||||
for p in soup.select("p"): #ditto for <p>
|
||||
p.insert_after("\n")
|
||||
p.unwrap()
|
||||
|
||||
for ht in soup.select("a.hashtag"): #make hashtags no longer links, just text
|
||||
ht.unwrap()
|
||||
|
||||
for link in soup.select("a"): #ocnvert <a href='https://example.com>example.com</a> to just https://example.com
|
||||
link.insert_after(link["href"])
|
||||
link.decompose()
|
||||
|
||||
toot = 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
|
||||
return text
|
Loading…
Reference in New Issue