agent.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import asyncio
  2. from config import ENTITY, API_ID, API_HASH, PHONE, BOT_NAME, DEFAULT_SPEAKER_NAME
  3. # https://docs.telethon.dev/en/latest/
  4. # https://pypi.org/project/telegram-cloud/
  5. from telethon import TelegramClient
  6. from telethon.tl.types import DocumentAttributeAudio
  7. import mimetypes
  8. import os
  9. from mutagen.mp3 import MP3
  10. # TODO add Exceptions hendling and typing
  11. def send_audio(filename, artist="", title=""):
  12. client = TelegramClient(ENTITY, int(API_ID), API_HASH)
  13. client.start(phone=PHONE)
  14. mimetypes.add_type('audio/aac', '.aac')
  15. mimetypes.add_type('audio/ogg', '.ogg')
  16. mp3_file_info = MP3(filename).info
  17. duration = mp3_file_info.length
  18. if artist == "":
  19. artist = DEFAULT_SPEAKER_NAME
  20. if title == "":
  21. title = filename
  22. cover_path = f"covers/{artist}.jpg"
  23. if os.path.isfile(cover_path):
  24. album_cover_path = cover_path
  25. else:
  26. album_cover_path = "covers/cover.jpg"
  27. async def send_file():
  28. await client.send_file(
  29. BOT_NAME,
  30. filename,
  31. caption=str(artist + '=' + title + '=' + str(duration)),
  32. use_cache=False,
  33. part_size_kb=512,
  34. thumb=album_cover_path,
  35. attributes=[DocumentAttributeAudio(
  36. int(duration),
  37. title=title,
  38. performer=artist)]
  39. )
  40. loop = asyncio.get_event_loop()
  41. loop.run_until_complete(send_file())