2 Commits 2b9e428e62 ... 2583350a92

Author SHA1 Message Date
  encrypt 2583350a92 auguricek 3 years ago
  encrypt 5f80655524 mastodon2telegram 3 years ago
2 changed files with 70 additions and 0 deletions
  1. 11 0
      auguricek/auguricek.sh
  2. 59 0
      mastodon2telegram/mastodon2telegram.py

+ 11 - 0
auguricek/auguricek.sh

@@ -0,0 +1,11 @@
+#!/bin/sh
+
+seconds_since_cek() {
+        echo $(( $(date +%s) - $(date -d "1979-03-22" +%s) ))
+}
+
+cek="@cek@mastodon.bida.im"
+life_in_seconds=$(seconds_since_cek)
+angurie="auguri $cek !!!!!\n auguri $cek 🎉🎉🎉🎉🎉\n auguri!! $cek 🎊🎊🎊🎊 \n auguri!!!!! $cek 🎁🎁🎁🎁"
+anguria=$(echo -ne $angurie | shuf | head -n 1)
+toot -t=wX_qlcNQZLsUPfk6Y5Rp__uwT8avlFYLNEfag1Yde40 "$anguria per i tuoi $life_in_seconds secondi di vita"

+ 59 - 0
mastodon2telegram/mastodon2telegram.py

@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+
+import requests
+import json
+import telegram
+import re
+from time import sleep
+from telegram.error import NetworkError, Unauthorized
+
+
+telegram_token= ""
+telegram_channel=""
+profile_id=""
+
+telegram_bot = telegram.Bot(telegram_token)
+response = requests.get("https://mastodon.bida.im/api/v1/accounts/"+profile_id+"/statuses")
+
+last_id_filename = "/var/db/mastodon2telegram/"+profile_id+"_last_id_telegram.txt"
+last_id_file = open(last_id_filename, 'r')
+last_id = int(last_id_file.read())
+last_id_file.close()
+statuses = json.loads(response.text)
+statuses = sorted(statuses, key=lambda k: k['created_at'])
+print(last_id)
+
+for status in list(filter(lambda x: int(x["id"]) > last_id, list(filter(lambda x: x["in_reply_to_account_id"] == None, statuses)))):
+    id = int(status["id"])
+    print(status["created_at"])
+    print(id)
+    if last_id < id:
+        last_id = id
+    content = status["content"]
+    if content != "":
+        content = re.sub(r'<p>', '\n', content)
+        content = re.sub(r'</p>', '\n', content)
+        content = re.sub(r'<br />', '\n', content)
+        content = re.sub(r'<(/?)(span)([^>]+)?>', '', content)
+        content = re.sub(r'<a [^>]+ rel=\"tag\">#([^>]+)<\/a>', r'#\1', content)
+        content = content.replace("&apos;", "'")
+        for emoji in status["emojis"]:
+            content = content.replace(":"+emoji["shortcode"]+":", "")
+        print(content)
+        try:
+            telegram_bot.send_message(chat_id=telegram_channel, text=content, parse_mode=telegram.ParseMode.HTML, disable_web_page_preview="true")
+        except Exception as err:
+            print("aiai", err)
+    for media in list(filter(lambda x: x["type"] == "image", status["media_attachments"])):
+        try:
+            telegram_bot.send_photo(chat_id=telegram_channel, photo=media["url"])
+            sleep(10)
+        except Exception:
+            print("mediaiaia")
+    sleep(10)
+    print("-----------")
+
+last_id_file = open(last_id_filename, 'w')
+last_id_file.write(str(last_id))
+last_id_file.close()
+