Exemplo n.º 1
0
 def generate_text(self):
     if self.post['text']:
         log.info('[AP] Обнаружен текст. Извлечение...')
         self.text = self.post['text']
         self.text = self.text.replace(self.pattern, '')
         if 'attachments' in self.post:
             for attachment in self.post['attachments']:
                 if attachment['type'] == 'link':
                     self.text += '\n<a href="%(url)s">%(title)s</a>' % attachment[
                         'link']
                     # self.text += '\n[%(title)s](%(url)s)' % attachment['link']
         if config.getboolean('global', 'sign') and self.user:
             log.info(
                 '[AP] Подписывание поста и добавление ссылки на его оригинал.'
             )
             # Markdown Parsing
             # self.text += '\nАвтор поста: [%(first_name)s %(last_name)s](https://vk.com/%(domain)s)' % self.user
             # self.text += '\nОригинал поста: [ссылка](https://vk.com/wall%(owner_id)s_%(id)s)' % self.post
             # HTML Parsing
             self.text += '\nАвтор поста: <a href="https://vk.com/%(domain)s">%(first_name)s %(last_name)s</a>' % self.user
             self.text += '\nОригинал поста: <a href="https://vk.com/wall%(owner_id)s_%(id)s">ссылка</a>' % self.post
         elif config.getboolean('global', 'sign') and not self.user:
             log.info(
                 '[AP] Добавление только ссылки на оригинал поста, так как в нем не указан автор.'
             )
             # Markdown Parsing
             # self.text += '\nОригинал поста: [ссылка](https://vk.com/wall%(owner_id)s_%(id)s)' % self.post
             # HTML Parsing
             self.text += '\nОригинал поста: <a href="https://vk.com/wall%(owner_id)s_%(id)s">ссылка</a>' % self.post
Exemplo n.º 2
0
Arquivo: ui.py Projeto: 0x64746b/alot
    def __init__(self, dbman, log, accountman, initialcmd, colourmode):
        """
        :param dbman: :class:`~alot.db.DBManager`
        :param log: :class:`logging.Logger`
        :param accountman: :class:`~alot.account.AccountManager`
        :param initialcmd: commandline applied after setting up interface
        :type initialcmd: str
        :param colourmode: determines which theme to chose
        :type colourmode: int in [1,16,256]
        """
        self.dbman = dbman
        self.dbman.ui = self  # register ui with dbman
        self.logger = log
        self.accountman = accountman

        if not colourmode:
            colourmode = config.getint('general', 'colourmode')
        self.logger.info('setup gui in %d colours' % colourmode)
        self.mainframe = urwid.Frame(urwid.SolidFill())
        self.inputwrap = InputWrap(self, self.mainframe)
        self.mainloop = urwid.MainLoop(self.inputwrap,
                config.get_palette(),
                handle_mouse=False,
                event_loop=urwid.TwistedEventLoop(),
                unhandled_input=self.unhandeled_input)
        self.mainloop.screen.set_terminal_properties(colors=colourmode)

        self.show_statusbar = config.getboolean('general', 'show_statusbar')
        self.notificationbar = None
        self.mode = 'global'
        self.commandprompthistory = []

        self.logger.debug('fire first command')
        self.apply_command(initialcmd)
        self.mainloop.run()
Exemplo n.º 3
0
Arquivo: ui.py Projeto: jhcepas/alot
    def __init__(self, dbman, log, accountman, initialquery, colourmode):
        self.dbman = dbman
        self.dbman.ui = self  # register ui with dbman
        self.logger = log
        self.accountman = accountman

        if not colourmode:
            colourmode = config.getint('general', 'colourmode')
        self.logger.info('setup gui in %d colours' % colourmode)
        self.mainframe = MainWidget(self)
        self.mainloop = urwid.MainLoop(self.mainframe,
                config.get_palette(),
                handle_mouse=False,
                unhandled_input=self.keypress)
        self.mainloop.screen.set_terminal_properties(colors=colourmode)

        self.show_statusbar = config.getboolean('general', 'show_statusbar')
        self.notificationbar = None
        self.mode = ''
        self.commandprompthistory = []

        self.logger.debug('setup bindings')
        cmd = commandfactory('search', query=initialquery)
        self.apply_command(cmd)
        self.mainloop.run()
