コード例 #1
0
ファイル: toolbox.py プロジェクト: dmzkrsk/torrt
def configure_notifier(notifier_alias, settings_dict):
    """Configures notifier using given settings.
    Saves successful configuration.

    :param notifier_alias: notifier alias
    :param settings_dict: settings dictionary to configure notifier with
    :return:
    """
    LOGGER.info('Configuring `%s` notifier ...', notifier_alias)

    notification_class = NotifierClassesRegistry.get(notifier_alias)
    if notification_class is not None:
        notifier = notification_class.spawn_with_settings(settings_dict)
        configured = notifier.test_configuration()
        if configured:
            notifier.save_settings()
            LOGGER.info('Notifier `%s` is configured', notifier_alias)
        else:
            LOGGER.error('Notifier `%s` configuration failed. Check your settings', notifier_alias)
    else:
        LOGGER.error('Notifier `%s` is unknown', notifier_alias)
コード例 #2
0
ファイル: mail.py プロジェクト: dmzkrsk/torrt
            except Exception as e:
                LOGGER.error(e)
                return
        if self.user and self.password:
            try:
                connection.login(self.user, self.password)
            except SMTPAuthenticationError as e:
                LOGGER.error(e)
                return
        return connection

    def send_message(self, msg):
        self.connection.sendmail(self.sender, [self.email], msg)

    def test_configuration(self):
        return bool(self.connection)

    def make_message(self, torrent_data):
        text = '''The following torrents were updated:\n%s\n\nBest regards,\ntorrt.''' \
               % '\n'.join(map(lambda t: t['name'], torrent_data.values()))

        msg = MIMEText(text)
        msg['Subject'] = 'New torrents were added to download queue.'
        msg['From'] = self.sender
        msg['To'] = self.email
        LOGGER.info('Notification message was sent to user %s' % self.email)
        return msg.as_string()


NotifierClassesRegistry.add(EmailNotifier)
コード例 #3
0
    def __init__(self, token, chat_id):
        """
        :param token: str - Telegram's bot token
        :param chat_id: str - Telegram's chat ID
        """

        self.token = token
        self.chat_id = chat_id

    def make_message(self, torrent_data):
        return '''The following torrents were updated:\n%s''' \
               % '\n'.join(map(lambda t: t['name'], torrent_data.values()))

    def test_configuration(self):
        url = '%s%s/getMe' % (self.url, self.token)
        r = requests.get(url)
        return r.json().get('ok', False)

    def send_message(self, msg):
        url = '%s%s/sendMessage' % (self.url, self.token)
        r = requests.post(url, data={'chat_id': self.chat_id, 'text': msg})
        if r.json()['ok']:
            LOGGER.info('Telegram message was sent to user %s' % self.chat_id)
        else:
            LOGGER.error('Telegram notification not send: %s' %
                         r['description'])


