Skip to content

Commit

Permalink
Added /add_paused command (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
noam09 committed Apr 20, 2019
1 parent f432ac3 commit d66dc1a
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions telegramer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def filter(self, message):
"urllib3_proxy_kwargs_password": "",
"regex_exp": {},
"categories": {},
"minimum_speed": -1,
"user_timer": 60
"minimum_speed": int(-1),
"user_timer": int(60)
}


Expand Down Expand Up @@ -241,7 +241,7 @@ def enable(self):
'help': self.cmd_help,
'start': self.cmd_help,
'reload': self.restart_telegramer,
'rss': self.cmd_add_rss,
# 'rss': self.cmd_add_rss,
'commands': self.cmd_help}

log.debug(prelog() + 'Initialize bot')
Expand Down Expand Up @@ -301,6 +301,19 @@ def enable(self):
},
fallbacks=[CommandHandler('cancel', self.cancel)]
)
# Add torrent paused
conv_handler_paused = ConversationHandler(
entry_points=[CommandHandler('add_paused', self.add_paused)],
states={
CATEGORY: [MessageHandler(Filters.text, self.category)],
SET_LABEL: [MessageHandler(Filters.text, self.set_label)],
TORRENT_TYPE: [MessageHandler(Filters.text, self.torrent_type)],
ADD_MAGNET: [MessageHandler(Filters.text, self.add_magnet)],
ADD_TORRENT: [MessageHandler(Filters.document, self.add_torrent)],
ADD_URL: [MessageHandler(Filters.text, self.add_url)]
},
fallbacks=[CommandHandler('cancel', self.cancel)]
)
conv_handler_rss = ConversationHandler(
entry_points=[CommandHandler('rss', self.cmd_add_rss)],
states={
Expand All @@ -313,6 +326,7 @@ def enable(self):
)

dp.add_handler(conv_handler)
dp.add_handler(conv_handler_paused)
dp.add_handler(conv_handler_rss)
dp.add_handler(MessageHandler(Filters.document, self.add_torrent))
dp.add_handler(MessageHandler(filter_magnets, self.find_magnet))
Expand Down Expand Up @@ -435,6 +449,7 @@ def cmd_help(self, bot, update):
if str(update.message.chat.id) in self.whitelist:
log.debug(prelog() + str(update.message.chat.id) + " in whitelist")
help_msg = ['/add - Add a new torrent',
'/add_paused - Add a new torrent paused',
'/rss - Add a new RSS filter',
'/list - List all torrents',
'/down - List downloading torrents',
Expand Down Expand Up @@ -483,6 +498,7 @@ def cmd_paused(self, bot, update):
def add(self, bot, update):
# log.error(type(update.message.chat.id) + str(update.message.chat.id))
if str(update.message.chat.id) in self.whitelist:
self.opts = {}
self.is_rss = False
self.magnet_only = False
return self.prepare_categories(bot, update)
Expand All @@ -493,6 +509,15 @@ def add(self, bot, update):
return self.prepare_categories(bot, update)
"""

def add_paused(self, bot, update):
# log.error(type(update.message.chat.id) + str(update.message.chat.id))
if str(update.message.chat.id) in self.whitelist:
self.opts = {}
self.opts["add_paused"] = True
self.is_rss = False
self.magnet_only = False
return self.prepare_categories(bot, update)

def cmd_add_rss(self, bot, update):
# log.error(type(update.message.chat.id) + str(update.message.chat.id))
if str(update.message.chat.id) in self.whitelist:
Expand Down Expand Up @@ -574,7 +599,7 @@ def category(self, bot, update):
if str(update.message.chat.id) in self.whitelist:
try:
if STRINGS['no_category'] == update.message.text:
self.opts = {}
self.opts = self.opts
else:
if update.message.text in self.config["categories"].keys():
# move_completed_path vs download_location
Expand All @@ -584,7 +609,7 @@ def category(self, bot, update):

# If none of the existing categories were selected,
# maybe user is trying to save to a new directory
if not self.opts:
else: # if not self.opts:
try:
log.debug(prelog() + 'Custom directory entered: ' +
str(update.message.text))
Expand All @@ -598,8 +623,8 @@ def category(self, bot, update):
if not os.path.exists(otherpath):
log.debug(prelog() + 'mkdir {}'.format(otherpath))
os.makedirs(otherpath)
self.opts = {'move_completed_path': otherpath,
'move_completed': True}
self.opts["move_completed_path"] = otherpath
self.opts["move_completed"] = True
except Exception as e:
log.error(prelog() + str(e) + '\n' +
traceback.format_exc())
Expand Down Expand Up @@ -982,14 +1007,14 @@ def update_stats(self):
log.debug('update_stats')

def check_speed(self):
log.debug("Minimum speed: %s" , self.config['minimum_speed'])
log.debug("Minimum speed: %s", self.config["minimum_speed"])
try:
for t in component.get('TorrentManager').torrents.values():
if t.get_status(('state',))['state'] == 'Downloading' :
if t.get_status(("state",))["state"] == "Downloading":
if t.status.download_rate < (self.config['minimum_speed'] * 1024):
message = _('Torrent *%(name)s* is slower than minimum speed!') % t.get_status({})
message = _('Torrent *%(name)s* is slower than minimum speed!') % t.get_status({})
self.telegram_send(message, to=self.notifylist, parse_mode='Markdown')
except Exception as e :
except Exception as e:
log.error(prelog() + 'Unexpected behavior %s.' % str(e))
return

Expand Down

0 comments on commit d66dc1a

Please sign in to comment.