forked from Jess3Jane/mastodon-ebooks
-
Notifications
You must be signed in to change notification settings - Fork 2
/
reply.py
54 lines (45 loc) · 1.93 KB
/
reply.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import mastodon
from mastodon import Mastodon
import gen
from os import path, listdir
import threading
api_base_url = "https://botsin.space"
class ReplyListener(mastodon.StreamListener):
def __init__(self, client, corpus_path):
self.client = client
self.corpus_path = corpus_path
def on_notification(self, notification):
print("got notification")
print(notification)
if notification['type'] == 'mention': #if we're mentioned:
acct = "@" + notification['account']['acct'] #get the account's @
post_id = notification['status']['id']
# mention = extract_toot(notification['status']['content'])
# toot = functions.make_toot(True)['toot'] #generate a toot
toot = gen.create_toot(self.corpus_path)
toot = acct + " " + toot #prepend the @
# print(acct + " says " + mention) #logging
visibility = notification['status']['visibility']
if visibility == "public":
visibility = "unlisted"
self.client.status_post(toot, post_id, visibility=visibility) #send toost
print("replied with " + toot) #logging
def createListener(user_directory):
access_token_path=path.join(user_directory, "usercred.secret")
corpus_path=path.join(user_directory, "corpus.txt")
client = Mastodon(
client_id="clientcred.secret",
access_token=access_token_path,
api_base_url=api_base_url)
r1 = ReplyListener(client, corpus_path)
print("listening...")
client.stream_user(r1)
if __name__ == "__main__":
import sys
# createListener(sys.argv[1])
user_directory = sys.argv[1]
directories = [path.join(user_directory, x) for x in listdir(user_directory) if not path.isfile(path.join(user_directory, x))]
for x in directories:
print("loading listener for", x)
t0 = threading.Thread(target=createListener, args=[x,])
t0.start()