Skip to content

Instantly share code, notes, and snippets.

@mveinot
Last active November 30, 2016 17:02
Show Gist options
  • Save mveinot/c5d3ae398c5556dc5beaad824bf3a041 to your computer and use it in GitHub Desktop.
Save mveinot/c5d3ae398c5556dc5beaad824bf3a041 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import urllib.request
import lxml.html
import untangle
import sys
def rchop(the_string, sub_str):
if (the_string.endswith(sub_str)):
return the_string[:-len(sub_str)]
return the_string
def GetImages(assetID):
param_str = '&Width=800&Height=600'
url = 'http://www.brydenfinancing.com/~i'+(str(assetID))+'-110'
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
con = urllib.request.urlopen(req)
dom = lxml.html.fromstring(con.read());
images = []
for link in dom.xpath('//img/@src'):
if (param_str in link):
img_src = rchop(link, param_str)
images.append(img_src)
return images
doc = untangle.parse('bryden.xml')
# use python's handy 'for item in list' syntax to traverse the list of Assets
for asset in doc.root.Dealership.Assets.Asset:
# save the VIN cdata value to a variable and print it
vin = asset.VIN.cdata
assetid = asset.AssetID.cdata
stockno = asset.StockNo.cdata
if 'ROB' in stockno: # Show Rob's Stock Numbers Only
print ('StockNo: '+stockno+' | VIN: '+vin+' | ID : '+assetid,end="")
# some Assets have no pictures which causes an IndexError exception when you try to access the object
# we surround the "dangerous" code with a try: block
try:
# essentially the same procedure as above for Assets... get python to give us each picture object
# for this asset in turn
for picture in asset.Pictures.Picture:
print ('\t'+picture['Url']+'\n')
# if the above code throws IndexError, we catch it and use that opportunity to inform that there are no pictures
except IndexError:
image_list = GetImages(assetid)
print ("| Found",len(image_list),"pictures")
sys.stdout.flush()
print ("\t",image_list[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment