Compare commits

..

4 commits

Author SHA1 Message Date
9a9644e8d4
permission changes 2018-10-27 23:27:23 +10:00
af0252f476
first working version (i hope) 2018-10-27 23:27:04 +10:00
eb56e43d09
imports 2018-10-27 23:18:13 +10:00
449f245fe4
moved login to separate file 2018-10-27 23:17:50 +10:00
4 changed files with 88 additions and 33 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
config.json
# ---> Python # ---> Python
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/

3
config.json.sample Normal file
View file

@ -0,0 +1,3 @@
{
"site":"fedi.lynnesbian.space"
}

28
login.py Executable file
View file

@ -0,0 +1,28 @@
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+"))

78
main.py
View file

@ -1,45 +1,67 @@
#!/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'))
if "client" not in cfg: client = Mastodon(
print("No client credentials, registering application") client_id=cfg['client']['id'],
client_id, client_secret = Mastodon.create_app("lynnegle-assistant",
api_base_url=cfg['site'],
scopes=scopes,
website="https://github.com/Lynnesbian/mstdn-ebooks")
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'], client_secret = cfg['client']['secret'],
access_token=cfg['secret'],
api_base_url=cfg['site']) api_base_url=cfg['site'])
print("Open this URL: {}".format(client.auth_request_url(scopes=scopes))) def extract_toot(toot):
cfg['secret'] = client.log_in(code=input("Secret: "), scopes=scopes) #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
json.dump(cfg, open("config.json", "w+")) 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' kind = 'general' #todo: support for images
if kind == 'images': if kind == 'images':
r = requests.get("https://searx.lynnesbian.space/?category_images=1&q={}&format=json".format(q)) 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)) r = requests.get("https://searx.lynnesbian.space/?q={}&format=json".format(q))
j = r.json() j = r.json()
if kind == 'images': if kind == 'images':
text = "Here's what I found by searching for images with the query \"{}\".".format(q) toot = "Here's what I found by searching for images with the query \"{}\".".format(q)
else: else:
result = j['results'][0] 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)