def parse(self, xml_text, code): _xml_text = "".join(xml_text).decode('latin_1').encode('utf_8') root = ElementTree.fromstring(_xml_text) root = root.find(".//Envio") if root is None: return found_code = get_text(root.find("Codigo"), "CodigoEnvio").strip() if found_code != code.get_code(): print >> sys.stderr,\ "Code mismatch: expected '{0}', found '{1}'.".format(code.get_code(), found_code) return order.NotFoundOrder(code) error_code = int(get_text(root, "CodError")) if error_code != 0: return order.NotFoundOrder(code) sent_order = order.SentOrder(code) event_list = root.find("ListaEventos") for xml_event in event_list.iter("Evento"): if len(xml_event) == 0: continue date_string = get_text(xml_event, "Fecha") time_string = get_text(xml_event, "Hora") date = time.strptime(date_string + " " + time_string, "%d/%m/%Y %H:%M:%S") text = get_text(xml_event, "Evento") description = get_text(xml_event, "DescripcionWeb") city = get_text(xml_event, "Unidad") province = get_text(xml_event, "Provincia") country = get_text(xml_event, "Pais") location = event.Location(country, city, province) new_event = event.Event(date, text, description, location) sent_order.add_event(new_event) return sent_order
def parse(self, html_text, code): soup = BeautifulSoup(html_text) if soup is None: return found_code = self.find_code(soup) if found_code != code.get_code(): print >> sys.stderr,\ "Code mismatch: expected '{0}', found '{1}'.".format(code.get_code(), found_code) return order.NotFoundOrder(code) #error_code = int(get_text(root, "CodError")) #if error_code != 0: # return order.NotFoundOrder(code) sent_order = order.SentOrder(code) event_list = soup.findAll('table')[0].findAll('tr')[2:] try: for html_event in event_list: if len(html_event) == 0: continue date_string = html_event.findAll('td')[0].contents date = time.strptime(date_string[0], "\r\t\t\t\t%d/%m/%Y") description_html = html_event.findAll('td')[1] description = description_html.span['title'] text = description_html.contents[1].contents[0].split("\r")[0] new_event = event.Event(date, text, description, None) sent_order.add_event(new_event) except: return order.NotFoundOrder(code) return sent_order
def dict_to_object(self, dictionary): if "exceptionType" in dictionary: return order.NotFoundOrder(self._code) if "tuStatus" in dictionary: return self.process_order(dictionary["tuStatus"][0]) if "history" in dictionary: return dictionary["history"] if "address" in dictionary: return self.process_event(dictionary) if "city" in dictionary and "countryName" in dictionary: return event.Location(dictionary["countryName"], dictionary["city"]) return None
def get_order(self, code): content = self.query(code.get_code()) if content is None or content == "": return order.NotFoundOrder(code) return self._parser.parse(content, code)