本文整理汇总了Python中email.feedparser.FeedParser.feed方法的典型用法代码示例。如果您正苦于以下问题:Python FeedParser.feed方法的具体用法?Python FeedParser.feed怎么用?Python FeedParser.feed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.feedparser.FeedParser
的用法示例。
在下文中一共展示了FeedParser.feed方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from email.feedparser import FeedParser [as 别名]
# 或者: from email.feedparser.FeedParser import feed [as 别名]
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()
示例2: get_email_message_from_file
# 需要导入模块: from email.feedparser import FeedParser [as 别名]
# 或者: from email.feedparser.FeedParser import feed [as 别名]
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
示例3: __init__
# 需要导入模块: from email.feedparser import FeedParser [as 别名]
# 或者: from email.feedparser.FeedParser import feed [as 别名]
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())
示例4: _parse_batch_api_response
# 需要导入模块: from email.feedparser import FeedParser [as 别名]
# 或者: from email.feedparser.FeedParser import feed [as 别名]
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)
示例5: message_from_response
# 需要导入模块: from email.feedparser import FeedParser [as 别名]
# 或者: from email.feedparser.FeedParser import feed [as 别名]
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
示例6: _send_batch_request
# 需要导入模块: from email.feedparser import FeedParser [as 别名]
# 或者: from email.feedparser.FeedParser import feed [as 别名]
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]
示例7: from_string
# 需要导入模块: from email.feedparser import FeedParser [as 别名]
# 或者: from email.feedparser.FeedParser import feed [as 别名]
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
示例8: message_from_string_safe
# 需要导入模块: from email.feedparser import FeedParser [as 别名]
# 或者: from email.feedparser.FeedParser import feed [as 别名]
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()
示例9: parse
# 需要导入模块: from email.feedparser import FeedParser [as 别名]
# 或者: from email.feedparser.FeedParser import feed [as 别名]
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()
示例10: load_message
# 需要导入模块: from email.feedparser import FeedParser [as 别名]
# 或者: from email.feedparser.FeedParser import feed [as 别名]
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()
示例11: parse
# 需要导入模块: from email.feedparser import FeedParser [as 别名]
# 或者: from email.feedparser.FeedParser import feed [as 别名]
def parse(self, fp, headersonly=False):
"""Create a message structure from the data in a file.
Reads all the data from the file and returns the root of the message
structure. Optional headersonly is a flag specifying whether to stop
parsing after reading the headers or not. The default is False,
meaning it parses the entire contents of the file.
"""
feedparser = FeedParser(self._class)
if headersonly:
feedparser._set_headersonly()
while True:
data = fp.read(8192)
if not data:
break
feedparser.feed(data)
return feedparser.close()
示例12: headers_factory
# 需要导入模块: from email.feedparser import FeedParser [as 别名]
# 或者: from email.feedparser.FeedParser import feed [as 别名]
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()
示例13: __break_into_parts
# 需要导入模块: from email.feedparser import FeedParser [as 别名]
# 或者: from email.feedparser.FeedParser import feed [as 别名]
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
示例14: _break_into_parts
# 需要导入模块: from email.feedparser import FeedParser [as 别名]
# 或者: from email.feedparser.FeedParser import feed [as 别名]
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
示例15: _parse
# 需要导入模块: from email.feedparser import FeedParser [as 别名]
# 或者: from email.feedparser.FeedParser import feed [as 别名]
def _parse (self):
"""Parse the BDF mime structure and record the locations of the binary
blobs. Sets up various data fields in the BDFData object."""
feedparser = FeedParser (Message)
binarychunks = {}
sizeinfo = None
headxml = None
self.fp.seek (0, 0)
while True:
data = self.fp.readline ()
if not data:
break
feedparser.feed (data)
skip = (data == '\n' and
len (feedparser._msgstack) == 3 and
feedparser._msgstack[-1].get_content_type () in ('application/octet-stream',
'binary/octet-stream'))
if skip:
# We just finished reading the headers for a huge binary blob.
# Time to remember where the data chunk is and pretend it doesn't
# exist.
msg = feedparser._msgstack[-1]
ident = msg['Content-Location']
assert ident.endswith ('.bin'), 'confusion #1 in hacky MIME parsing!'
binarychunks[ident] = self.fp.tell ()
if sizeinfo is None:
headxml, sizeinfo, tagpfx = _extract_size_info (feedparser)
kind = ident.split ('/')[-1]
assert kind in sizeinfo, 'no size info for binary chunk kind %s in MIME!' % kind
self.fp.seek (sizeinfo[kind] + 1, 1) # skip ahead by data chunk size
sample = self.fp.read (16)
assert sample.startswith ('--MIME'), 'crap, unexpected chunk size in MIME parsing: %r' % sample
self.fp.seek (-16, 1) # go back
# check that two major kinds of data are read at least once
if any([k.split('/')[3] == '3' for k in binarychunks.iterkeys()]):
break
if headxml is None:
raise RuntimeError ('never found any binary data')
self.mimemsg = feedparser.close ()
self.headxml = headxml
self.sizeinfo = sizeinfo
self.binarychunks = binarychunks
headsize, intsize = self.calc_intsize()
# Compute some miscellaneous parameters that we'll need.
# self.n_integrations = len (self.mimemsg.get_payload ()) - 1
self.n_integrations = os.stat(self.fp.name).st_size/intsize
self.n_antennas = int (headxml.find (tagpfx + nanttag).text)
self.n_baselines = (self.n_antennas * (self.n_antennas - 1)) // 2
ds = headxml.find (tagpfx + dstag)
nbb = 0
nspw = 0
nchan = 0
crosspolstr = None
for bb in ds.findall (tagpfx + basebandtag):
nbb += 1
for spw in bb.getchildren ():
nspw += 1
nchan += int (spw.get ('numSpectralPoint'))
if crosspolstr is None:
crosspolstr = spw.get ('crossPolProducts')
elif spw.get ('crossPolProducts') != crosspolstr:
raise Exception ('can only handle spectral windows with identical cross pol products')
self.n_basebands = nbb
self.n_spws = nspw
self.n_channels = nchan
self.crosspols = crosspolstr.split ()
self.n_pols = len(self.crosspols)
# if bdf info pkl not present, write it
if os.path.exists(os.path.dirname(self.pklname)) and self.pklname and (not os.path.exists(self.pklname)):
logger.info('Writing bdf pkl info to %s...' % (self.pklname))
with open(self.pklname,'wb') as pkl:
# Compute some miscellaneous parameters that we'll need.
pickle.dump( (self.mimemsg, self.headxml, self.sizeinfo, self.binarychunks, self.n_integrations, self.n_antennas, self.n_baselines, self.n_basebands, self.n_spws, self.n_channels, self.crosspols), pkl)
return self # convenience