Exemplo n.º 4
0
 def __init__(self, tid, dbman):
     self.dbman = dbman
     self.thread = dbman.get_thread(tid)
     self.tag_widgets = []
     self.display_content = config.getboolean('general',
                                 'display_content_in_threadline')
     self.rebuild()
     urwid.AttrMap.__init__(self, self.columns,
                            'threadline', 'threadline_focus')
Exemplo n.º 5
0
def start(args):
    """
    Handles the start of the rosie application
    """
    import time
    from robot import Robot
    # create the robot
    Robot()
    modules = []

    def close_modules(signum, frame):
        """
        Close running modules upon Crtl-c
        """
        for module, thr in modules:
            logging.info('closing %s...', module.__name__)
            if hasattr(module, 'end'):
                module.end()
            while thr and thr.isAlive():
                thr.join(3)
                if thr.is_alive():
                    logging.warning(
                        'join timed out, killing thread!!')
                    if sys.version_info.major == 3:
                        thr._tstate_lock.release()
                        thr._stop()
                    else:
                        thr._Thread__stop()
            logging.info('closed %s...', module.__name__)
        event.clear()

    signal.signal(signal.SIGTERM, close_modules)
    signal.signal(signal.SIGINT, close_modules)

    for sect in (s for s in config.sections()
                 if config.getboolean(s, 'active')):
        logging.info('loading %s...', sect)
        try:
            module = importlib.import_module(sect)
        except ImportError:
            logging.error(traceback.format_exc())
            logging.error('module %s not loaded\033[0m', sect)
        # no exception was raised
        else:
            logging.info('loaded %s', sect)
            thr = None
            if hasattr(module, 'init'):
                thr = threading.Thread(target=module.init)
                thr.start()
            modules.append((module, thr))

    event = threading.Event()
    event.set()
    # main thread
    while event.is_set():
        time.sleep(2)
Exemplo n.º 6
0
def init():
    from settings import config
    host = config.get('restAPI', 'host', fallback='0.0.0.0')
    port = config.getint('restAPI', 'port', fallback=5000)
    debug = config.getboolean('restAPI', 'debug', fallback=False)
    app.debug = debug

    global server
    server = WebSocketServer((host, port), app, handler_class=WebSocketHandler)
    server.serve_forever()
Exemplo n.º 7
0
 def __init__(self, tid, dbman):
     self.dbman = dbman
     #logging.debug('tid: %s' % tid)
     self.thread = dbman.get_thread(tid)
     #logging.debug('tid: %s' % self.thread)
     self.tag_widgets = []
     self.display_content = config.getboolean('general',
                                 'display_content_in_threadline')
     self.rebuild()
     urwid.AttrMap.__init__(self, self.columns,
                            'search_thread', 'search_thread_focus')
Exemplo n.º 8
0
def get_active_discovery_methods():
    """
    Returns a list of active discovery methods
    """
    global ACTIVE_DISCOVERY_METHODS, DISCOVERY_METHODS

    if ACTIVE_DISCOVERY_METHODS is None:
        ACTIVE_DISCOVERY_METHODS = [
            m for m in DISCOVERY_METHODS
            if (config.has_section(m) and
                config.has_option(m, "enabled") and
                config.getboolean(m, "enabled"))
        ]
    return ACTIVE_DISCOVERY_METHODS
