Last active
July 24, 2020 15:35
-
-
Save ortsed/14b2cb7dd3d92eb4b97c5f6bf6ca92b5 to your computer and use it in GitHub Desktop.
Calculates Delivery Time for Standard Shipping Through USPS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Extends USPS api class to calculate time for Standard delivery from zip to zzip | |
from lxml import etree | |
def get_time(origin_zip, dest_zip): | |
usps = USPSApi(USPS_API_USER, test=True) | |
#usps.urls['calc'] = 'PriorityMail&XML={xml}' | |
usps.urls['calc'] = 'StandardB&XML={xml}' | |
# extends USPS class to search by Zipcode | |
class Zip(object): | |
def __init__(self, zipcode, | |
zipcode_ext=''): | |
self.zipcode = zipcode | |
self.zipcode_ext = zipcode_ext | |
def add_to_xml(self, root, prefix='To', validate=False): | |
zipcode = etree.SubElement(root, prefix + 'Zip5') | |
zipcode.text = self.zipcode | |
zipcode_ext = etree.SubElement(root, prefix + 'Zip4') | |
zipcode_ext.text = self.zipcode_ext | |
class TimeCalc(object): | |
def __init__(self, origin, destination): | |
#StandardBRequest | |
xml = etree.Element('StandardBRequest', {'USERID': usps.api_user_id}) | |
#xml = etree.Element('PriorityMailRequest', {'USERID': usps.api_user_id}) | |
_origin = etree.SubElement(xml, 'OriginZip') | |
_origin.text = str(origin) | |
_destination = etree.SubElement(xml, 'DestinationZip') | |
_destination.text = str(destination) | |
#print(etree.tostring(xml, pretty_print=True)) | |
self.result = usps.send_request('calc', xml) | |
def time_calc(self, *args, **kwargs): | |
return TimeCalc(self, *args, **kwargs) | |
usps.time_calc = time_calc | |
label = usps.time_calc(origin_zip, dest_zip) | |
return label.result | |
def get_time_val(dest_zip): | |
val = get_time(dest_zip) | |
return val["StandardBResponse"]['Days'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment