Browse Source

first commit

d0c 3 years ago
parent
commit
c7a2d398b5
7 changed files with 114 additions and 0 deletions
  1. 13 0
      Dockerfile
  2. 24 0
      README.md
  3. 12 0
      env.sample
  4. 54 0
      megafono.py
  5. 2 0
      requirements.txt
  6. 1 0
      txt/last_id.txt
  7. 8 0
      txt/prove_annunci.txt

+ 13 - 0
Dockerfile

@@ -0,0 +1,13 @@
+FROM python:3
+
+RUN mkdir /data
+
+WORKDIR "/usr/src/app"
+ENV IS_DOCKER 1
+
+COPY requirements.txt .
+RUN pip install --no-cache-dir -r requirements.txt
+
+COPY . .
+
+CMD [ "python", "./megafono.py" ]

+ 24 - 0
README.md

@@ -0,0 +1,24 @@
+# Intro
+
+Megafono is a simple bot for [Mastodon](https://github.com/tootsuite/mastodon/) instances using [Mastodon.py](https://github.com/halcy/Mastodon.py).
+
+# Usage
+
+Requires Python3 and pip.
+
+Run `pip install -r requirements.txt`.
+
+See the included `.env.sample` file for information on configuration.
+
+# Docker
+
+To use the docker version, build it, and run it with:
+
+```
+docker build -t megafono .
+docker run -v "$(pwd)"/txt:/data -it megafono:latest
+```
+Files in txt directory can be updated without rebuild image
+
+# Credits
+https://github.com/caffeinewriter/botvenon

+ 12 - 0
env.sample

@@ -0,0 +1,12 @@
+# Mastodon settings
+INSTANCE_BASE = 'https://'
+# To fill following values log in your mastodon instance and go Preferences -> Development -> "Your application" (if empty create new application)
+CLIENT_ID = ''
+CLIENT_SECRET = ''
+ACCESS_TOKEN = ''
+
+# File with messages (one per line) to be tooted. Must be in txt directory. 
+ANNOUNCEMENT_FILE = 'es. announce.txt'
+
+# Set to 1 to enable random read from ANNOUNCEMENT_FILE
+RANDOM = 0

+ 54 - 0
megafono.py

@@ -0,0 +1,54 @@
+from mastodon import Mastodon
+from dotenv import load_dotenv
+from pathlib import Path
+import os
+import time
+
+
+def megafono():
+    """
+    Toot only one line from ANNOUNCEMENT_FILE gradually or random if RANDOM is set.
+    It's useful configure a crontab for invocation
+    """
+    load_dotenv()
+    INSTANCE_BASE = os.getenv('INSTANCE_BASE')
+    CLIENT_ID = os.getenv('CLIENT_ID')
+    CLIENT_SECRET = os.getenv('CLIENT_SECRET')
+    ACCESS_TOKEN = os.getenv('ACCESS_TOKEN')
+    ANNOUNCEMENT_FILE = os.getenv('ANNOUNCEMENT_FILE')
+    RANDOM = os.getenv('RANDOM')
+    IS_DOCKER = os.getenv('IS_DOCKER', False)
+
+    if IS_DOCKER:
+        last_id_file = str(Path('/data') / 'last_id')
+        ANNOUNCEMENT_FILE = str(Path('/data') / ANNOUNCEMENT_FILE)
+    else:
+        last_id_file = str(Path('./txt') / 'last_id.txt')
+        ANNOUNCEMENT_FILE = str(Path('./txt') / ANNOUNCEMENT_FILE)
+
+    mastodon = Mastodon(
+       client_id=CLIENT_ID,
+       client_secret=CLIENT_SECRET,
+       access_token=ACCESS_TOKEN,
+       api_base_url=INSTANCE_BASE)
+
+    if int(RANDOM):
+        import random
+        toot = random.choice(list(open(ANNOUNCEMENT_FILE, "r")))
+    else:
+        last_id = 0
+        with open(last_id_file) as f:
+            last_id = int(f.read())
+        with open(ANNOUNCEMENT_FILE, "r") as a:
+            toot = a.readlines()[last_id]
+        with open(ANNOUNCEMENT_FILE, "r") as a:
+            length = len(a.readlines())
+        last_id += 1 
+        last_id %= length
+        with open(last_id_file, 'w+') as f:
+                f.write(str(last_id))
+    mastodon.status_post(toot,  visibility='unlisted')
+    print("Sent: "+str(toot))
+
+
+megafono()

+ 2 - 0
requirements.txt

@@ -0,0 +1,2 @@
+Mastodon.py
+python-dotenv

+ 1 - 0
txt/last_id.txt

@@ -0,0 +1 @@
+6

+ 8 - 0
txt/prove_annunci.txt

@@ -0,0 +1,8 @@
+1
+2
+3
+4
+5
+6
+7
+8