From ca2d43a49c048fcac47989c832ebf3e474b22c2d Mon Sep 17 00:00:00 2001 From: EngRobot33 Date: Thu, 20 Jun 2024 19:00:54 +0330 Subject: [PATCH 1/3] docs(): update .gitignore for excluding .idea directory --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 51e9d59..3611a98 100644 --- a/.gitignore +++ b/.gitignore @@ -157,4 +157,4 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +.idea/ From 7420cf1daf8220d59a88d3fcd7639fe1061f7e0f Mon Sep 17 00:00:00 2001 From: EngRobot33 Date: Thu, 20 Jun 2024 19:01:47 +0330 Subject: [PATCH 2/3] feat(): add identifier normalizer function --- tsut/cli.py | 7 +++---- tsut/utils.py | 5 +++++ 2 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 tsut/utils.py diff --git a/tsut/cli.py b/tsut/cli.py index 0840d64..5e14250 100644 --- a/tsut/cli.py +++ b/tsut/cli.py @@ -1,4 +1,6 @@ import argparse + +from tsut.utils import id_normalizer from .service import TSUT import sys import logging @@ -53,10 +55,7 @@ async def perform(): logging.error("Error: bot token has not been provided.") sys.exit(1) - channel_id = args.channel_id - - if not channel_id.startswith('@'): - channel_id = '@' + channel_id + channel_id = id_normalizer(args.channel_id) diff --git a/tsut/utils.py b/tsut/utils.py new file mode 100644 index 0000000..2060215 --- /dev/null +++ b/tsut/utils.py @@ -0,0 +1,5 @@ +def id_normalizer(identifier: str) -> str: + if identifier.startswith('@'): + return identifier + else: + return '@' + identifier From b97b0684430816056079027f112fec085c1e71bc Mon Sep 17 00:00:00 2001 From: EngRobot33 Date: Thu, 20 Jun 2024 19:04:49 +0330 Subject: [PATCH 3/3] style(): Apply PEP8 formatting to project files --- tsut/cli.py | 29 +++++++++++++++-------------- tsut/service.py | 12 ++++++------ 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/tsut/cli.py b/tsut/cli.py index 5e14250..36c882f 100644 --- a/tsut/cli.py +++ b/tsut/cli.py @@ -1,9 +1,10 @@ import argparse +import logging +import sys -from tsut.utils import id_normalizer from .service import TSUT -import sys -import logging +from .utils import id_normalizer + async def perform(): parser = argparse.ArgumentParser() @@ -30,42 +31,42 @@ async def perform(): "--bot-token", help="Telegram bot token." ) - + parser.add_argument( "-C", "--channel-id", help="Telegram channel ID." ) - + args = parser.parse_args() - + if args.link is None: logging.error("Error: space link has not been provided.") sys.exit(1) - + if args.cookie_file is None: logging.error("Error: twitter cookie file has not been provided.") sys.exit(1) - + if args.channel_id is None: logging.error("Error: channel id has not been provided.") sys.exit(1) if args.bot_token is None: - logging.error("Error: bot token has not been provided.") + logging.error("Error: bot token has not been provided.") sys.exit(1) - + channel_id = id_normalizer(args.channel_id) - - t = TSUT(args.bot_token, channel_id, args.link, args.cookie_file) await t.download_audio() await t.upload_to_telegram() + def run(): import asyncio asyncio.run(perform()) - + + if __name__ == "__main__": - run() \ No newline at end of file + run() diff --git a/tsut/service.py b/tsut/service.py index 0bd1854..8c655b3 100644 --- a/tsut/service.py +++ b/tsut/service.py @@ -1,8 +1,10 @@ import asyncio -import os import glob -from telegram import Bot import logging +import os + +from telegram import Bot + class TSUT: def __init__(self, telegram_bot_token: str, telegram_channel_id: str, space_link: str, cookie_file: str): @@ -11,7 +13,6 @@ def __init__(self, telegram_bot_token: str, telegram_channel_id: str, space_link self.space_link = space_link self.cookie_file = cookie_file - async def upload_to_telegram(self, directory="."): bot = Bot(self.telegram_bot_token) with open('output.txt', 'r') as desc: @@ -20,7 +21,6 @@ async def upload_to_telegram(self, directory="."): if file.endswith('m4a'): await bot.send_audio(chat_id=self.telegram_channel_id, audio=file) - async def download_audio(self): logging.info("Downloading content has been started...") process = await asyncio.create_subprocess_shell( @@ -37,7 +37,8 @@ async def download_audio(self): with open('output.txt', 'w') as file: for i in text: x = i.split(";") - file.write(str(x[0])+'\n'+'\n'+str(x[3])+' ('+str(x[2])+')'+'\n'+'\n'+str(x[1])+'\n'+'\n'+'\n'+self.space_link) + file.write(str(x[0]) + '\n' + '\n' + str(x[3]) + ' (' + str(x[2]) + ')' + '\n' + '\n' + str( + x[1]) + '\n' + '\n' + '\n' + self.space_link) newname = str(x[0]) if len(newname) == 0: newname = x[3] @@ -45,7 +46,6 @@ async def download_audio(self): await self.split_audio(newname) - async def split_audio(self, newname): logging.info(f"Splitting audio {newname}...") process = await asyncio.create_subprocess_shell(