telegram-apk-downloader/src/main.py
2020-11-25 22:40:41 +01:00

60 行
2.0 KiB
Python

import logging
import os
from telegram.ext import Updater, MessageHandler, Filters
from download_utils import APK, BadPackageNameException
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
def get_format(update, context):
logger.info("from {}: {}".format(update.message.chat_id, update.message.text)) # "history"
if update.message.text == '/start':
update.message.reply_text("To start downloading APK file send me a PlayStore link ok a package name. :)")
return
conf = {
'yes': True,
'verbose': False,
'tokencachefile': os.path.join(os.environ['CONF_FOLDER'], 'token.cache'),
}
apk = APK(update.message.text, conf=conf, conf_file=os.path.join(os.environ['CONF_FOLDER'], 'gplaycli.conf'))
msg = context.bot.send_message(text="Downloading...",
chat_id=update.message.chat_id)
try:
apk.download()
except BadPackageNameException as e:
# instead of editing the text message we delete and resend one so a new notification will trigger
context.bot.delete_message(chat_id=update.message.chat_id,
message_id=msg.message_id)
context.bot.send_message(text="Bad package name: {}".format(e),
chat_id=update.message.chat_id)
return
with apk.send() as files:
for f in files:
context.bot.send_document(chat_id=update.message.chat_id, document=open(f, 'rb'))
context.bot.delete_message(chat_id=update.message.chat_id,
message_id=msg.message_id)
if os.environ.get('CONF_FOLDER') is None:
logger.error("No conf folder available")
exit(1)
TOKEN = None
with open(os.path.join(os.environ['CONF_FOLDER'], 'bot.token')) as f:
TOKEN = f.read().strip()
logger.error(TOKEN)
updater = Updater(token=TOKEN)
updater.dispatcher.add_handler(MessageHandler(Filters.text, get_format))
updater.start_polling()
updater.idle()