NotifierClassesRegistry.add(TelegramNotifier)
コード例 #4
0
def process_commands():
    def settings_dict_from_list(lst):
        settings_dict = {}
        for s in lst:
            splitted = s.split('=')
            settings_dict[splitted[0]] = splitted[1]
        return settings_dict

    arg_parser = argparse.ArgumentParser(
        'torrt', description='Automates torrent updates for you.')
    arg_parser.add_argument('--version',
                            action='version',
                            version='%(prog)s ' + '.'.join(map(str, VERSION)))

    subp_main = arg_parser.add_subparsers(title='Supported commands',
                                          dest='command')

    subp_main.add_parser('list_rpc', help='Shows known RPCs aliases')
    subp_main.add_parser('list_trackers', help='Shows known trackers aliases')
    subp_main.add_parser('list_torrents',
                         help='Shows torrents registered for updates')
    subp_main.add_parser('list_notifiers', help='Shows configured notifiers')

    parser_configure_tracker = subp_main.add_parser(
        'configure_tracker',
        help='Sets torrent tracker settings (login credentials, etc.)',
        description=
        'E.g.: configure_tracker rutracker.org username=idle password=pSW0rt')
    parser_configure_tracker.add_argument(
        'tracker_alias',
        help='Tracker alias (usually domain) to apply settings to')
    parser_configure_tracker.add_argument(
        'settings',
        help='Settings string, format: setting1=val1 setting2=val2. '
        'Supported settings (any of): username, password',
        nargs='*')

    parser_configure_rpc = subp_main.add_parser(
        'configure_rpc',
        help='Sets RPCs settings (login credentials, etc.)',
        description='E.g.: configure_rpc transmission user=idle password=pSW0rt'
    )
    parser_configure_rpc.add_argument('rpc_alias',
                                      help='RPC alias to apply settings to')
    parser_configure_rpc.add_argument(
        'settings',
        help='Settings string, format: setting1=val1 setting2=val2. '
        'Supported settings (any of): url, host, port, user, password',
        nargs='*')

    parser_configure_notifier = subp_main.add_parser(
        'configure_notifier',
        help='Sets Notifiers settings (smtp credentials, etc.)',
        description=
        'E.g.: configure_notifier email [email protected] user=idle password=pSW0rt'
    )
    parser_configure_notifier.add_argument(
        'notifier_alias', help='Notifier alias to apply settings to')
    parser_configure_notifier.add_argument(
        'settings',
        help='Settings string, format: setting1=val1 setting2=val2. '
        'Supported settings for email notifier (any of): email, host, port, use_tls, user, password.'
        'Supported settings for telegram notifier: token, chat_id.',
        nargs='*')

    parser_configure_bot = subp_main.add_parser(
        'configure_bot',
        help='Sets Bot settings (token, etc.)',
        description='E.g.: configure_bot telegram token=YourBotSuperToken')
    parser_configure_bot.add_argument('bot_alias',
                                      help='Bot alias to apply settings to')
    parser_configure_bot.add_argument(
        'settings',
        help='Settings string, format: setting1=val1 setting2=val2. '
        'Supported settings for telegram bot: token.',
        nargs='*')

    parser_walk = subp_main.add_parser(
        'walk',
        help='Walks through registered torrents and performs automatic updates'
    )
    parser_walk.add_argument(
        '-f',
        help='Forces walk. Forced walks do not respect walk interval settings',
        dest='forced',
        action='store_true')
    parser_walk.add_argument(
        '--dump',
        help=
        'Dump web pages scraped by torrt into current or a given directory',
        dest='dump')

    parser_run_bots = subp_main.add_parser('run_bots',
                                           help='Run registered bots')
    parser_run_bots.add_argument('aliases',
                                 help='Bots to run aliases',
                                 nargs='*')

    parser_set_interval = subp_main.add_parser(
        'set_walk_interval',
        help=
        'Sets an interval *in hours* between consecutive torrent updates checks'
    )
    parser_set_interval.add_argument(
        'walk_interval',
        help='Interval *in hours* between consecutive torrent updates checks')

    parser_enable_rpc = subp_main.add_parser('enable_rpc',
                                             help='Enables RPC by its alias')
    parser_enable_rpc.add_argument('alias', help='Alias of RPC to enable')

    parser_disable_rpc = subp_main.add_parser('disable_rpc',
                                              help='Disables RPC by its alias')
    parser_disable_rpc.add_argument('alias', help='Alias of RPC to disable')

    parser_add_torrent = subp_main.add_parser(
        'add_torrent',
        help='Adds torrent from an URL both to torrt and torrent clients')
    parser_add_torrent.add_argument('url', help='URL to download torrent from')
    parser_add_torrent.add_argument(
        '-d',
        help=
        'Destination path to download torrent contents into (in filesystem where torrent client daemon works)',
        dest='download_to',
        default=None)
    parser_add_torrent.add_argument(
        '--dump',
        help=
        'Dump web pages scraped by torrt into current or a given directory',
        dest='dump')

    parser_remove_torrent = subp_main.add_parser(
        'remove_torrent',
        help='Removes torrent by its hash both from torrt and torrent clients')
    parser_remove_torrent.add_argument('hash', help='Torrent identifying hash')
    parser_remove_torrent.add_argument(
        '-d',
        help='If set data downloaded for torrent will also be removed',
        dest='delete_data',
        action='store_true')

    parser_register_torrent = subp_main.add_parser(
        'register_torrent',
        help=
        'Registers torrent within torrt by its hash (for torrents already existing at torrent clients)'
    )
    parser_register_torrent.add_argument('hash',
                                         help='Torrent identifying hash')
    parser_register_torrent.add_argument('-u',
                                         dest='url',
                                         default=None,
                                         help='URL to download torrent from')

    parser_unregister_torrent = subp_main.add_parser(
        'unregister_torrent',
        help='Unregisters torrent from torrt by its hash')
    parser_unregister_torrent.add_argument('hash',
                                           help='Torrent identifying hash')

    parser_remove_notifier = subp_main.add_parser(
        'remove_notifier', help='Remove configured notifier by its alias')
    parser_remove_notifier.add_argument('alias',
                                        help='Alias of notifier to remove')

    for parser in subp_main.choices.values():
        parser.add_argument('--verbose',
                            help='Switch to show debug messages',
                            dest='verbose',
                            action='store_true')

    args = arg_parser.parse_args()
    args = vars(args)

    configure_logging(logging.DEBUG if args.get('verbose') else logging.INFO)

    bootstrap()

    dump_into = args.get('dump')

    if dump_into:
        GlobalParam.set('dump_into', path.abspath(dump_into))

    if args['command'] == 'enable_rpc':
        toggle_rpc(args['alias'], True)

    elif args['command'] == 'disable_rpc':
        toggle_rpc(args['alias'], False)

    elif args['command'] == 'list_trackers':

        for tracker_alias, _ in TrackerClassesRegistry.get().items():
            LOGGER.info(tracker_alias)

    elif args['command'] == 'list_rpc':
        rpc_statuses = {}

        for rpc_alias, rpc in RPCClassesRegistry.get().items():
            rpc_statuses[rpc_alias] = 'unconfigured'

        for rpc_alias, rpc in RPCObjectsRegistry.get().items():
            rpc_statuses[rpc_alias] = 'enabled' if rpc.enabled else 'disabled'

        for rpc_alias, rpc_status in rpc_statuses.items():
            LOGGER.info(f'{rpc_alias}\t status={rpc_status}')

    elif args['command'] == 'list_torrents':
        for torrent_hash, torrent_data in get_registered_torrents().items():
            LOGGER.info(f"{torrent_hash}\t{torrent_data['name']}")

    elif args['command'] == 'list_notifiers':
        notifiers = {}
        for notifier_alias in NotifierClassesRegistry.get().keys():
            notifiers[notifier_alias] = 'unconfigured'

        for notifier_alias in NotifierObjectsRegistry.get().keys():
            notifiers[notifier_alias] = 'enabled'

        for notifier_alias, notifier_status in notifiers.items():
            LOGGER.info(f"{notifier_alias}\t status={notifier_status}")

    elif args['command'] == 'walk':
        walk(forced=args['forced'], silent=True)

    elif args['command'] == 'set_walk_interval':
        set_walk_interval(args['walk_interval'])

    elif args['command'] == 'add_torrent':
        add_torrent_from_url(args['url'], args['download_to'])

    elif args['command'] == 'remove_torrent':
        remove_torrent(args['hash'], args['delete_data'])

    elif args['command'] == 'register_torrent':
        register_torrent(args['hash'], url=args['url'])

    elif args['command'] == 'unregister_torrent':
        unregister_torrent(args['hash'])

    elif args['command'] == 'configure_rpc':
        configure_rpc(args['rpc_alias'],
                      settings_dict_from_list(args['settings']))

    elif args['command'] == 'configure_tracker':
        configure_tracker(args['tracker_alias'],
                          settings_dict_from_list(args['settings']))

    elif args['command'] == 'configure_notifier':
        configure_notifier(args['notifier_alias'],
                           settings_dict_from_list(args['settings']))

    elif args['command'] == 'remove_notifier':
        remove_notifier(args['alias'])

    elif args['command'] == 'configure_bot':
        configure_bot(args['bot_alias'],
                      settings_dict_from_list(args['settings']))

    elif args['command'] == 'run_bots':
        run_bots(args['aliases'])
