main.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import logging
  2. import os
  3. from telegram.ext import Updater, MessageHandler, Filters
  4. from download_utils import APK, BadPackageNameException
  5. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
  6. logger = logging.getLogger(__name__)
  7. def get_format(update, context):
  8. logger.info("from {}: {}".format(update.message.chat_id, update.message.text)) # "history"
  9. if update.message.text == '/start':
  10. update.message.reply_text("To start downloading APK file send me a PlayStore link ok a package name. :)")
  11. return
  12. conf = {
  13. 'yes': True,
  14. 'verbose': False,
  15. 'tokencachefile': os.path.join(os.environ['CONF_FOLDER'], 'token.cache'),
  16. }
  17. apk = APK(update.message.text, conf=conf, conf_file=os.path.join(os.environ['CONF_FOLDER'], 'gplaycli.conf'))
  18. msg = context.bot.send_message(text="Downloading...",
  19. chat_id=update.message.chat_id)
  20. try:
  21. apk.download()
  22. except BadPackageNameException as e:
  23. # instead of editing the text message we delete and resend one so a new notification will trigger
  24. context.bot.delete_message(chat_id=update.message.chat_id,
  25. message_id=msg.message_id)
  26. context.bot.send_message(text="Bad package name: {}".format(e),
  27. chat_id=update.message.chat_id)
  28. return
  29. with apk.send() as files:
  30. for f in files:
  31. context.bot.send_document(chat_id=update.message.chat_id, document=open(f, 'rb'))
  32. context.bot.delete_message(chat_id=update.message.chat_id,
  33. message_id=msg.message_id)
  34. if os.environ.get('CONF_FOLDER') is None:
  35. logger.error("No conf folder available")
  36. exit(1)
  37. TOKEN = None
  38. with open(os.path.join(os.environ['CONF_FOLDER'], 'bot.token')) as f:
  39. TOKEN = f.read().strip()
  40. logger.error(TOKEN)
  41. updater = Updater(token=TOKEN)
  42. updater.dispatcher.add_handler(MessageHandler(Filters.text, get_format))
  43. updater.start_polling()
  44. updater.idle()