agent.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import asyncio
  2. from config import ENTITY, API_ID, API_HASH, PHONE, BOT_NAME, DEFAULT_SPEAKER_NAME, ALBUM_NAME, GENRE
  3. # TODO https://habr.com/ru/post/348234/ файлы больше 50мб
  4. # https://docs.telethon.dev/en/latest/
  5. # https://pypi.org/project/telegram-cloud/
  6. from telethon import TelegramClient
  7. from telethon.tl.types import DocumentAttributeAudio
  8. import mimetypes
  9. import os
  10. from mutagen.mp3 import MP3
  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"img/{artist}.jpg"
  25. if os.path.isfile(cover_path):
  26. album_cover_path = cover_path
  27. else:
  28. album_cover_path = "img/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())
  44. # client = TelegramClient(ENTITY, API_ID, API_HASH, update_workers=None, spawn_read_thread=False)
  45. # client.connect()
  46. # if not client.is_user_authorized():
  47. # # client.send_code_request(phone) # при первом запуске - раскомментить, после авторизации для избежания FloodWait
  48. # # советую закомментить
  49. # client.sign_in(PHONE, input('Enter code: '))
  50. # client.start()
  51. #
  52. #
  53. # def main(argv):
  54. # file_path = argv[1]
  55. # file_name = argv[2]
  56. # chat_id = argv[3]
  57. # object_id = argv[4]
  58. # bot_name = argv[5]
  59. # duration = argv[6]
  60. # mimetypes.add_type('audio/aac','.aac')
  61. # mimetypes.add_type('audio/ogg','.ogg')
  62. # msg = client.send_file(
  63. # str(bot_name),
  64. # file_path,
  65. # caption=str(chat_id + ':' + object_id + ':' + duration),
  66. # file_name=str(file_name),
  67. # use_cache=False,
  68. # part_size_kb=512,
  69. # attributes=[DocumentAttributeAudio(
  70. # int(duration),
  71. # voice=None,
  72. # title=file_name[:-4],
  73. # performer='')]
  74. # )
  75. # client.disconnect()
  76. # return 0