123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import asyncio
- from config import ENTITY, API_ID, API_HASH, PHONE, BOT_NAME, DEFAULT_SPEAKER_NAME, ALBUM_NAME, GENRE
- # TODO https://habr.com/ru/post/348234/ файлы больше 50мб
- # https://docs.telethon.dev/en/latest/
- # https://pypi.org/project/telegram-cloud/
- from telethon import TelegramClient
- from telethon.tl.types import DocumentAttributeAudio
- import mimetypes
- import os
- from mutagen.mp3 import MP3
- def send_audio(filename, artist="", title=""):
- client = TelegramClient(ENTITY, API_ID, API_HASH)
- client.start(phone=PHONE)
- mimetypes.add_type('audio/aac', '.aac')
- mimetypes.add_type('audio/ogg', '.ogg')
- mp3_file_info = MP3(filename).info
- chat_id = 'chat'
- object_id = 'obj'
- duration = mp3_file_info.length
- if artist == "":
- artist = DEFAULT_SPEAKER_NAME
- if title == "":
- title = filename
-
- cover_path = f"img/{artist}.jpg"
- if os.path.isfile(cover_path):
- album_cover_path = cover_path
- else:
- album_cover_path = "img/cover.jpg"
- async def send_file():
- await client.send_file(
- BOT_NAME,
- filename,
- caption=str(chat_id + ':' + object_id + ':' + str(duration)),
- use_cache=False,
- part_size_kb=512,
- thumb=album_cover_path,
- attributes=[DocumentAttributeAudio(
- int(duration),
- title=title,
- performer=artist)]
- )
- loop = asyncio.get_event_loop()
- loop.run_until_complete(send_file())
- # client = TelegramClient(ENTITY, API_ID, API_HASH, update_workers=None, spawn_read_thread=False)
- # client.connect()
- # if not client.is_user_authorized():
- # # client.send_code_request(phone) # при первом запуске - раскомментить, после авторизации для избежания FloodWait
- # # советую закомментить
- # client.sign_in(PHONE, input('Enter code: '))
- # client.start()
- #
- #
- # def main(argv):
- # file_path = argv[1]
- # file_name = argv[2]
- # chat_id = argv[3]
- # object_id = argv[4]
- # bot_name = argv[5]
- # duration = argv[6]
- # mimetypes.add_type('audio/aac','.aac')
- # mimetypes.add_type('audio/ogg','.ogg')
- # msg = client.send_file(
- # str(bot_name),
- # file_path,
- # caption=str(chat_id + ':' + object_id + ':' + duration),
- # file_name=str(file_name),
- # use_cache=False,
- # part_size_kb=512,
- # attributes=[DocumentAttributeAudio(
- # int(duration),
- # voice=None,
- # title=file_name[:-4],
- # performer='')]
- # )
- # client.disconnect()
- # return 0
|