Created
August 22, 2020 23:07
-
-
Save vinaypai/ed30ad195a10d7163e68c4f2db9ed30b to your computer and use it in GitHub Desktop.
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
import base64 | |
import csv | |
import json | |
import sys | |
import urllib.request | |
import lxml.html | |
def get_inventory(url): | |
items = [] | |
try: | |
with urllib.request.urlopen(url) as response: | |
tree = lxml.html.parse(response) | |
for item in tree.getroot().cssselect( | |
'.inventoryList li > div.hproduct[data-bodystyle=Sedan]' | |
): | |
items.append({ | |
'vin': item.attrib['data-vin'], | |
'color': item.attrib['data-exteriorcolor'], | |
'msrp': base64.b64decode(item.attrib['data-msrp']).decode('utf-8'), | |
'real price': base64.b64decode(item.attrib['data-internetprice']).decode('utf-8'), | |
'instock': not item.cssselect('.badge-in-transit'), | |
'link': item.cssselect('a.url')[0].attrib['href'] | |
}) | |
except urllib.error.HTTPError as err: | |
print("Failed to fetch from %s: %s" % (url, err.msg), file=sys.stderr) | |
return items | |
def get_dealers(): | |
url='https://www.subaru.com/services/dealers/distances/by/bounded-location?latitude=40.741024&longitude=-73.999565&neLatitude=41.43472556702858&neLongitude=-72.39578161303399&swLatitude=39.86494216665349&swLongitude=-75.50491783682702&count=-1' | |
with urllib.request.urlopen(url) as response: | |
jsn = json.load(response) | |
return [ | |
[ | |
item['dealer']['name'], | |
item['distance'], | |
', '.join([ | |
item['dealer']['address']['street'], | |
item['dealer']['address']['city'], | |
item['dealer']['address']['state'], | |
item['dealer']['address']['zipcode'], | |
]), | |
item['dealer']['siteUrl'] | |
] for item in jsn | |
] | |
def main(): | |
search_path = '/new-inventory/index.htm?search=&saveFacetState=true&model=Impreza&trim=Premium&sortBy=inventoryDate+asc&lastFacetInteracted=inventory-listing1-facet-anchor-trim-2' | |
# dealers = [ | |
# ('Bay Ridge Subaru','https://www.brooklynsubarudeals.com'), | |
# ('Star Subaru', 'https://www.starsubaru.com'), | |
# ('Island Subaru', 'https://www.myislandsubaru.com/'), | |
# ('Hassett Subaru', 'https://HassettSubaru.dealer.subaru.com') | |
# ] | |
csvwr = csv.writer(sys.stdout) | |
for name, distance, address, url in get_dealers(): | |
items = get_inventory(url + search_path) | |
for item in items: | |
csvwr.writerow([ | |
name, | |
distance, | |
item['color'], | |
item['msrp'], | |
item['real price'], | |
item['instock'], | |
address, | |
url + item['link'], | |
item['vin'], | |
]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment