Browse Source

added the replier

oloturia 1 year ago
parent
commit
c78bc6ba7e
5 changed files with 61 additions and 15 deletions
  1. 2 0
      .gitignore
  2. 1 3
      config.json
  3. 11 7
      mastodon_main.py
  4. 32 5
      mastodon_replier.py
  5. 15 0
      register_app.py

+ 2 - 0
.gitignore

@@ -11,3 +11,5 @@ history/*
 bin/*
 temp/*
 *.pyc
+secret
+mastodon_listener_token

+ 1 - 3
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",

+ 11 - 7
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()

+ 32 - 5
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"
 
-if __name__ == "__main__":
-	config = readConfig
-
+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__":
+	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)

+ 15 - 0
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!")