main.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import os
  2. import logging
  3. from glob import glob
  4. import youtube_dl
  5. from telegram.ext import Updater, MessageHandler, Filters
  6. from vid_utils import check_dimension
  7. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
  8. logger = logging.getLogger(__name__)
  9. updater = Updater(token='TOKEN') # put here the bot's token
  10. dispatcher = updater.dispatcher
  11. ydl_opts = {
  12. 'restrictfilenames': True,
  13. }
  14. def download(bot, update):
  15. for f in glob('*.mp4*') + glob('*.webm*'): # with glob it isn't possible to check multiple extension in one regex
  16. os.remove(f) # remove old video(s)
  17. try:
  18. with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  19. ydl.download([update.message.text])
  20. for f in glob('*.mp4*') + glob('*.webm*'): # if the video is bigger than 50MB split it
  21. check_dimension(f)
  22. break # check first file
  23. for f in glob('*.mp4*') + glob('*.webm*'): # send document(s)
  24. bot.send_document(chat_id=update.message.chat_id, document=open(f, 'rb'))
  25. except Exception as e:
  26. bot.sendMessage(chat_id=update.message.chat_id, text='Error: {}'.format(e))
  27. logger.info(e)
  28. download_handler = MessageHandler(Filters.text, download)
  29. dispatcher.add_handler(download_handler)
  30. updater.start_polling()
  31. updater.idle()