def getdate(msg): if not 'date' in msg: return None datehdr = msg['date'].strip() try: return Utils.mktime_tz(Utils.parsedate_tz(datehdr)) except: return None
def p_http(t): from email import Utils import time tt = Utils.parsedate_tz(t) ts = time.mktime(tt[:9]) utc = 0 # (datetime.utcnow() - datetime.now()).seconds if tt[9] is not None: utc += tt[9] return datetime.fromtimestamp(ts - utc)
def getdate(msg): """Returns the date/time from msg in seconds-since-epoch, if possible. Otherwise, returns None.""" if not 'date' in msg: # No Date header present. return None datehdr = msg['date'].strip() try: return Utils.mktime_tz(Utils.parsedate_tz(datehdr)) except: # Some sort of error occured, likely because of an invalid date. return None
def __getMailMessage(self, addNewLine=False): view = self.view m = Mail.MailMessage(itsView=view) m.fromAddress = Mail.EmailAddress.getEmailAddress(view, "*****@*****.**") toOne = Mail.EmailAddress.getEmailAddress(view, "*****@*****.**") toTwo = Mail.EmailAddress.getEmailAddress(view, "*****@*****.**", "John Johnson") m.toAddress = [] m.toAddress.append(toOne) m.toAddress.append(toTwo) ccOne = Mail.EmailAddress.getEmailAddress(view, "*****@*****.**") m.ccAddress = [] m.ccAddress.append(ccOne) m.subject = "test mail" if addNewLine: m.subject += "\n" m.headers['Content-Length'] = "75" m.headers['Content-Type'] = "text/plain; charset=utf-8; format=flowed" m.headers['Content-Transfer-Encoding'] = "7bit" m.headers['Mime-Version'] = "1.0" m.headers['Received'] = "from [192.168.101.37] (w002.z065106067.sjc-ca.dsl.cnc.net [65.106.67.2]) by kahuna.test.com (8.12.8/8.12.8) with ESMTP id i7GKWWpo017020; Mon, 16 Aug 2004 13:32:32 -0700" m.headers['References'] = "<*****@*****.**> <*****@*****.**> <*****@*****.**> <*****@*****.**>" dateString = "Mon, 9 Aug 2004 13:55:15 -0700" m.dateSent = datetime.fromtimestamp(emailUtils.mktime_tz(emailUtils.parsedate_tz(dateString)), view.tzinfo.getInstance("Etc/GMT-7")) m.dateSentString = dateString m.itsItem.body = u"This is the body" if not IGNORE_ATTACHMENTS: m.rfc2822Message = utils.dataToBinary(m, "rfc2822Message", self.__mail) self.__mailMessage = m return self.__mailMessage
def RFC2822DateToDatetime(dateString, tz=None): """ Parses an RFC 2822 date value (e.g. in the Date: header) and returns a C{datetime.datetime} value. Uses C{email.utils.parsedate_tz}, so that a variety of date formats (not all required by the RFC) can be parsed. @param dateString: Input date value @type dateString: C{basestring} @param tz: The timezone to use for the return value. If C{None}, and the input header specified a timezone offset, the returned C{datetime} will be a UTC value. If C{None}, and no timezone offset was specified, the returned value will be as parsed. If a non-C{None} C{tz} is passed in, the returned datetime will have a C{tzinfo} of C{tz}. @type tz: C{datetime.tzinfo} @rtype: C{datetime.datetime} """ parsed = Utils.parsedate_tz(dateString) if parsed is None: return None result = datetime(*parsed[:6]) if parsed[9] is not None: # i.e. parsed[9] is an offset. Let's convert to UTC result -= timedelta(seconds=parsed[9]) if tz is not None: result = result.replace(tzinfo=PyICU.ICUtzinfo.getInstance("UTC")) result = result.astimezone(tz) else: # (shrug) no tz result = result.replace(tzinfo=tz) return result
def init_headers(self): from email import Header, Errors, Utils self.headers = [] self.title = None for h in self.msg.keys(): for s in (self.msg.get_all(h) or []): if not s: continue v = decode_header(s) self.headers.append((h, v)) if h.lower() == 'date': try: self.mtime = int(Utils.mktime_tz(Utils.parsedate_tz(v))) except (TypeError, ValueError, OverflowError): pass elif h.lower() == 'subject': self.title = v if not self.title: fname = self.msg.get_filename() if fname: self.title = decode_header(fname) return
def update_list (mlist, **kw): archive = mlist.data.get ('list_archive') mboxes = [] if archive != None: links = LinkExtractor (archive).get_links () for link in links: if link.endswith ('.txt.gz'): mboxes.append (urlparse.urljoin (archive, link)) mlist.data.setdefault ('archives', {}) for url in mboxes: parsed = urlparse.urlparse (url) con = httplib.HTTPConnection (parsed[1]) headers = {'Accept-encoding': 'gzip'} if kw.get('timestamps', True): dats = mlist.data['archives'].get (url, {}) mod = dats.get ('modified') if mod != None: headers['If-Modified-Since'] = mod etag = dats.get ('etag') if etag != None: headers['Etag'] =etag con.request ('GET', parsed[2], None, headers) res = con.getresponse () if res.status != 200: continue urlfile = url urlslash = urlfile.rfind ('/') if urlslash >= 0: urlfile = urlfile[urlslash+1:] pulse.utils.log ('Processing list archive %s %s' % (mlist.ident, urlfile)) urlbase = url[:-7] + '/' tmp = pulse.utils.tmpfile() fd = open (tmp, 'w') fd.write (gzip.GzipFile (fileobj=StringIO.StringIO (res.read ())).read ()) fd.close () mbox = mailbox.PortableUnixMailbox (open(tmp, 'rb')) i = -1 for msg in mbox: i += 1 msgfrom = msgdate = msgsubject = msgid = msgparent = None for hdr in msg.headers: lower = hdr.lower() if lower.startswith ('from:'): msgfrom = hdr[5:] elif lower.startswith ('date:'): msgdate = hdr[5:] elif lower.startswith ('subject:'): msgsubject = hdr[8:].strip() elif lower.startswith ('message-id:'): msgid = hdr[11:] elif lower.startswith ('in-reply-to:'): msgparent = hdr[12:] if msgid == None: continue msgid = pulse.utils.utf8dec (emailutils.parseaddr (msgid)[1]) ident = mlist.ident + u'/' + msgid post = pulse.db.ForumPost.get_or_create (ident, u'ListPost') postdata = {'forum': mlist, 'name': msgsubject} if msgparent != None: msgparent = pulse.utils.utf8dec (emailutils.parseaddr (msgparent)[1]) pident = mlist.ident + '/' + msgparent parent = pulse.db.ForumPost.get_or_create (pident, u'ListPost') parent.forum = mlist postdata['parent_ident'] = parent.ident msgfrom = emailutils.parseaddr (msgfrom) personident = u'/person/' + pulse.utils.utf8dec (msgfrom[1]) person = pulse.db.Entity.get_or_create (personident, u'Person') person.extend (name=msgfrom[0]) postdata['author_ident'] = person.ident if person.ident != personident: postdata['alias_ident'] = personident pulse.db.Queue.push (u'people', person.ident) msgdate = emailutils.parsedate_tz (msgdate) try: dt = datetime.datetime (*msgdate[:6]) if msgdate[-1] != None: dt = dt + datetime.timedelta (seconds=msgdate[-1]) except: dt = None postdata['datetime'] = dt postdata['weeknum'] = pulse.utils.weeknum (dt) postdata['web'] = urlbase + 'msg%05i.html' % i post.update (postdata) try: os.remove (tmp) except: pass mlist.data['archives'].setdefault (url, {}) mlist.data['archives'][url]['modified'] = res.getheader ('Last-Modified') mlist.data['archives'][url]['etag'] = res.getheader ('Etag') update_graphs (mlist, 100, **kw)
def get_message_date(msg): try: return int(Utils.mktime_tz(Utils.parsedate_tz(msg['date']))) except: return 0
self.method = "If-modified-since" self.ratio = 0 return else: self.method = "Error %d" % err.code return except urllib2.URLError: self.method = "Error URL" return except socket.timeout: self.method = "Error timeout" return if "last-modified" in f.info(): date = Utils.mktime_tz( Utils.parsedate_tz(f.info()["last-modified"])) if date == self.date: self.method = "Last-modified" self.ratio = 0 return else: date = time.time() if "content-length" in f.info(): length = f.info()["content-length"] if length == self.length: self.method = "Content-length" self.ratio = 0 return else: self.length = length
print '***Header in massage: ' for header, value in msg.items(): print header + ':' print " " + value if msg.is_multipart(): print 'this program can not handler MIME multipart message!' sys.exit(-1) #特定头 print '-'*60 if 'subject' in msg: print 'Subject: ', msg['subject'] print '-'*60 print 'Message Body: \n' print msg.get_payload() #解析日期 print '-'*60 if 'date' not in msg: exit(-1) datehdr = msg['date'].strip() print datehdr dateval = Utils.mktime_tz(Utils.parsedate_tz(datehdr)) print dateval print 'Message was sent on', time.strftime("%A, %B %d %Y at %I:%M %p", time.localtime(dateval))
def get_message_date(msg): try: return int(Utils.mktime_tz(Utils.parsedate_tz(msg["date"]))) except: return 0