def parseEmail(self,msg_data): for response_part in msg_data: if(isinstance(response_part, tuple)): msg = email.message_from_string(response_part[1]) msgLines = string.split(msg.get_payload(),'\r\n') msgList = [] for msgLine in msgLines: msgList.append(msgLine.rstrip('\r\n').rstrip('=')) message = ''.join(msgList) #get order ID oids = re.findall(r'(?<=EmailOrderNoInfo">).{14}', message) if len(oids) is not 1: raise MailException('find multiple oids in the email: ' + string.join(oids)) orderID = oids[0] order = Order() order.order_id = orderID #Shipping time time = re.findall(r'(?<=Delivery/Tracking:).{5,10}(?=-)',message) if time is None: raise MailException("can not parse the shipping time for order:" + orderID) order.shipping_time = datetime.strptime(time[0].strip(), '%m/%d/%y') #shipping comanpy company = re.findall(r'Delivery/Tracking.*?<a ', message) company =company[0] if '- UPS -' in company: order.shipping_company = u'UPS' elif '- US Mail -' in company: order.shipping_company = u'USPS' elif '- On Trac -' in company: order.shipping_company = u'On Trac' else: order.shipping_company = u'other' #Track number trackNo = re.findall(r'(?<=>).{5,40}?(?=</a> \(Tracking information)', message) if len(trackNo) is not 1: raise MailException("multiple track no found in order:" + orderID + string.join(trackNo)) order.tracking_no = trackNo[0] #Carrier Name if '00698 Flushing' in message: order.carrier_name = u'HMUS' elif '2820 119th St Flushing' in message: order.carrier_name = u'HMUS' elif '156-07 45th ave' in message: order.carrier_name = u'SOONDAA' elif '4 Lewis Cir' in message: order.carrier_name = u'CULEXPRESS' order.order_status = u'vender_shipped' try: oldOrder= Order.objects.get(order_id = order.order_id) oldOrder.carrier_name = order.carrier_name oldOrder.tracking_no = order.tracking_no oldOrder.shipping_company = order.shipping_company oldOrder.shipping_time = order.shipping_time oldOrder.save() order = oldOrder self.numUpdateOrder += 1 except ObjectDoesNotExist: order.save() self.numNewOrder += 1 #pids & qtys pids = re.findall(r'(?<=pid=).{8}', message) qtys = re.findall(r'(?<=valign=3D"top">)\d{1,3}',message) if pids is None: raise MailException("can not parse the pids for order:" + orderID) if qtys is None: raise MailException("can not parse the qtys for order:" + orderID) if len(pids) != len(qtys): raise MailException("pids do not match with qtys" + orderID) for item in range(len(pids)): try: product = Product.objects.get(dr_id = pids[item]) orderItem = OrderItem() orderItem.product = product orderItem.order = order orderItem.num = qtys[item] try: OrderItem.objects.get(product = orderItem.product, order = orderItem.order, num = orderItem.num) self.numDuplicateOrderItem += 1 except ObjectDoesNotExist: orderItem.save() self.numNewOrderItem += 1 except ObjectDoesNotExist: raise MailException("Product is not find in database, order_ID:" + orderID + " product id:" + pids[item])
def parseMail(startnum, endnum, force): mailConnector = poplib.POP3_SSL('pop3.live.com', 995) #Connect to hotmail pop3 server try: mailConnector.user('*****@*****.**') mailConnector.pass_('zhanglingyan18') except: print "username or password incorrect" else: print "Successful login" (numMsg, numSize) = mailConnector.stat() print 'numMsg: ' + repr(numMsg) + ' numBoxSize: ' + repr(numSize) for msgID in range(numMsg-startnum, numMsg-endnum, -1): print 'Start process Msg ID'+repr(msgID) (respMsg, mailMsg, octet) = mailConnector.retr(msgID) mail = email.message_from_string(string.join(mailMsg, '\n')) subject = email.Header.decode_header(mail['subject'])[0][0] subcode = email.Header.decode_header(mail['subject'])[0][1] if subcode == None: subject = subject else: subject = unicode(subject, subcode) sender = email.Header.decode_header(mail['From'])[0][0] subcode = email.Header.decode_header(mail['From'])[0][1] if subcode == None: sender = sender else: sender = unicode(sender, subcode) #print unicode(subject) == u'Shipment Confirmation' #print unicode(sender) == u'drugstore.com <*****@*****.**>' if unicode(subject) == u'Shipment Confirmation': order = Order() message = mail.get_payload() searchResult = re.search(r'EmailOrderNoInfo">\d{14}', message,re.DOTALL) orderID = re.search(r'\d{14}', searchResult.group(0), re.DOTALL) orderID = orderID.group(0) order.order_id = orderID try: Order.objects.get(order_id = orderID) #if the order alredy exist, quit if force: continue else: break except ObjectDoesNotExist: searchResult = re.search(r'Delivery/Tracking.*information will be ', message, re.DOTALL) searchResult = searchResult.group(0) #get the shipping company comanpy = searchResult if '- UPS -' in comanpy: order.shipping_company = u'UPS' elif '- US Mail -' in comanpy: order.shipping_company = u'USPS' else: order.shipping_company = u'other' #get shipping date time timeSearch = re.search(r'\d{1,2}/\d{1,2}/\d{2}', searchResult) timeString = timeSearch.group(0) order.shipping_time = datetime.strptime(timeString, '%m/%d/%y') searchResult = re.search(r'">.*</a>', searchResult, re.DOTALL) searchResult = searchResult.group(0) length = len(searchResult) trackingNo = searchResult[2:(length-4)] order.tracking_no = trackingNo order.save() else: continue mailConnector.quit() #disconnect to the mail server