本文整理汇总了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
示例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()
示例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)
示例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]
示例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
示例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()
示例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
示例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
示例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()
示例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
示例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())
示例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
示例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()
示例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()
示例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()