From 878f4c88823e669ba1be4db8c4d0a1b0273aa4c3 Mon Sep 17 00:00:00 2001 From: Danial Behzadi Date: Tue, 12 Dec 2023 07:46:30 -0500 Subject: [PATCH] Update to newer pyrogram. --- .gitignore | 5 ++- README.md | 3 +- config.ini.temp | 3 -- config.py.temp | 2 ++ tgchannel2toot.py | 79 +++++++++++++++++++++++------------------------ 5 files changed, 46 insertions(+), 46 deletions(-) delete mode 100644 config.ini.temp diff --git a/.gitignore b/.gitignore index e66d32f..dc9602c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ .env *.pyc __pycache__ -config.ini config.py tgchannel2toot.session +downloads/ +nohup.out +unknown_errors.txt +tgchannel2toot.session-journal diff --git a/README.md b/README.md index 3405718..bc69af5 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,8 @@ To make it work, do the following: Config ====== -Make a copy of each config file: +Make a copy of config file: - (.env)$ cp config.ini.temp config.ini (.env)$ cp config.py.temp config.py Fill out config files. diff --git a/config.ini.temp b/config.ini.temp deleted file mode 100644 index 9287365..0000000 --- a/config.ini.temp +++ /dev/null @@ -1,3 +0,0 @@ -[pyrogram] -api_id = 12345 -api_hash = 0123456789abcdef0123456789abcdef diff --git a/config.py.temp b/config.py.temp index 67dd391..c3d0b34 100644 --- a/config.py.temp +++ b/config.py.temp @@ -2,3 +2,5 @@ channel_id = 'iranFSevents' access_token = '5He72z-Dz6DEruFjvAVYL8ieTRhha5xPwkLGv19rfgA' instance = 'https://botsin.space' character_limit = 500 +api_id = 12345 +api_hash = 0123456789abcdef0123456789abcdef diff --git a/tgchannel2toot.py b/tgchannel2toot.py index 5a41440..b9463d5 100644 --- a/tgchannel2toot.py +++ b/tgchannel2toot.py @@ -9,78 +9,77 @@ And post them on Mastodon import os -from pyrogram import Client, idle -from mastodon import Mastodon +import pyrogram +import mastodon import config +def splitext(text: str): + """ + split text into parts + """ + limit = config.character_limit + result = [ + text[limit * part : limit * (part + 1)] + for part in range(len(text) // limit + 1) + ] + return result + + def get_text_and_media(message): - ''' + """ returns the text and media from post - ''' + """ text = " " media = None - if message['text']: - text = message['text'] - elif message['media']: - text = message['caption'] + if hasattr(message, "media"): + text = message.caption media = message.download() + elif hasattr(message, "text"): + text = message.text return splitext(text), media def connect(): - ''' + """ initialize mastodon - ''' - mastodon = Mastodon( - access_token=config.access_token, - api_base_url=config.instance - ) - return mastodon - - -def splitext(text: str): - ''' - split text into parts - ''' - limit = config.character_limit - result = [ - text[limit*part:limit*(part+1)] - for part in range(len(text)//limit+1)] - return result + """ + mast = mastodon.Mastodon( + access_token=config.access_token, api_base_url=config.instance + ) + return mast def main(): - ''' + """ main function - ''' - telegram = Client("tgchannel2toot") + """ + telegram = pyrogram.Client( + "tgchannel2toot", api_id=config.api_id, api_hash=config.api_hash + ) mastodon = connect() @telegram.on_message() def check_message(client, message): - chat = message['chat'] + chat = message.chat - if chat['type'] == 'channel': - if chat['username'] == config.channel_id: + if chat.type==pyrogram.enums.ChatType.CHANNEL: + if chat.username == config.channel_id: text, media = get_text_and_media(message) if media: mid = mastodon.media_post(media) os.remove(media) else: mid = None - post = mastodon.status_post( - text[0], - media_ids=mid) + post = mastodon.status_post(text[0], media_ids=mid) for part in range(1, len(text)): post = mastodon.status_post( - text[part], - in_reply_to_id=post['id']) + text[part], in_reply_to_id=post["id"] + ) - telegram.start() - idle() + telegram.run() -if __name__ == '__main__': +if __name__ == "__main__": main()