def getLabel(event, context): date = datetime.datetime.now() datenow = re.sub(r'\s.*', '', str(date)) shipmentdate = str(event["shipment_date"]) destination_shipmentId = str(event["destination"]["shipment_id"]) destination_company = str(event["destination"]["company"]) destination_line1 = str(event["destination"]["line1"]) destination_line2 = str(event["destination"]["line2"]) destination_zipcode = str(event["destination"]["zipcode"]) destination_countryCode = str(event["destination"]["country_code"]) destination_country = str(event["destination"]["country"]) destination_name = str(event["destination"]["name"]) destination_firstname = str(event["destination"]["first_name"]) destination_lastname = str(event["destination"]["last_name"]) destination_firstname_lastname = str(destination_firstname) + " " + str( destination_lastname) destination_phone = str(event["destination"]["phone"]) destination_email = str(event["destination"]["email"]) destination_city = str(event["destination"]["city"]) parcel_weight_in_grams = float(event["parcel"]["weight_in_grams"]) / 1000 parcel_weight_in_kg = str(parcel_weight_in_grams) parcel_width_in_cm = str(event["parcel"]["width_in_cm"]) parcel_height_in_cm = str(event["parcel"]["height_in_cm"]) parcel_length_in_cm = str(event["parcel"]["length_in_cm"]) content = str(event["contents"]) origin_company = str(event["origin"]["company"]) origin_city = str(event["origin"]["city"]) origin_line1 = str(event["origin"]["line1"]) origin_line2 = str(event["origin"]["line2"]) + "" origin_country = str(event["origin"]["country"]) origin_zipcode = str(event["origin"]["zipcode"]) + "" origin_countrycode = str(event["origin"]["country_code"]) origin_name = str(event["origin"]["name"]) origin_phone = str(event["origin"]["phone"]) origin_email = str(event["origin"]["email"]) origin_state = str(event["origin"]["state"]) origin_packagelocation = str(event["origin"]["place_description"]) xml = """ <?xml version="1.0" encoding="UTF-8" ?> <req:ShipmentRequest xmlns:req="http://www.dhl.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dhl.com ship-val-global-req.xsd" schemaVersion="1.0"> <Request> <ServiceHeader> <MessageTime>2002-08-20T11:28:56.000-08:00</MessageTime> <MessageReference>1234567890123456789012345678901</MessageReference> <SiteID>SRBFrance</SiteID> <Password>jXxlVhceKE</Password> </ServiceHeader> </Request> <RegionCode>EU</RegionCode> <NewShipper>Y</NewShipper> <LanguageCode>en</LanguageCode> <PiecesEnabled>Y</PiecesEnabled> <Billing> <ShipperAccountNumber>223932540</ShipperAccountNumber> <ShippingPaymentType>S</ShippingPaymentType> <BillingAccountNumber>223932540</BillingAccountNumber> <DutyPaymentType>R</DutyPaymentType> </Billing> <Consignee> <CompanyName>""" + destination_company + """</CompanyName> <AddressLine>""" + destination_line1 + """</AddressLine> <AddressLine>""" + destination_line2 + """</AddressLine> <City>""" + destination_city + """</City> <Division></Division> <PostalCode>""" + destination_zipcode + """</PostalCode> <CountryCode>""" + destination_countryCode + """</CountryCode> <CountryName>""" + destination_country + """</CountryName> <Contact> <PersonName>""" + destination_firstname_lastname + """</PersonName> <PhoneNumber>""" + destination_phone + """</PhoneNumber> <PhoneExtension></PhoneExtension> <FaxNumber></FaxNumber> <Telex></Telex> <Email>""" + destination_email + """</Email> </Contact> </Consignee> <Commodity> <CommodityCode>""" + destination_shipmentId + """</CommodityCode> <CommodityName>cn</CommodityName> </Commodity> <Dutiable> <DeclaredValue>0</DeclaredValue> <DeclaredCurrency>EUR</DeclaredCurrency> <ScheduleB>3002905110</ScheduleB> <ExportLicense>D123456</ExportLicense> <ShipperEIN>112233445566</ShipperEIN> <ShipperIDType>S</ShipperIDType> <ImportLicense>ImportLic</ImportLicense> <ConsigneeEIN>ConEIN2123</ConsigneeEIN> <TermsOfTrade>DTP</TermsOfTrade> </Dutiable> <ShipmentDetails> <NumberOfPieces>1</NumberOfPieces> <Pieces> <Piece> <PieceID>1</PieceID> <PackageType>YP</PackageType> <Weight>""" + parcel_weight_in_kg + """</Weight> <DimWeight>0.0</DimWeight> <Width>""" + parcel_width_in_cm + """</Width> <Height>""" + parcel_height_in_cm + """</Height> <Depth>""" + parcel_length_in_cm + """</Depth> </Piece> </Pieces> <Weight>""" + parcel_weight_in_kg + """</Weight> <WeightUnit>K</WeightUnit> <GlobalProductCode>D</GlobalProductCode> <LocalProductCode>U</LocalProductCode> <Date>""" + shipmentdate + """</Date> <Contents>""" + content + """</Contents> <DoorTo>DD</DoorTo> <DimensionUnit>C</DimensionUnit> <InsuredAmount>00.00</InsuredAmount> <PackageType>EE</PackageType> <IsDutiable>N</IsDutiable> <CurrencyCode>EUR</CurrencyCode> </ShipmentDetails> <Shipper> <ShipperID>12345</ShipperID> <CompanyName>""" + origin_company + """</CompanyName> <RegisteredAccount>123456789</RegisteredAccount> <AddressLine>""" + origin_line1 + """</AddressLine> <AddressLine>""" + origin_line2 + """</AddressLine> <City>""" + origin_city + """</City> <Division></Division> <PostalCode>""" + origin_zipcode + """</PostalCode> <CountryCode>""" + origin_countrycode + """</CountryCode> <CountryName>""" + origin_country + """</CountryName> <Contact> <PersonName>enquiry sing</PersonName> <PhoneNumber>""" + origin_phone + """</PhoneNumber> <PhoneExtension></PhoneExtension> <FaxNumber></FaxNumber> <Telex></Telex> <Email>""" + origin_email + """</Email> </Contact> </Shipper> <Place> <ResidenceOrBusiness>B</ResidenceOrBusiness> <CompanyName>""" + origin_company + """</CompanyName> <AddressLine>""" + origin_line1 + """</AddressLine> <AddressLine>""" + origin_line2 + """</AddressLine> <City>""" + origin_city + """</City> <CountryCode>""" + origin_countrycode + """</CountryCode> <DivisionCode>""" + origin_state + """</DivisionCode> <Division>FR</Division> <PostalCode>""" + origin_zipcode + """</PostalCode> <PackageLocation>""" + origin_packagelocation + """</PackageLocation> </Place> <EProcShip>N</EProcShip> <LabelImageFormat>PDF</LabelImageFormat> </req:ShipmentRequest>""" c = boto.connect_s3("AKIAJKZ7KCBQFGFGD2ZA", "2HM3b8GPRMQFb4B86pokgXpk6A6bESo7R3NRRw61") b = c.get_bucket("srbstickers", validate=False) print b headers = { 'Content-Type': 'application/xml' } # set what your server accepts resp = requests.post('https://xmlpitest-ea.dhl.com/XMLShippingServlet', data=xml, headers=headers).text print "start ET" # print resp # conn = tinys3.Connection('AKIAJIXKF5KD5RGPMTNQ','riG3NZfR8CpC2monYlvUaBXIFkYfKTw6nD8Q4',tls=True,endpoint='s3.us-east-2.amazonaws.com') root = ET.fromstring(resp) data = [] print root for child in root.findall('LabelImage'): pdf = child.find('OutputFormat').text + '.pdf' img_data = child.find('OutputImage').text #print pdf print img_data name_file = str(time.time()) + ".pdf" k = Key(b) print k k.key = name_file k.contentType = "application/pdf" k.ContentDisposition = "inline" # k.set_contents_from_string(img_data) k.set_contents_from_string(img_data.decode('base64')) link_pdf = "https://s3-us-west-2.amazonaws.com/srbstickers/" + name_file #------------- shipmentId = "0" allroot = ET.fromstring(resp) for getchild in allroot.findall('AirwayBillNumber'): if len(getchild) < 0: shipmentId = '' else: shipmentId = getchild.text if shipmentId == "0": return resp data = { "origin": event["origin"], "destination": event["destination"], "parcel": event["parcel"], "shipment_id": shipmentId, "label_url": link_pdf } return data
def label(self, paramlist): print("label function") tree = ET.parse('Assets/dhl/requests/002_Shipment_FR_BE.txt') root = tree.getroot() date = datetime.datetime.now() datenow = re.sub(r'\s.*', '', str(date)) s3key1 = os.environ["S3_KEY1"] s3key2 = os.environ["S3_KEY2"] origin_country = str(paramlist["origin"]["country"]) if origin_country == "FR": shipperAccountNumber = os.environ["SHIPPER_ACCOUNT_NUMBER_EXPORT"] else: shipperAccountNumber = os.environ["SHIPPER_ACCOUNT_NUMBER_IMPORT"] messageTime = str(datenow) + "T11:28:56.000-08:00" messageReference = str(randrange(0, 10000000000000000000000000000000)) if "shipment_date" not in paramlist: return "shipmentdate is missing..." shipmentdate = str(paramlist["shipment_date"]) if shipmentdate == "": return "shipmentdate is missing..." if "shipment_id" not in paramlist["destination"]: return "destination_shipmentId is missing..." destination_shipmentId = str(paramlist["destination"]["shipment_id"]) if destination_shipmentId == "": return "destination_shipmentId is missing..." if "company" not in paramlist["destination"]: return "CompanyName destination is missing..." destination_company = str(paramlist["destination"]["company"]) if destination_company == "": return "CompanyName destination is missing..." if "line1" not in paramlist["destination"]: return "One of the address in destination must be completed..." if "line2" not in paramlist["destination"]: return "one of the address in destination must be completed.." destination_line1 = str(paramlist["destination"]["line1"]) destination_line2 = str(paramlist["destination"]["line2"]) destination_line1 = destination_line1 + " " + destination_line2 if "city" not in paramlist["destination"]: destination_city = "" else: destination_city = str(paramlist["destination"]["city"]) if "state" not in paramlist["destination"]: destination_state = "" else: destination_state = str(paramlist["destination"]["state"]) if "zipcode" not in paramlist["destination"]: destination_zipcode = "" else: destination_zipcode = str(paramlist["destination"]["zipcode"]) if "country_code" not in paramlist["destination"]: return "destination_countryCode is missing..." destination_countryCode = str(paramlist["destination"]["country_code"]) if destination_countryCode == "": return "destination_countryCode is mandatory" if "country" not in paramlist["destination"]: return "destination_country_name is missing" destination_country = str(paramlist["destination"]["country"]) if destination_country == "": return "destination_country_name is mandatory" if "name" not in paramlist["destination"]: destination_name = "" else: destination_name = str(paramlist["destination"]["name"]) if "phone" not in paramlist["destination"]: destination_phone = "" else: destination_phone = str(paramlist["destination"]["phone"]) # if destination_phone=="": # return "destination_phone is mandatory" if "first_name" not in paramlist["destination"]: return "destination_firstname is missing ...." destination_firstname = str(paramlist["destination"]["first_name"]) if "last_name" not in paramlist["destination"]: return "destination_lastname is missing .." destination_lastname = str(paramlist["destination"]["last_name"]) if destination_firstname == "": return "destination_firstname cannot be empty." if destination_lastname == "": return "destination_lastname cannot be empty" destination_fullname = str(destination_firstname) + " " + str( destination_lastname) if "email" not in paramlist["destination"]: destination_email = "" else: destination_email = str(paramlist["destination"]["email"]) if "weight_in_grams" not in paramlist["parcel"]: parcel_weight_in_grams = "0.0" else: parcel_weight_in_grams = str( paramlist["parcel"]["weight_in_grams"]) parcel_weight_in_grams = str(float(parcel_weight_in_grams) / 1000) if parcel_weight_in_grams <= "0": parcel_weight_in_grams = "0.0" if "width_in_cm" not in paramlist["parcel"]: parcel_width_in_cm = "" else: parcel_width_in_cm = str(paramlist["parcel"]["width_in_cm"]) # if parcel_width_in_cm=="0": # return "width_in_cm must be more than 0" if "height_in_cm" not in paramlist["parcel"]: parcel_height_in_cm = "" else: parcel_height_in_cm = str(paramlist["parcel"]["height_in_cm"]) if "length_in_cm" not in paramlist["parcel"]: parcel_length_in_cm = "" else: parcel_length_in_cm = str(paramlist["parcel"]["length_in_cm"]) if "contents" not in paramlist: contents = "" else: contents = str(paramlist["contents"]) if "first_name" not in paramlist["origin"]: origin_firstname = "" else: origin_firstname = str(paramlist["origin"]["first_name"]) if "last_name" not in paramlist["origin"]: last_name = "" else: origin_lastname = str(paramlist["origin"]["last_name"]) if "company" not in paramlist["origin"]: origin_company = "" else: origin_company = str(paramlist["origin"]["company"]) if "city" not in paramlist["origin"]: return "origin_city is missing.." origin_city = str(paramlist["origin"]["city"]) if origin_city == "": return "origin_city cannot be empty" if "line1" not in paramlist["origin"]: return "origin_line1 is missing" origin_line1 = str(paramlist["origin"]["line1"]) if origin_line1 == "": return "origin_line1 cannot be empty" if "line2" not in paramlist["origin"]: origin_line2 = "" else: origin_line2 = str(paramlist["origin"]["line2"]) + "" origin_line1 = origin_line1 + " " + origin_line2 if "country" not in paramlist["origin"]: return "origin_country is missing" if origin_country == "": return "origin_country cannot be empty" if "zipcode" not in paramlist["origin"]: origin_zipcode = "" else: origin_zipcode = str(paramlist["origin"]["zipcode"]) + "" if "country_code" not in paramlist["origin"]: return "origin_countrycode is missing..." origin_countrycode = str(paramlist["origin"]["country_code"]) if origin_countrycode == "": return "origin_countrycode cannot be empty" if "name" not in paramlist["origin"]: return "origin_name is missing.." origin_name = str(paramlist["origin"]["name"]) if origin_name == "": return "origin_name cannot be empty" if "phone" not in paramlist["origin"]: origin_phone = "" else: origin_phone = str(paramlist["origin"]["phone"]) if "email" not in paramlist["origin"]: return "origin email is missing .." origin_email = str(paramlist["origin"]["email"]) if "state" not in paramlist["origin"]: origin_state = "" else: origin_state = str(paramlist["origin"]["state"]) if "place_description" not in paramlist["origin"]: origin_packagelocation = "" else: origin_packagelocation = str( paramlist["origin"]["place_description"]) root.find("Request/ServiceHeader/MessageTime").text = messageTime root.find( "Request/ServiceHeader/MessageReference").text = messageReference root.find( "Request/ServiceHeader/SiteID").text = os.environ["DHL_USERID"] root.find( "Request/ServiceHeader/Password").text = os.environ["DHL_PWD"] root.find("Billing/ShipperAccountNumber").text = shipperAccountNumber root.find("Billing/BillingAccountNumber").text = shipperAccountNumber root.find("Consignee/CompanyName").text = destination_company root.find("Consignee/AddressLine").text = destination_line1 root.find("Consignee/City").text = destination_city root.find("Consignee/Division").text = destination_state root.find("Consignee/PostalCode").text = destination_zipcode root.find("Consignee/CountryCode").text = destination_countryCode root.find("Consignee/CountryName").text = destination_country root.find("Consignee/Contact/PersonName").text = destination_fullname root.find("Consignee/Contact/PhoneNumber").text = destination_phone root.find("Consignee/Contact/Email").text = destination_email root.find( "Consignee/Contact/MobilePhoneNumber").text = destination_phone root.find("Commodity/CommodityCode").text = destination_shipmentId # root.find("ShipmentDetails/NumberOfPieces").text = destination_shipmentId root.find("ShipmentDetails/Pieces/Piece/Weight" ).text = parcel_weight_in_grams root.find( "ShipmentDetails/Pieces/Piece/Width").text = parcel_width_in_cm root.find( "ShipmentDetails/Pieces/Piece/Height").text = parcel_height_in_cm root.find( "ShipmentDetails/Pieces/Piece/Depth").text = parcel_length_in_cm root.find("ShipmentDetails/Weight").text = parcel_weight_in_grams root.find("ShipmentDetails/Date").text = shipmentdate root.find("ShipmentDetails/Contents").text = contents root.find("Shipper/ShipperID").text = shipperAccountNumber root.find("Shipper/CompanyName").text = origin_company root.find("Shipper/AddressLine").text = origin_line1 # root.find("Shipper/AddressLine").text = origin_line2 root.find("Shipper/City").text = origin_city root.find("Shipper/PostalCode").text = origin_zipcode root.find("Shipper/CountryCode").text = origin_countrycode root.find("Shipper/CountryName").text = origin_country root.find("Shipper/Contact/PersonName" ).text = origin_firstname + " " + origin_lastname root.find("Shipper/Contact/PhoneNumber").text = origin_phone root.find("Shipper/Contact/Email").text = origin_email root.find("Place/CompanyName").text = origin_company root.find("Place/AddressLine").text = origin_line1 root.find("Place/City").text = origin_city root.find("Place/CountryCode").text = origin_countrycode root.find("Place/Division").text = origin_state root.find("Place/PostalCode").text = origin_zipcode root.find("Place/PackageLocation").text = origin_packagelocation c = boto.connect_s3(s3key1, s3key2) b = c.get_bucket("srbstickers", validate=False) xmlresult = ET.tostring(root, encoding='ascii', method='xml') xmlresponse = netw.sendRequest(DHL_URL, xmlresult, "post", "xml", "xml") xmlroot = ET.fromstring(xmlresponse) for child in xmlroot.findall('LabelImage'): pdf = child.find('OutputFormat').text + '.pdf' img_data = child.find('OutputImage').text name_file = str(time.time()) + ".pdf" k = Key(b) k.key = name_file k.contentType = "application/pdf" k.ContentDisposition = "inline" # k.set_contents_from_string(img_data.decode('base64')) k.set_contents_from_string( base64.b64decode(img_data.encode('ascii'))) link_pdf = "https://s3-us-west-2.amazonaws.com/srbstickers/" + name_file shipmentId = "0" for getchild in xmlroot.findall('AirwayBillNumber'): if len(getchild) < 0: shipmentId = '' else: shipmentId = getchild.text if shipmentId == "0": return xmlresponse data = { "origin": paramlist["origin"], "destination": paramlist["destination"], "parcel": paramlist["parcel"], "shipment_id": shipmentId, "label_url": link_pdf } return data