From c78bc6ba7e5121ae96f7c140764130d9359f4e01 Mon Sep 17 00:00:00 2001 From: oloturia <5429234+oloturia@users.noreply.github.com> Date: Sat, 30 Apr 2022 00:54:16 +0200 Subject: [PATCH] added the replier --- .gitignore | 2 ++ config.json | 4 +--- mastodon_main.py | 18 +++++++++++------- mastodon_replier.py | 37 ++++++++++++++++++++++++++++++++----- register_app.py | 15 +++++++++++++++ 5 files changed, 61 insertions(+), 15 deletions(-) mode change 100644 => 100755 mastodon_replier.py create mode 100755 register_app.py diff --git a/.gitignore b/.gitignore index e40fbb7..d5a24c4 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ history/* bin/* temp/* *.pyc +secret +mastodon_listener_token diff --git a/config.json b/config.json index 9d821cc..5aa168b 100644 --- a/config.json +++ b/config.json @@ -1,8 +1,6 @@ { "defaultProfile": "oloturia", - "defaultMastodonToken": "./mastodon_token", - "defaultTelegramToken": "./telegram_token", - "defaultTwitterToken": "./twitter_token", + "mastodonListenerToken": "./mastodon_listener_token", "oloturia": { "saveLocation": "/var/tmp", diff --git a/mastodon_main.py b/mastodon_main.py index e58e5b5..fbdfc14 100755 --- a/mastodon_main.py +++ b/mastodon_main.py @@ -8,13 +8,8 @@ fileDir = os.path.dirname(os.path.abspath(__file__)) fileDir = fileDir +"/" API_URL = "https://botsin.space" -if __name__ == "__main__": - if len(sys.argv) == 2: - altProfile = [sys.argv[1]] - else: - altProfile = False +def publishStrip(altProfile=False,user=False): config = readConfig(platform="mastodon",profile=altProfile) - with open(config["token"]) as f: createapp = f.readlines() createapp = [x.strip() for x in createapp] @@ -26,7 +21,10 @@ if __name__ == "__main__": for i in range(1,100): try: new_strip = mastodon.media_post(config["saveLocation"]+config["filename"],"image/png") - mastodon.status_post(config["text"],media_ids=new_strip) + if not(user): + mastodon.status_post(config["text"],media_ids=new_strip) + else: + mastodon.status_post(user+" "+config["text"],media_ids=new_strip,visibility="direct") published = True except: continue @@ -36,3 +34,9 @@ if __name__ == "__main__": else: print("error creating image\n") print(status) + +if __name__ == "__main__": + if len(sys.argv) == 2: + publishStrip([sys.argv[1]]) + else: + publishStrip() diff --git a/mastodon_replier.py b/mastodon_replier.py old mode 100644 new mode 100755 index 45a2711..ba7dde7 --- a/mastodon_replier.py +++ b/mastodon_replier.py @@ -1,7 +1,9 @@ #!/usr/bin/python3 -from randstrip import createStrip,readConfig -from mastodon import Mastodon +from randstrip import createStrip +from mastodon import Mastodon, StreamListener +from mastodon_main import publishStrip +import json import os import sys @@ -9,7 +11,32 @@ fileDir = os.path.dirname(os.path.abspath(__file__)) fileDir = fileDir + "/" API_URL = "https://botsin.space" +class stripListener(StreamListener): + def on_notification(self, notification): + try: + account = "@"+notification["account"]["acct"] + content = notification["status"]["content"] + if content.find("help") != -1: + mastodon.status_post("Hello "+account+" just send me a message with 'new strip' and the desired profile. Try with 'oloturia' (Italian), 'oloeng' (broken English) or 'olofra' (French). If not specified, Italian will be selected as default.",visibility="direct") + elif content.find("strip") != -1: + if content.find("oloeng") != -1: + profile = "oloeng" + elif content.find("olofra") != -1: + profile = "olofra" + else: + profile = "oloturia" + publishStrip([profile],account) + except KeyError: + return + + if __name__ == "__main__": - config = readConfig - - + with open(fileDir+"/config.json") as f: + config = json.load(f) + with open(config["mastodonListenerToken"]) as f: + createapp = f.readlines() + createapp = [x.strip() for x in createapp] + TOKEN = createapp[0] + mastodon = Mastodon(access_token = TOKEN,api_base_url = API_URL) + listener = stripListener() + mastodon.stream_user(listener) diff --git a/register_app.py b/register_app.py new file mode 100755 index 0000000..9699628 --- /dev/null +++ b/register_app.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 +from mastodon import Mastodon + +''' +you need to register the app only once +''' + + +Mastodon.create_app( + "oloturia_listener", + api_base_url = "https://botsin.space", + to_file = "secret" +) + +print("App registered!")