|
@@ -1,5 +1,6 @@
|
|
|
import logging
|
|
|
import os
|
|
|
+from subprocess import CalledProcessError
|
|
|
|
|
|
from telegram.ext import Updater, MessageHandler, Filters
|
|
|
|
|
@@ -8,37 +9,32 @@ 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 error_callback(update, context):
|
|
|
+ logger.warning('Update "%s" caused error "%s"', update, context.error)
|
|
|
|
|
|
-def get_format(update, context):
|
|
|
+def get_package(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=CONF_FILE)
|
|
|
|
|
|
- }
|
|
|
- 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...",
|
|
|
+ msg = context.bot.send_message(text=f"Downloading {apk.package_name}...",
|
|
|
chat_id=update.message.chat_id)
|
|
|
try:
|
|
|
apk.download()
|
|
|
+ with apk.send() as files:
|
|
|
+ for f in files:
|
|
|
+ context.bot.send_document(chat_id=update.message.chat_id, document=open(f, 'rb'))
|
|
|
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'))
|
|
|
+ except CalledProcessError:
|
|
|
+ context.bot.send_message(text="Failed to send APK file",
|
|
|
+ chat_id=update.message.chat_id)
|
|
|
|
|
|
context.bot.delete_message(chat_id=update.message.chat_id,
|
|
|
message_id=msg.message_id)
|
|
@@ -50,9 +46,18 @@ if os.environ.get('CONF_FOLDER') is None:
|
|
|
TOKEN = None
|
|
|
with open(os.path.join(os.environ['CONF_FOLDER'], 'bot.token')) as f:
|
|
|
TOKEN = f.read().strip()
|
|
|
+
|
|
|
+CONF = {
|
|
|
+ 'yes': True,
|
|
|
+ 'verbose': False,
|
|
|
+ 'tokencachefile': os.path.join(os.environ['CONF_FOLDER'], 'token.cache'),
|
|
|
+}
|
|
|
+CONF_FILE = os.path.join(os.environ['CONF_FOLDER'], 'gplaycli.conf')
|
|
|
+
|
|
|
updater = Updater(token=TOKEN)
|
|
|
|
|
|
-updater.dispatcher.add_handler(MessageHandler(Filters.text, get_format))
|
|
|
+updater.dispatcher.add_handler(MessageHandler(Filters.text, get_package))
|
|
|
+updater.dispatcher.add_error_handler(error_callback)
|
|
|
|
|
|
updater.start_polling()
|
|
|
updater.idle()
|