agent.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import asyncio
  2. from config import ENTITY, API_ID, API_HASH, PHONE, BOT_NAME, DEFAULT_SPEAKER_NAME, ALBUM_NAME, GENRE
  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, 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. chat_id = 'chat'
  18. object_id = 'obj'
  19. duration = mp3_file_info.length
  20. if artist == "":
  21. artist = DEFAULT_SPEAKER_NAME
  22. if title == "":
  23. title = filename
  24. cover_path = f"covers/{artist}.jpg"
  25. if os.path.isfile(cover_path):
  26. album_cover_path = cover_path
  27. else:
  28. album_cover_path = "covers/cover.jpg"
  29. async def send_file():
  30. await client.send_file(
  31. BOT_NAME,
  32. filename,
  33. caption=str(chat_id + ':' + object_id + ':' + str(duration)),
  34. use_cache=False,
  35. part_size_kb=512,
  36. thumb=album_cover_path,
  37. attributes=[DocumentAttributeAudio(
  38. int(duration),
  39. title=title,
  40. performer=artist)]
  41. )
  42. loop = asyncio.get_event_loop()
  43. loop.run_until_complete(send_file())