當前位置: 首頁>>代碼示例>>Python>>正文


Python feedparser.FeedParser類代碼示例

本文整理匯總了Python中email.feedparser.FeedParser的典型用法代碼示例。如果您正苦於以下問題:Python FeedParser類的具體用法?Python FeedParser怎麽用?Python FeedParser使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了FeedParser類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: get_email_message_from_file

def get_email_message_from_file(filename="notification.email"):
    """Load a mail and parse it into a email.message."""
    email_string = get_string_from_file(filename)
    parser = FeedParser()
    parser.feed(email_string)
    email_message = parser.close()
    return email_message
開發者ID:abilian,項目名稱:abilian-sbe,代碼行數:7,代碼來源:util.py

示例2: __init__

class mail:
    """Class to filter mail."""
    def __init__(self):
        """Initialise class"""
        # initiate class for feedparser
        self.raw_stream = FeedParser()

        # variables for parsed mail data
        self.raw_data   = ''
        self.raw_msg    = ''
        self.headers    = {}
        self.body    = ''
        self.sender  = ''
        self.to      = ''
        self.subject = ''
        self.date_s  = ''
        self.date_d  = ''

    def feed(self, r1):
        """Read data before parsing."""
        self.raw_data = ''.join(r1) 

    def parse(self):
        """Parse raw data using FeedParser to extract body and headers."""

        # pass raw data to feedparser instance
        self.raw_stream.feed(self.raw_data)

        # close and create a feedparser instance 
        self.raw_msg = self.raw_stream.close()
        # Mail processing
        # sort raw messages to extract variables
        for each_key in self.raw_msg.keys():
            self.headers[each_key] = self.raw_msg.get(each_key)

        # mail related variables
        # Get payload without parsing if it is not multipart
        if self.raw_msg.is_multipart() == False:
            self.body    = h.html_to_text(self.raw_msg.get_payload())
        # If message is multi-part and has both html/text parts,
        # get only the text message
        elif self.raw_msg.get_content_type() == 'multipart/alternative':
            for part in self.raw_msg.walk():
                if h.contains(part.get_content_type(),'text/plain'):
                    self.body =  part.get_payload(decode=True)
            self.body = h.html_to_text(self.body)
        else:
            # If message is multi-part and encoded with base-64, combine plain 
            # text and html text and strip all html tags
            for part in self.raw_msg.walk():
                if h.contains(part.get_content_type(), 'text'):
                    self.body = self.body + part.get_payload(decode=True)
            self.body = h.html_to_text(self.body)

        # Store data into essential variables
        self.sender  = self.headers['From'].lower()
        self.to      = self.headers['To'].lower()
        self.date_s  = self.headers['Date']
        self.date_d = h.c_date(self.date_s)
        self.subject = self.headers['Subject'].lower()
開發者ID:pamri,項目名稱:pilter,代碼行數:60,代碼來源:mailhandler.py

示例3: _parse_batch_api_response

  def _parse_batch_api_response(self, response):
    """Parses an individual part of the MIME multipart server response.

    Args:
      response: One part of the MIME mutlipart message, string.
    Raises:
      ValueError: if an invalid HTTP header is encountered.
    Returns:
      An instance of GoogleComputeEngineBase.BATCH_RESPONSE named tuple.
    """
    status, payload = response.split('\n', 1)
    split = status.split(None, 2)
    if len(split) > 1:
      status = split[1]
      reason = split[2] if len(split) > 2 else ''
    else:
      raise ValueError('Invalid HTTP server response.')

    parser = FeedParser()
    parser.feed(payload)
    msg = parser.close()
    msg['status'] = status
    http_response = httplib2.Response(msg)
    http_response.reason = reason
    payload = msg.get_payload()
    return GoogleComputeEngineBase.BATCH_RESPONSE(http_response, payload)
開發者ID:FYJen,項目名稱:GoogleCloud,代碼行數:26,代碼來源:gce_base.py

示例4: _send_batch_request

  def _send_batch_request(self, requests):
    """Sends a batch of requests to the server and processes the HTTP responses.

    Args:
      requests: List of GoogleComputeEngineBase.API_REQUEST named tuples. Must
        contain <= MAX_BATCH_SIZE elements.

    Raises:
      ValueError: If requests has more than MAX_BATCH_SIZE elements.

    Returns:
      List of GoogleComputeEngineBase.BATCH_RESPONSE named tuples, one for
      each element of request parameter.
    """
    if len(requests) > MAX_BATCH_SIZE:
      raise ValueError('Too many requests provided'
                       '(maximum is {0})'.format(MAX_BATCH_SIZE))

    batch = _BatchApiRequest()
    base = urlparse.urlsplit(self.base_url)
    base_path = base.path.rstrip('/')
    for i, request in enumerate(requests):
      msg = MIMENonMultipart('application', 'http')
      msg.add_header('Content-ID', '<{0}>'.format(i))
      msg.set_payload(self._serialize_batch_api_request(base_path, request))
      batch.attach(msg)

    batch_string = batch.as_string()
    content_type = 'multipart/mixed; boundary="{0}"'.format(
        batch.get_boundary())

    url = urlparse.urlunsplit((base.scheme, base.netloc, 'batch',
                               self._create_url_query(None), None))
    response, data = self._send_request(url, 'POST', batch_string, content_type)

    if response.status >= 300:
      error = gce.GceError(
          message=response.reason, status=response.status)
      return [error] * len(requests)  # Return all errors.
    elif not data:
      error = gce.GceError(
          message='Server returned no data', status=response.status)
      return [error] * len(requests)  # Return all errors.

    # Process successful response.
    data = 'content-type: {0}\r\n\r\n'.format(response['content-type']) + data
    parser = FeedParser()
    parser.feed(data)
    response = parser.close()

    responses = []
    for part in response.get_payload():
      responses.append((
          int(RESPONSE_ID_REGEX.match(part['Content-ID']).group(1)),
          self._parse_batch_api_response(part.get_payload())))

    responses.sort(key=lambda r: r[0])
    return [r[1] for r in responses]
