Skip to content

Commit

Permalink
Add stop task
Browse files Browse the repository at this point in the history
  • Loading branch information
wrenfairbank committed Jun 27, 2020
1 parent 6a0ed03 commit 49f96d3
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 169 deletions.
11 changes: 7 additions & 4 deletions telegram_gcloner/handlers/process_drive_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import html
import logging
import re
import threading

from telegram import ParseMode, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Dispatcher, CallbackQueryHandler

from utils.fire_save_files import fire_save_files
from utils.fire_save_files import MySaveFileThread
from utils.google_drive import GoogleDrive
from utils.helper import parse_folder_id_from_url, alert_users, get_inline_keyboard_pagination_data, simplified_path

Expand Down Expand Up @@ -100,7 +99,8 @@ def process_drive_links(update, context):
else:
inline_keyboard_drive_ids = [[InlineKeyboardButton(text='未收藏团队盘,先收藏才能操作。', callback_data='#')]]
inline_keyboard = inline_keyboard_drive_ids
update.message.reply_text(message, parse_mode=ParseMode.HTML, reply_markup=InlineKeyboardMarkup(inline_keyboard))
update.message.reply_text(message, parse_mode=ParseMode.HTML,
disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup(inline_keyboard))


def save_to_folder_page(update, context):
Expand Down Expand Up @@ -151,7 +151,10 @@ def save_to_folder(update, context):
return
dest_folder = fav_folders[match.group(1)]
dest_folder['folder_id'] = match.group(1)
t = threading.Thread(target=fire_save_files, args=(update, context, folder_ids, text, dest_folder))
if not context.user_data.get('tasks', None):
context.user_data['tasks'] = []
t = MySaveFileThread(args=(update, context, folder_ids, text, dest_folder))
context.user_data['tasks'].append(t)
t.start()
query.message.edit_reply_markup(reply_markup=InlineKeyboardMarkup(
[[InlineKeyboardButton(text='已执行', callback_data='#')]]))
36 changes: 36 additions & 0 deletions telegram_gcloner/handlers/stop_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import logging
import re

from telegram.ext import Dispatcher, CallbackQueryHandler

from utils.helper import alert_users
from utils.restricted import restricted

logger = logging.getLogger(__name__)

regex_stop_task = r'^stop_task,(\d+)'


def init(dispatcher: Dispatcher):
"""Provide handlers initialization."""
dispatcher.add_handler(CallbackQueryHandler(stop_task, pattern=regex_stop_task))


@restricted
def stop_task(update, context):
query = update.callback_query
if query.data:
match = re.search(regex_stop_task, query.data)
if match:
thread_id = int(match.group(1))
tasks = context.user_data.get('tasks', None)
if tasks:
for t in tasks:
if t.native_id == thread_id:
t.kill()
return
alert_users(context, update.effective_user, 'invalid query data', query.data)
query.answer(text='哟呵', show_alert=True)
return
9 changes: 7 additions & 2 deletions telegram_gcloner/telegram_gcloner.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,20 +191,25 @@ def error(update, context):
# normally, we always have an user. If not, its either a channel or a poll update.
if update.effective_user:
payload += f' with the user ' \
f'{mention_html(update.effective_user.id, html.escape(update.effective_user.first_name))}'
f'{mention_html(update.effective_user.id, html.escape(update.effective_user.first_name))} '
# there are more situations when you don't get a chat
if update.effective_chat:
if update.effective_chat.title:
payload += f' within the chat <i>{html.escape(update.effective_chat.title)}</i>'
if update.effective_chat.username:
payload += f' (@{update.effective_chat.username})'
payload += f' (@{update.effective_chat.username}, {update.effective_chat.id})'
# but only one where you have an empty payload by now: A poll (buuuh)
if update.poll:
payload += f' with the poll id {update.poll.id}.'
# lets put this in a "well" formatted text
text = f"Hey.\n The error <code>{html.escape(str(context.error))}</code> happened{str(payload)}. " \
f"The full traceback:\n\n<code>{html.escape(str(trace))}" \
f"</code>"

# ignore message is not modified error from telegram
if 'Message is not modified' in context.error:
raise

# and send it to the dev(s)
for dev_id in devs:
context.bot.send_message(dev_id, text, parse_mode=ParseMode.HTML)
Expand Down
Loading

0 comments on commit 49f96d3

Please sign in to comment.