Exemplo n.º 9
0
class DefaultRouter(BaseRouter):
    _TEMPLATES = {}
    _RULES = []  # List of MetricRule
    CONFIG_SECTION = "pm_router"

    @classmethod
    def route(cls, object, settings):
        """
        Default pm-to-graphite router
        """
        model_id = cls.get_model_id(object)
        logger.debug("Mapping %s %s (%s)",
                     model_id, object, settings.metric_type)
        tpl = cls._TEMPLATES.get(model_id.lower())
        if not tpl:
            logger.debug("No template for model %s. Disabling metric",
                         model_id)
            settings.is_active = False
            return
        settings.is_active = cls.is_active(model_id, object)
        if not settings.is_active:
            logger.debug(
                "Deactivating metric for %s %s (%s)",
                model_id, object, settings.metric_type
            )
            settings.is_active = False
            return
        settings.metric = tpl.render(Context({
            "object": object,
            "model_id": model_id,
            "metric_type": settings.metric_type
        }))
        settings.probe = cls.route_probe(object, settings)
        # Apply rules
        for r in cls._RULES:
            if r.rx.search(settings.metric):
                logger.debug("Apply rule %s", r.n)
                settings.probe = r.probe
                settings.active = r.active
                if not settings.active:
                    logger.debug("Metric disabled by rule %s", r.n)
                    settings.is_active = False
                    return
                break
        logger.debug(
            "Setting metric for %s %s (%s) "
            "to %s (probe %s)",
            model_id, object, settings.metric_type,
            settings.metric, settings.probe
        )

    @classmethod
    def route_probe(cls, object, settings):
        """
        Get probe name by object.
        First, try to call model routers.
        i.e, for sa.ManagedObject model try to call
        route_probe_sa_managedobject function
        """
        model_id = cls.get_model_id(object)
        model_handler = "route_probe_%s" % model_id.replace(".", "_").lower()
        if hasattr(cls, model_handler):
            pn = getattr(cls, model_handler)(object, settings)
            if isinstance(pn, basestring):
                return cls.get_probe(pn)
            else:
                return pn
        else:
            return cls.get_default_probe()

    @classmethod
    def route_probe_sa_managedobject(cls, object, settings):
        """
        Default probe router for managed object
        """
        return cls.get_default_probe()

    @classmethod
    def route_probe_inv_interface(cls, object, settings):
        """
        Default probe router for interface
        """
        return cls.get_default_probe()

    @classmethod
    def is_active(cls, model_id, object):
        if model_id == "inv.Interface":
            return object.managed_object.is_managed
        elif model_id == "sa.ManagedObject":
            return object.is_managed
        elif model_id in (
                "sa.ManagedObjectProfile", "inv.InterfaceProfile"):
            return False
        else:
            return True

    @classmethod
    def configure(cls):
        rules = {}
        for opt in config.options(cls.CONFIG_SECTION):
            if opt.startswith("map."):
                # Process metric mappings
                _, model_id = opt.split(".", 1)
                v = config.get(cls.CONFIG_SECTION, opt)
                logger.info("Configuring metric mappings %s -> %s",
                            model_id, v)
                try:
                    cls._TEMPLATES[model_id] = Template(v)
                except TemplateSyntaxError, why:
                    logging.error("Template syntax error: %s", why)
            elif opt.startswith("metric."):
                # Process metric rules
                n = int(opt.split(".")[1])
                v = config.get(cls.CONFIG_SECTION, opt)
                try:
                    rx = re.compile(v)
                except re.error, why:
                    logging.error(
                        "Invalid regular expression in rule %s: %s",
                        n, why
                    )
                    continue
                if config.has_option(cls.CONFIG_SECTION, "probe.%s" % n):
                    probe_name = config.get(cls.CONFIG_SECTION, "probe.%s" % n)
                    try:
                        probe = cls.get_probe(probe_name)
                    except:
                        logging.error(
                            "Invalid probe in rule %s: %s",
                            n, probe_name
                        )
                else:
                    probe = cls.get_default_probe()
                if config.has_option(cls.CONFIG_SECTION, "active.%s" % n):
                    active = config.getboolean(cls.CONFIG_SECTION, "active.%s" % n)
                else:
                    active = True
                rules[n] = MetricRule(
                    n=n, rx=rx, probe=probe, active=active)
Exemplo n.º 10
0
from time import sleep

from datetime import datetime, time

from crawler import WebsiteCrawler
from helpers import convert_string_to_time
from settings import config
from telegram_bot import send_telegram

interval_minutes = config.getint("general", "interval_minutes")

send_live_bits = config.getboolean("general", "send_live_bits")
live_bit_only_on_weekdays = [1, 2, 3, 4, 5, 6, 7]

raw_start_time = config.get("general", "start_time")

start_time = convert_string_to_time(config.get("general", "start_time"))
end_time = convert_string_to_time(config.get("general", "end_time"))

if __name__ == '__main__':
    crawler = WebsiteCrawler()

    send_telegram("Service gestartet")

    previous_day = -1
    while True:

        try:
            now_time = datetime.now().time()

            # check website between this time