1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #!/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("'", "'")
- 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()
|