Compare commits
No commits in common. "9a9644e8d42c4676b738aad6ab5cdb7df367d388" and "9f0150ad3ae355674a583be8e67ee0a032ecdbf0" have entirely different histories.
9a9644e8d4
...
9f0150ad3a
4 changed files with 33 additions and 88 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,5 +1,3 @@
|
||||||
config.json
|
|
||||||
|
|
||||||
# ---> Python
|
# ---> Python
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
{
|
|
||||||
"site":"fedi.lynnesbian.space"
|
|
||||||
}
|
|
28
login.py
28
login.py
|
@ -1,28 +0,0 @@
|
||||||
import json
|
|
||||||
from mastodon import Mastodon
|
|
||||||
|
|
||||||
scopes = ["read:statuses", "read:accounts", "read:follows", "write:statuses"]
|
|
||||||
cfg = json.load(open('config.json', 'r'))
|
|
||||||
|
|
||||||
if "client" not in cfg:
|
|
||||||
print("No client credentials, registering application")
|
|
||||||
client_id, client_secret = Mastodon.create_app("lynnegle-assistant",
|
|
||||||
api_base_url=cfg['site'],
|
|
||||||
scopes=scopes,
|
|
||||||
website="https://git.lynnesbian.space/lynnesbian/lynnegle-assistant")
|
|
||||||
|
|
||||||
cfg['client'] = {
|
|
||||||
"id": client_id,
|
|
||||||
"secret": client_secret
|
|
||||||
}
|
|
||||||
|
|
||||||
if "secret" not in cfg:
|
|
||||||
print("No user credentials, logging in")
|
|
||||||
client = Mastodon(client_id = cfg['client']['id'],
|
|
||||||
client_secret = cfg['client']['secret'],
|
|
||||||
api_base_url=cfg['site'])
|
|
||||||
|
|
||||||
print("Open this URL: {}".format(client.auth_request_url(scopes=scopes)))
|
|
||||||
cfg['secret'] = client.log_in(code=input("Secret: "), scopes=scopes)
|
|
||||||
|
|
||||||
json.dump(cfg, open("config.json", "w+"))
|
|
88
main.py
88
main.py
|
@ -1,67 +1,45 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
# This Source Code Form is subject to the terms of the
|
|
||||||
# Mozilla Public License, v. 2.0.
|
|
||||||
# If a copy of the MPL was not distributed with this file,
|
|
||||||
# You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from mastodon import Mastodon
|
from mastodon import Mastodon
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
scopes = ["read:statuses", "read:accounts", "read:follows", "write:statuses"]
|
||||||
cfg = json.load(open('config.json', 'r'))
|
cfg = json.load(open('config.json', 'r'))
|
||||||
|
|
||||||
client = Mastodon(
|
if "client" not in cfg:
|
||||||
client_id=cfg['client']['id'],
|
print("No client credentials, registering application")
|
||||||
client_secret = cfg['client']['secret'],
|
client_id, client_secret = Mastodon.create_app("lynnegle-assistant",
|
||||||
access_token=cfg['secret'],
|
api_base_url=cfg['site'],
|
||||||
api_base_url=cfg['site'])
|
scopes=scopes,
|
||||||
|
website="https://github.com/Lynnesbian/mstdn-ebooks")
|
||||||
|
|
||||||
def extract_toot(toot):
|
cfg['client'] = {
|
||||||
#copied from main.py, see there for comments
|
"id": client_id,
|
||||||
soup = BeautifulSoup(toot, "html.parser")
|
"secret": client_secret
|
||||||
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
|
|
||||||
|
|
||||||
class ReplyListener(mastodon.StreamListener):
|
if "secret" not in cfg:
|
||||||
def on_notification(self, notification):
|
print("No user credentials, logging in")
|
||||||
if notification['type'] == 'mention':
|
client = Mastodon(client_id = cfg['client']['id'],
|
||||||
acct = "@" + notification['account']['acct']
|
client_secret = cfg['client']['secret'],
|
||||||
post_id = notification['status']['id']
|
api_base_url=cfg['site'])
|
||||||
mention = extract_toot(notification['status']['content'])
|
|
||||||
print(acct + " says " + mention)
|
|
||||||
|
|
||||||
kind = 'general' #todo: support for images
|
print("Open this URL: {}".format(client.auth_request_url(scopes=scopes)))
|
||||||
if kind == 'images':
|
cfg['secret'] = client.log_in(code=input("Secret: "), scopes=scopes)
|
||||||
r = requests.get("https://searx.lynnesbian.space/?category_images=1&q={}&format=json".format(q))
|
|
||||||
else:
|
|
||||||
r = requests.get("https://searx.lynnesbian.space/?q={}&format=json".format(q))
|
|
||||||
j = r.json()
|
|
||||||
|
|
||||||
if kind == 'images':
|
json.dump(cfg, open("config.json", "w+"))
|
||||||
toot = "Here's what I found by searching for images with the query \"{}\".".format(q)
|
|
||||||
else:
|
|
||||||
result = j['results'][0]
|
|
||||||
toot = result['title'] + "\n" + result['url'] + "\n" + result['content'] + "\n" + "(Score: {})\nMore results: {}{}".format(result['score'], "https://searx.lynnesbian.space/?q=", q)
|
|
||||||
|
|
||||||
toot = acct + " " + toot
|
kind = 'general'
|
||||||
visibility = notification['status']['visibility']
|
if kind == 'images':
|
||||||
if visibility == "public":
|
r = requests.get("https://searx.lynnesbian.space/?category_images=1&q={}&format=json".format(q))
|
||||||
visibility = "unlisted"
|
else:
|
||||||
client.status_post(toot, post_id, visibility=visibility)
|
r = requests.get("https://searx.lynnesbian.space/?q={}&format=json".format(q))
|
||||||
print("replied with " + toot)
|
j = r.json()
|
||||||
|
|
||||||
|
if kind == 'images':
|
||||||
|
text = "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'])
|
||||||
|
|
||||||
|
print(text)
|
||||||
|
|
Loading…
Reference in a new issue