summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore5
-rw-r--r--config.py23
-rw-r--r--main.py116
-rw-r--r--patterns.py7
4 files changed, 151 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..906ee43
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+*.secret
+__pycache__
+notes.txt
+oauth.txt
+./oauth.txt
diff --git a/config.py b/config.py
new file mode 100644
index 0000000..0469806
--- /dev/null
+++ b/config.py
@@ -0,0 +1,23 @@
+APPNAME = 'catbot'
+BASEURL = 'https://vnil.de'
+
+# no need to change the following ---
+CLIENTID = 'client.secret'
+TOKEN = 'token.secret'
+
+# in seconds
+POLL_INTERVAL = 20
+
+# account username and PW
+UNAME = ""
+PW = ""
+ACCOUNT_SECRET = "account.secret"
+try:
+ f = open(ACCOUNT_SECRET)
+ lines = f.readlines()
+ UNAME = lines[0]
+ PW = lines[1]
+except:
+ print("account secret not found, please manually input:")
+ UNAME = input("username or email")
+ PW = input("password (not concealed)")
diff --git a/main.py b/main.py
new file mode 100644
index 0000000..1416e3c
--- /dev/null
+++ b/main.py
@@ -0,0 +1,116 @@
+from mastodon import Mastodon
+import config
+import time
+import re
+import random
+import os
+from patterns import *
+
+
+def create_app():
+ Mastodon.create_app(client_name='void**** cat',api_base_url='https://vnil.de',to_file='vnil_bot.secret')
+
+# create_app()
+
+def login_refresh_token():
+ session = Mastodon(client_id=config.CLIENTID)
+ session.log_in(username=config.UNAME, password=config.PW, to_file=config.TOKEN)
+ return session
+
+def restore_session():
+ return Mastodon(client_id=config.CLIENTID, access_token=config.TOKEN)
+
+
+class VnilCat:
+ def __init__(self,config):
+ self.session = restore_session()
+ try:
+ self.session.account_verify_credentials()
+ except:
+ try:
+ self.session = login_refresh_token()
+ except:
+ exit()
+
+
+ def reply_meow(self, ori_status):
+ print("replying meow to ", ori_status["id"])
+ self.session.status_reply(to_status=ori_status, status=random.choice(cat_sounds))
+
+
+ # For now we don't handle other interaction types
+ def handle_mention(self, notification):
+ nid = notification["id"]
+ # in the case of mention,
+ # notification['status']['account'] and
+ # notification['account'] are the same thing
+ acc = notification["account"]
+ status = notification["status"]
+ sid = status["id"]
+ # created_at = notification["created_at"]
+ # pleroma = notification["pleroma"]
+ print("we have a mention > ",nid)
+ content = status["content"]
+ if re_contains_meow.search(content) is not None:
+ self.reply_meow(status)
+
+ print("dismissing notification ", nid)
+ self.session.notifications_dismiss(nid)
+
+ def handle_follow(self, notification):
+ nid = notification["id"]
+ uid = notification['account']['id']
+ rel = self.session.account_relationships(uid)
+ if rel[0]['following'] == False:
+ print("follow ", uid)
+ self.session.account_follow(uid, reblogs=False, notify=False)
+ else:
+ print("already following", uid)
+ self.session.notifications_dismiss(nid)
+
+ def scan_local(self,session):
+ print("TODO")
+
+ def handle_notification(self):
+ ns = self.session.notifications()
+ for n in ns:
+ if n['type'] == "mention":
+ self.handle_mention(n)
+ if n['type'] == "follow":
+ self.handle_follow(n)
+
+
+# TODO re-organize auth mgmt. But it involves a lot of mamual testing so I'll
+# leave it to the future...
+def init_bot():
+ Mastodon.create_app(config.APPNAME, api_base_url=config.BASEURL,
+ to_file=config.CLIENTID)
+ session = login_refresh_token()
+
+
+def run():
+ # print("HELLO")
+ cat = VnilCat(config)
+ # print(cat.session.notifications())
+
+ # print(session.notifications())
+ # session.toot("WTF")
+
+ while True:
+ time.sleep(config.POLL_INTERVAL)
+ try:
+ cat.handle_notification()
+ except:
+ print("something wrong...")
+
+
+if __name__ == "__main__":
+ # check session data
+ if not os.path.isfile(config.CLIENTID):
+ print("client data doesn't exist..creating...")
+ init_bot()
+
+ run()
+
+
+
diff --git a/patterns.py b/patterns.py
new file mode 100644
index 0000000..9118227
--- /dev/null
+++ b/patterns.py
@@ -0,0 +1,7 @@
+import re
+
+#### RE PATTERNS ####
+re_contains_meow = re.compile(r'(me+o+w|喵)',re.IGNORECASE)
+
+#### MISC STRING PRESETS ####
+cat_sounds = ["Meow!", "Mrrrow!", "Purr...", "Meee-OW!", "Mreoww!", "Nya~", "Mew?", "Rowr?", "Prrrr...", "Maow-maow!"]