first working version (i hope)
This commit is contained in:
parent
eb56e43d09
commit
af0252f476
1 changed files with 47 additions and 12 deletions
53
main.py
53
main.py
|
@ -9,24 +9,59 @@ import requests
|
|||
from mastodon import Mastodon
|
||||
import json
|
||||
|
||||
cfg = json.load(open('config.json', 'r'))
|
||||
|
||||
client = Mastodon(
|
||||
client_id=cfg['client']['id'],
|
||||
client_secret = cfg['client']['secret'],
|
||||
access_token=cfg['secret'],
|
||||
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
|
||||
return text
|
||||
|
||||
kind = 'general'
|
||||
if kind == 'images':
|
||||
class ReplyListener(mastodon.StreamListener):
|
||||
def on_notification(self, notification):
|
||||
if notification['type'] == 'mention':
|
||||
acct = "@" + notification['account']['acct']
|
||||
post_id = notification['status']['id']
|
||||
mention = extract_toot(notification['status']['content'])
|
||||
print(acct + " says " + mention)
|
||||
|
||||
kind = 'general' #todo: support for images
|
||||
if kind == 'images':
|
||||
r = requests.get("https://searx.lynnesbian.space/?category_images=1&q={}&format=json".format(q))
|
||||
else:
|
||||
else:
|
||||
r = requests.get("https://searx.lynnesbian.space/?q={}&format=json".format(q))
|
||||
j = r.json()
|
||||
j = r.json()
|
||||
|
||||
if kind == 'images':
|
||||
text = "Here's what I found by searching for images with the query \"{}\".".format(q)
|
||||
else:
|
||||
if kind == 'images':
|
||||
toot = "Here's what I found by searching for images with the query \"{}\".".format(q)
|
||||
else:
|
||||
result = j['results'][0]
|
||||
text = result['title'] + "\n" + result['url'] + "\n" + result['content'] + "\n" + "(Score: {})".format(result['score'])
|
||||
toot = result['title'] + "\n" + result['url'] + "\n" + result['content'] + "\n" + "(Score: {})\nMore results: {}{}".format(result['score'], "https://searx.lynnesbian.space/?q=", q)
|
||||
|
||||
print(text)
|
||||
toot = acct + " " + toot
|
||||
visibility = notification['status']['visibility']
|
||||
if visibility == "public":
|
||||
visibility = "unlisted"
|
||||
client.status_post(toot, post_id, visibility=visibility)
|
||||
print("replied with " + toot)
|
||||
|
|
Loading…
Reference in a new issue