amber-ebooks/reply.py

81 lines
2.9 KiB
Python
Raw Normal View History

2018-10-09 01:11:51 +00:00
#!/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 mastodon
2019-05-19 13:06:31 +00:00
import random, re, json
2019-01-11 12:47:42 +00:00
import functions
2018-10-09 01:11:51 +00:00
from bs4 import BeautifulSoup
cfg = json.load(open('config.json', 'r'))
2018-10-09 01:11:51 +00:00
client = mastodon.Mastodon(
2018-10-26 01:35:14 +00:00
client_id=cfg['client']['id'],
2019-05-19 13:06:31 +00:00
client_secret=cfg['client']['secret'],
access_token=cfg['secret'],
2018-10-26 01:35:14 +00:00
api_base_url=cfg['site'])
2018-10-09 01:11:51 +00:00
def extract_toot(toot):
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)
2018-10-09 01:11:51 +00:00
return text
class ReplyListener(mastodon.StreamListener):
def on_notification(self, notification): #listen for notifications
if notification['type'] == 'mention': #if we're mentioned:
acct = "@" + notification['account']['acct'] #get the account's @
2018-10-09 01:11:51 +00:00
post_id = notification['status']['id']
2019-05-19 13:06:31 +00:00
# check if we've already been participating in this thread
try:
context = client.status_context(post_id)
except:
print("failed to fetch thread context")
return
me = client.account_verify_credentials()['id']
posts = 0
for post in context['ancestors']:
if post['account']['id'] == me:
pin = post["id"] #Only used if pin is called, but easier to call here
posts += 1
2019-05-19 13:06:31 +00:00
if posts >= cfg['max_thread_length']:
# stop replying
print("didn't reply (max_thread_length exceeded)")
return
2018-10-09 01:11:51 +00:00
mention = extract_toot(notification['status']['content'])
if (mention == "pin") or (mention == "unpin"): #check for keywords
print("Found pin/unpin")
#get a list of people the bot is following
validusers = client.account_following(me)
for user in validusers:
if user["id"] == notification["account"]["id"]: #user is #valid
print("User is valid")
2019-05-19 12:54:59 +00:00
visibility = notification['status']['visibility']
if visibility == "public":
visibility = "unlisted"
if mention == "pin":
print("pin received, pinning")
client.status_pin(pin)
2019-05-19 12:57:20 +00:00
client.status_post("Toot pinned!", post_id, visibility=visibility, spoiler_text = cfg['cw'])
else:
print("unpin received, unpinning")
2019-05-19 12:57:20 +00:00
client.status_post("Toot unpinned!", post_id, visibility=visibility, spoiler_text = cfg['cw'])
client.status_unpin(pin)
else:
print("User is not valid")
else:
2019-07-02 10:43:34 +00:00
toot = functions.make_toot(True) #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']) #send toost
print("replied with " + toot) #logging
2018-10-09 01:11:51 +00:00
rl = ReplyListener()
client.stream_user(rl) #go!