コード例 #5
0
ファイル: telegram.py プロジェクト: idlesign/torrt
        self.chat_id = chat_id

    def make_message(self, torrent_data):
        return '''The following torrents were updated:\n%s''' \
               % '\n'.join(map(lambda t: t['name'], torrent_data.values()))

    def test_configuration(self):
        url = '%s%s/getMe' % (self.url, self.token)
        r = requests.get(url)
        return r.json().get('ok', False)

    def send_message(self, msg):
        url = '%s%s/sendMessage' % (self.url, self.token)
        try:
            response = requests.post(url, data={'chat_id': self.chat_id, 'text': msg})
        except RequestException as e:
            LOGGER.error('Failed to send Telegram message: %s', e)
        else:
            if response.ok:
                json_data = response.json()
                if json_data['ok']:
                    LOGGER.debug('Telegram message was sent to user %s', self.chat_id)
                else:
                    LOGGER.error('Telegram notification not send: %s', json_data['description'])
            else:
                LOGGER.error('Telegram notification not send. Response code: %s (%s)',
                             response.status_code, response.reason)


NotifierClassesRegistry.add(TelegramNotifier)
コード例 #6
0
ファイル: main.py プロジェクト: dmzkrsk/torrt
def process_commands():

    def settings_dict_from_list(lst):
        settings_dict = {}
        for s in lst:
            splitted = s.split('=')
            settings_dict[splitted[0]] = splitted[1]
        return settings_dict

    arg_parser = argparse.ArgumentParser('torrt', description='Automates torrent updates for you.')
    arg_parser.add_argument('--version', action='version', version='%(prog)s ' + '.'.join(map(str, VERSION)))
    arg_parser.add_argument('--verbose', help='Switch to show debug messages', dest='verbose', action='store_true')

    subp_main = arg_parser.add_subparsers(title='Supported commands', dest='command')

    subp_main.add_parser('list_rpc', help='Shows known RPCs aliases')
    subp_main.add_parser('list_trackers', help='Shows known trackers aliases')
    subp_main.add_parser('list_torrents', help='Shows torrents registered for updates')
    subp_main.add_parser('list_notifiers', help='Shows configured notifiers')

    parser_configure_tracker = subp_main.add_parser(
        'configure_tracker', help='Sets torrent tracker settings (login credentials, etc.)',
        description='E.g.: configure_tracker rutracker.org username=idle password=pSW0rt')
    parser_configure_tracker.add_argument(
        'tracker_alias', help='Tracker alias (usually domain) to apply settings to')
    parser_configure_tracker.add_argument(
        'settings',
        help='Settings string, format: setting1=val1 setting2=val2. '
             'Supported settings (any of): username, password',
        nargs='*')

    parser_configure_rpc = subp_main.add_parser(
        'configure_rpc', help='Sets RPCs settings (login credentials, etc.)',
        description='E.g.: configure_rpc transmission user=idle password=pSW0rt')
    parser_configure_rpc.add_argument(
        'rpc_alias', help='RPC alias to apply settings to')
    parser_configure_rpc.add_argument(
        'settings',
        help='Settings string, format: setting1=val1 setting2=val2. '
             'Supported settings (any of): url, host, port, user, password',
        nargs='*')

    parser_configure_notifier = subp_main.add_parser(
        'configure_notifier', help='Sets Notifiers settings (smtp credentials, etc.)',
        description='E.g.: configure_notifier email [email protected] user=idle password=pSW0rt')
    parser_configure_notifier.add_argument(
        'notifier_alias', help='Notifier alias to apply settings to')
    parser_configure_notifier.add_argument(
        'settings',
        help='Settings string, format: setting1=val1 setting2=val2. '
             'Supported settings for email notifier (any of): email, host, port, use_tls, user, password.'
             'Supported settings for telegram notifier: token, chat_id.',
        nargs='*')

    parser_walk = subp_main.add_parser(
        'walk', help='Walks through registered torrents and performs automatic updates')
    parser_walk.add_argument(
        '-f', help='Forces walk. Forced walks do not respect walk interval settings', dest='forced',
        action='store_true')

    parser_set_interval = subp_main.add_parser(
        'set_walk_interval', help='Sets an interval *in hours* between consecutive torrent updates checks')
    parser_set_interval.add_argument(
        'walk_interval', help='Interval *in hours* between consecutive torrent updates checks')

    parser_enable_rpc = subp_main.add_parser('enable_rpc', help='Enables RPC by its alias')
    parser_enable_rpc.add_argument('alias', help='Alias of RPC to enable')

    parser_disable_rpc = subp_main.add_parser('disable_rpc', help='Disables RPC by its alias')
    parser_disable_rpc.add_argument('alias', help='Alias of RPC to disable')

    parser_add_torrent = subp_main.add_parser(
        'add_torrent', help='Adds torrent from an URL both to torrt and torrent clients')
    parser_add_torrent.add_argument(
        'url', help='URL to download torrent from')
    parser_add_torrent.add_argument(
        '-d',
        help='Destination path to download torrent contents into (in filesystem where torrent client daemon works)',
        dest='download_to', default=None)

    parser_remove_torrent = subp_main.add_parser(
        'remove_torrent', help='Removes torrent by its hash both from torrt and torrent clients')
    parser_remove_torrent.add_argument(
        'hash', help='Torrent identifying hash')
    parser_remove_torrent.add_argument(
        '-d', help='If set data downloaded for torrent will also be removed',
        dest='delete_data', action='store_true')

    parser_register_torrent = subp_main.add_parser(
        'register_torrent',
        help='Registers torrent within torrt by its hash (for torrents already existing at torrent clients)')
    parser_register_torrent.add_argument(
        'hash', help='Torrent identifying hash')

    parser_unregister_torrent = subp_main.add_parser(
        'unregister_torrent', help='Unregisters torrent from torrt by its hash')
    parser_unregister_torrent.add_argument(
        'hash', help='Torrent identifying hash')

    parser_remove_notifier = subp_main.add_parser(
        'remove_notifier', help='Remove configured notifier by its alias')
    parser_remove_notifier.add_argument('alias', help='Alias of notifier to remove')

    args = arg_parser.parse_args()
    args = vars(args)

    loggin_level = logging.INFO
    if args['verbose']:
        loggin_level = logging.DEBUG

    configure_logging(loggin_level)
    bootstrap()

    if args['command'] == 'enable_rpc':
        toggle_rpc(args['alias'], True)

    elif args['command'] == 'disable_rpc':
        toggle_rpc(args['alias'], False)

    elif args['command'] == 'list_trackers':

        for tracker_alias, _ in TrackerClassesRegistry.get().items():
            LOGGER.info(tracker_alias)

    elif args['command'] == 'list_rpc':
        rpc_statuses = {}

        for rpc_alias, rpc in RPCClassesRegistry.get().items():
            rpc_statuses[rpc_alias] = 'unconfigured'

        for rpc_alias, rpc in RPCObjectsRegistry.get().items():
            rpc_statuses[rpc_alias] = 'enabled' if rpc.enabled else 'disabled'

        for rpc_alias, rpc_status in rpc_statuses.items():
            LOGGER.info('%s\t status=%s', rpc_alias, rpc_status)

    elif args['command'] == 'list_torrents':
        for torrent_hash, torrent_data in get_registered_torrents().items():
            LOGGER.info('%s\t%s', torrent_hash, torrent_data['name'])

    elif args['command'] == 'list_notifiers':
        notifiers = {}
        for notifier_alias in NotifierClassesRegistry.get().keys():
            notifiers[notifier_alias] = 'unconfigured'

        for notifier_alias in NotifierObjectsRegistry.get().keys():
            notifiers[notifier_alias] = 'enabled'

        for notifier_alias, notifier_status in notifiers.items():
            LOGGER.info('%s\t status=%s', notifier_alias, notifier_status)

    elif args['command'] == 'walk':
        walk(forced=args['forced'], silent=True)

    elif args['command'] == 'set_walk_interval':
        set_walk_interval(args['walk_interval'])

    elif args['command'] == 'add_torrent':
        add_torrent_from_url(args['url'], args['download_to'])

    elif args['command'] == 'remove_torrent':
        remove_torrent(args['hash'], args['delete_data'])

    elif args['command'] == 'register_torrent':
        register_torrent(args['hash'])

    elif args['command'] == 'unregister_torrent':
        unregister_torrent(args['hash'])

    elif args['command'] == 'configure_rpc':
        configure_rpc(args['rpc_alias'], settings_dict_from_list(args['settings']))

    elif args['command'] == 'configure_tracker':
        configure_tracker(args['tracker_alias'], settings_dict_from_list(args['settings']))

    elif args['command'] == 'configure_notifier':
        configure_notifier(args['notifier_alias'], settings_dict_from_list(args['settings']))

    elif args['command'] == 'remove_notifier':
        remove_notifier(args['alias'])