1+ import asyncio
12from pathlib import Path
23
3- from pyrogram import Client
4- from pyrogram .enums import ParseMode
4+ from telethon import TelegramClient
55
66from utils .logger_manager import logger
77from utils .utils import read_telegram_config
@@ -17,58 +17,71 @@ def __init__(self):
1717
1818 self .api_id = config ["api_id" ]
1919 self .api_hash = config ["api_hash" ]
20- self .bot_token = config ["bot_token" ]
2120 self .chat_id = config ["chat_id" ]
2221
23- self .app = Client (
24- "telegram_session " ,
22+ self .client = TelegramClient (
23+ "tiktok_live_recorder_session " ,
2524 api_id = self .api_id ,
2625 api_hash = self .api_hash ,
27- bot_token = self .bot_token ,
2826 )
2927
3028 def upload (self , file_path : str ):
3129 """
32- Upload a file to the bot 's own chat (saved messages) .
30+ Upload a file to the user 's Saved Messages via Telethon .
3331 """
34- try :
35- self .app .start ()
36-
37- me = self .app .get_me ()
38- is_premium = me .is_premium
39- max_size = (
40- PREMIUM_USER_MAX_FILE_SIZE if is_premium else FREE_USER_MAX_FILE_SIZE
41- )
42-
43- file_size = Path (file_path ).stat ().st_size
44- logger .info (
45- f"File to upload: { Path (file_path ).name } "
46- f"({ round (file_size / (1024 * 1024 ))} MB)"
47- )
48-
49- if file_size > max_size :
50- logger .warning (
51- "The file is too large to be uploaded with this type of account."
32+
33+ async def _upload ():
34+ try :
35+ await self .client .connect ()
36+
37+ if not await self .client .is_user_authorized ():
38+ await self .client .start ()
39+
40+ me = await self .client .get_me ()
41+ is_premium = me .premium
42+ max_size = (
43+ PREMIUM_USER_MAX_FILE_SIZE
44+ if is_premium
45+ else FREE_USER_MAX_FILE_SIZE
46+ )
47+
48+ file_size = Path (file_path ).stat ().st_size
49+ logger .info (
50+ f"File to upload: { Path (file_path ).name } "
51+ f"({ round (file_size / (1024 * 1024 ))} MB)"
52+ )
53+
54+ if file_size > max_size :
55+ logger .warning (
56+ "The file is too large to be uploaded "
57+ "with this type of account."
58+ )
59+ return
60+
61+ logger .info (
62+ "Uploading video on Telegram... "
63+ "This may take a while depending on file size."
64+ )
65+
66+ await self .client .send_file (
67+ entity = self .chat_id ,
68+ file = file_path ,
69+ caption = (
70+ "🎥 <b>Video recorded via "
71+ '<a href="https://github.com/Michele0303/'
72+ 'tiktok-live-recorder">'
73+ "TikTok Live Recorder</a></b>"
74+ ),
75+ parse_mode = "html" ,
76+ force_document = True ,
5277 )
53- return
54-
55- logger .info (
56- "Uploading video on Telegram... This may take a while depending on the file size."
57- )
58- self .app .send_document (
59- chat_id = self .chat_id ,
60- document = file_path ,
61- caption = (
62- '🎥 <b>Video recorded via <a href="https://github.com/Michele0303/tiktok-live-recorder">'
63- "TikTok Live Recorder</a></b>"
64- ),
65- parse_mode = ParseMode .HTML ,
66- force_document = True ,
67- )
68- logger .info ("File successfully uploaded to Telegram.\n " )
69-
70- except Exception as e :
71- logger .error (f"Error during Telegram upload: { e } \n " )
72-
73- finally :
74- self .app .stop ()
78+
79+ logger .info ("File successfully uploaded to Telegram.\n " )
80+
81+ except Exception as e :
82+ logger .error (f"Error during Telegram upload: { e } \n " )
83+
84+ finally :
85+ await self .client .disconnect ()
86+
87+ asyncio .run (_upload ())
0 commit comments