Exemplo n.º 1
0
    def _update(self, provider, update, test=False):
        config = provider.get("config", {})
        new_items = []

        try:
            imap = self.authenticate(provider, config)

            try:
                rv, data = imap.select(config.get("mailbox", None),
                                       readonly=False)
                if rv != "OK":
                    raise IngestEmailError.emailMailboxError()
                try:
                    rv, data = imap.search(None,
                                           config.get("filter", "(UNSEEN)"))
                    if rv != "OK":
                        raise IngestEmailError.emailFilterError()
                    for num in data[0].split():
                        rv, data = imap.fetch(num, "(RFC822)")
                        if rv == "OK" and not test:
                            try:
                                parser = self.get_feed_parser(provider, data)
                                new_items.append(parser.parse(data, provider))
                                rv, data = imap.store(num, "+FLAGS", "\\Seen")
                            except IngestEmailError:
                                continue
                finally:
                    imap.close()
            finally:
                imap.logout()
        except IngestEmailError:
            raise
        except Exception as ex:
            raise IngestEmailError.emailError(ex, provider)
        return new_items
Exemplo n.º 2
0
    def _update(self, provider, update, test=False):
        config = provider.get('config', {})
        server = config.get('server', '')
        port = int(config.get('port', 993))
        new_items = []

        try:
            try:
                socket.setdefaulttimeout(app.config.get('EMAIL_TIMEOUT', 10))
                imap = imaplib.IMAP4_SSL(host=server, port=port)
            except (socket.gaierror, OSError) as e:
                raise IngestEmailError.emailHostError(exception=e,
                                                      provider=provider)

            try:
                imap.login(config.get('user', None),
                           config.get('password', None))
            except imaplib.IMAP4.error:
                raise IngestEmailError.emailLoginError(imaplib.IMAP4.error,
                                                       provider)

            try:
                rv, data = imap.select(config.get('mailbox', None),
                                       readonly=False)
                if rv != 'OK':
                    raise IngestEmailError.emailMailboxError()
                try:
                    rv, data = imap.search(None,
                                           config.get('filter', '(UNSEEN)'))
                    if rv != 'OK':
                        raise IngestEmailError.emailFilterError()
                    for num in data[0].split():
                        rv, data = imap.fetch(num, '(RFC822)')
                        if rv == 'OK' and not test:
                            try:
                                parser = self.get_feed_parser(provider, data)
                                item = parser.parse(data, provider)
                                if config.get('attachment'):
                                    self.save_attachment(data, item)
                                new_items.append(item)
                                rv, data = imap.store(num, '+FLAGS', '\\Seen')
                            except IngestEmailError:
                                continue
                finally:
                    imap.close()
            finally:
                imap.logout()
        except IngestEmailError:
            raise
        except Exception as ex:
            raise IngestEmailError.emailError(ex, provider)
        return new_items
Exemplo n.º 3
0
    def _update(self, provider, update, test=False):
        config = provider.get('config', {})
        server = config.get('server', '')
        port = int(config.get('port', 993))
        new_items = []

        try:
            try:
                socket.setdefaulttimeout(app.config.get('EMAIL_TIMEOUT', 10))
                imap = imaplib.IMAP4_SSL(host=server, port=port)
            except (socket.gaierror, OSError) as e:
                raise IngestEmailError.emailHostError(exception=e, provider=provider)

            try:
                imap.login(config.get('user', None), config.get('password', None))
            except imaplib.IMAP4.error:
                raise IngestEmailError.emailLoginError(imaplib.IMAP4.error, provider)

            try:
                rv, data = imap.select(config.get('mailbox', None), readonly=False)
                if rv != 'OK':
                    raise IngestEmailError.emailMailboxError()
                try:
                    rv, data = imap.search(None, config.get('filter', '(UNSEEN)'))
                    if rv != 'OK':
                        raise IngestEmailError.emailFilterError()
                    for num in data[0].split():
                        rv, data = imap.fetch(num, '(RFC822)')
                        if rv == 'OK' and not test:
                            try:
                                parser = self.get_feed_parser(provider, data)
                                new_items.append(parser.parse(data, provider))
                                rv, data = imap.store(num, '+FLAGS', '\\Seen')
                            except IngestEmailError:
                                continue
                finally:
                    imap.close()
            finally:
                imap.logout()
        except IngestEmailError:
            raise
        except Exception as ex:
            raise IngestEmailError.emailError(ex, provider)
        return new_items
Exemplo n.º 4
0
    def _update(self, provider, update, test=False):
        config = provider.get("config", {})
        new_items = []

        try:
            imap = self.authenticate(provider, config)

            try:
                rv, data = imap.select(config.get("mailbox", None),
                                       readonly=False)
                if rv != "OK":
                    raise IngestEmailError.emailMailboxError()
                try:
                    # at least one criterion must be set
                    # (see file:///usr/share/doc/python/html/library/imaplib.html#imaplib.IMAP4.search)
                    rv, data = imap.search(None,
                                           config.get("filter") or "(UNSEEN)")
                    if rv != "OK":
                        raise IngestEmailError.emailFilterError()
                    for num in data[0].split():
                        rv, data = imap.fetch(num, "(RFC822)")
                        if rv == "OK" and not test:
                            try:
                                parser = self.get_feed_parser(provider, data)
                                parsed_items = parser.parse(data, provider)
                                self.parse_extra(imap, num, parsed_items)
                                new_items.append(parsed_items)
                                rv, data = imap.store(num, "+FLAGS", "\\Seen")
                            except IngestEmailError:
                                continue
                finally:
                    imap.close()
            finally:
                imap.logout()
        except IngestEmailError:
            raise
        except Exception as ex:
            raise IngestEmailError.emailError(ex, provider)
        return new_items