Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add identifier normalizer function and Apply PEP8 formatting #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
34 changes: 17 additions & 17 deletions tsut/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import argparse
from .service import TSUT
import sys
import logging
import sys

from .service import TSUT
from .utils import id_normalizer


async def perform():
parser = argparse.ArgumentParser()
Expand All @@ -28,45 +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 = args.channel_id

if not channel_id.startswith('@'):
channel_id = '@' + channel_id



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()
run()
12 changes: 6 additions & 6 deletions tsut/service.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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:
Expand All @@ -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(
Expand All @@ -37,15 +37,15 @@ 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]
os.rename(os.path.join(os.getcwd(), i), os.path.join(os.getcwd(), newname))

await self.split_audio(newname)


async def split_audio(self, newname):
logging.info(f"Splitting audio {newname}...")
process = await asyncio.create_subprocess_shell(
Expand Down
5 changes: 5 additions & 0 deletions tsut/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def id_normalizer(identifier: str) -> str:
if identifier.startswith('@'):
return identifier
else:
return '@' + identifier