開發者ID:FYJen,項目名稱:GoogleCloud,代碼行數:58,代碼來源:gce_base.py

示例5: from_string

def from_string(data, base, plugin, enabled=False):
    parser = FeedParser(PluginMessage)
    parser.feed(data)
    message = parser.close()
    message.is_new = False
    message.enabled = enabled
    message.plugin = plugin
    message.base = base
    return message
開發者ID:fermat618,項目名稱:pida,代碼行數:9,代碼來源:metadata.py

示例6: load_message

def load_message(fp):
    """Load message from a file handle.

    Reads data from a file handler and parse it
    to a message object.
    """
    parser = FeedParser()
    for line in fp.readlines():
        parser.feed(line)
    
    return parser.close()
開發者ID:amitramon,項目名稱:plainMail2HTML,代碼行數:11,代碼來源:message_utils.py

示例7: __init__

 def __init__(self):
     self._body_start = 0
     self._filename = None
     self._fp = StringIO()
     self._headers = None
     self._parser = FeedParser(Message)
     self._rollover = 262144  # 256kb
開發者ID:abec,項目名稱:vsmtpd,代碼行數:7,代碼來源:transaction.py

示例8: __break_into_parts

    def __break_into_parts(self):

        p = FeedParser()
        for h in self.get_headers_array():
            p.feed(h + "\r\n")
        p.feed("\r\n")
        p.feed(self.__body)
        msg = p.close()
        parts = msg.get_payload()
        return parts
開發者ID:vyshakhbabji,項目名稱:ringcentral-python-sdk-with-slate-sample-code,代碼行數:10,代碼來源:response.py

示例9: headers_factory

 def headers_factory(_, fp, *args):
     headers = 0
     feedparser = FeedParser(OldMessage)
     try:
         while True:
             line = to_local(fp.readline(client._MAXLINE + 1))
             if len(line) > client._MAXLINE:
                 ret = OldMessage()
                 ret.status = 'Line too long'
                 return ret
             headers += 1
             if headers > client._MAXHEADERS:
                 raise client.HTTPException("got more than %d headers" % client._MAXHEADERS)
             feedparser.feed(line)
             if line in ('\r\n', '\n', ''):
                 return feedparser.close()
     finally:
         # break the recursive reference chain
         feedparser.__dict__.clear()
開發者ID:lwoydziak,項目名稱:gevent,代碼行數:19,代碼來源:pywsgi.py

示例10: _break_into_parts

    def _break_into_parts(self):
        p = FeedParser()

        for h in self._response.headers:
            p.feed(h + ':' + self._response.headers[h] + "\r\n")

        p.feed("\r\n")
        p.feed(self.text())

        msg = p.close()

        parts = msg.get_payload()

        return parts
開發者ID:ColleenSC,項目名稱:ringcentral-python,代碼行數:14,代碼來源:api_response.py

示例11: __init__

 def __init__(self, message=None):
     """Initialize a Message instance."""
     feedparser = FeedParser(email.message.Message)
     feedparser._set_headersonly()
     data = message.read(4096)
     feedparser.feed(data)
     self._become_message(feedparser.close())
開發者ID:stef,項目名稱:maelstrom,代碼行數:7,代碼來源:index.py

示例12: message_from_response

 def message_from_response(self, headers, body):
     fp = FeedParser()
     for header, value in headers.iteritems():
         fp.feed("%s: %s\n" % (header, Header(value).encode()))
     fp.feed("\n")
     fp.feed(body)
     response = fp.close()
     return response
開發者ID:evanelias,項目名稱:python-typepad-api,代碼行數:8,代碼來源:test_tpobject.py

示例13: message_from_string_safe

def message_from_string_safe(text):
    'handle failure by email standard library by bypassing StringIO blowup'
    try: # use the standard library by default
        return email.message_from_string(text)
    except UnicodeEncodeError: # code copied from email.parser.Parser.parse()
        feedparser = FeedParser(Message)
        feedparser._set_headersonly()
        feedparser.feed(text)
        return feedparser.close()
開發者ID:cjlee112,項目名稱:respectmail,代碼行數:9,代碼來源:imap.py

示例14: parse

 def parse(self, fp, headersonly=False):
     feedparser = FeedParser(self._class, policy=self.policy)
     if headersonly:
         feedparser._set_headersonly()
     while True:
         data = fp.read(8192)
         if not data:
             break
         feedparser.feed(data)
     return feedparser.close()
開發者ID:johndpope,項目名稱:sims4-ai-engine,代碼行數:10,代碼來源:parser.py

示例15: __init__

 def __init__(self, fd):
   self.message = ''
   self.attachments = list()
   self.headers = {
       'subject': 'None',
       'sent': int(time.time()),
       'sender': 'Anonymous',
       'email': '[email protected]',
       'group_name': '',
       'parent': '',
       'sage': False,
       'public_key': ''
   }
   self._signature = None
   self.signature_valid = None
   self._fd = fd
   self._parser = FeedParser()
   self._parse_headers()
   if self.headers and self.headers['public_key']:
     self._check_signature()
開發者ID:4cdn,項目名稱:equanimous-octo-garbanzo,代碼行數:20,代碼來源:overchan_parser.py


注:本文中的email.feedparser.FeedParser類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。