Writing Code to Check if Lowe’s has a Heater

Nothing says Winter like a heater breaking in a small room and not being able to find it anywhere. As I struggled searching everywhere to find a heater I was wondering, why doesn’t Lowe’s have a way to look up multiple stores to check if a heater exists? I sometimes forget how handy it is to know basic web development to build a quick application to accomplish such a task.

Now a simple google search could provide this information, however it’s an important skill to understand how to figure out where data comes from for a UI. For those who don’t know if you right click on any page using Google Chrome and click Inspect Element you’ll be able to see all the elements of the page, but you can click over to the Network tab and view all traffic. This tab allows you to view all API calls made that give you the information on a page.

If you hit CTRL + R you can see output of all network traffic. So back to the prompt we want to find a specific product, so navigating to any product page will yield us some success. I started off on the following product: https://www.lowes.com/pd/KOHLER-Highline-Curve-White-WaterSense-Elongated-Chair-Height-2-Piece-Toilet-12-in-Rough-In-Size/1001097304

Now that I have a page with some sort of ID at the very end: 1001097304 which is our first clue to building out the script. Now if we look at the network traffic of this page, we see an interesting entry for guest:

Notice the 1081 at the end

Hint hint, this is most likely the API they are using. This contains the product ID we have, a detail page and another ID (this is the store ID I figured out later on).

When we visit this page we get a large JSON response with a ton of details to load up the page. Here’s just a snippet:

I’d recommend using a web extension for JSON so it’s easy to close unnecessary portions of the JSON response. The most important portion we are looking for is availability of a product. The response also contains this super nice value:

The isAvlSts is the value that determines whether or not it’s available. The only way I was able to derive this is that the quantities map directly to what’s displayed on the UI and displays a specific message for availability.

Now that I have all the information I need from looking through the website we can get started on writing a script to check for a specific product ID and a couple store IDs near you. I’ll use python since it’s easy to write a quick script.

The only requirement is we need to use pip to install the requests library. You can run the following command pip install requests

Once we have this library one other thing we need to note is that we are using a public facing API (probably not generally accepted for use), so we need to put time in between requests. I use 5 seconds as a general rule of thumb like below:

Final thing, if you do the following:

We get a 401 error. This a common practice to limit the usage of APIs getting spammed by scripts/etc. However this can be easily worked around using a user-agent to say we are on some sort of browser. Here’s the headers I ended up using to act like a browser:

This is a common security type trick, but many simple applications can be used to do this. The main thing is to not spam the API as we still want to use it as a customer with a certain use case.

Well after all the hiccups here’s how I wrote out the script:

Now this script is pretty simple, I just organized the functions into simple parts and used a list of store ids that are near to me.

I’ve looked back at the API again since writing this script and it looks like they have proximity stores and the availability of a product at the bottom as well. While this is nice, some of the stores are wrong and it requires manual review on each page. Ideally I’d expand the script to do these checks so I don’t have to browse each inventory catalog.

I feel like Lowes has a lot of potential to just give the closest available version of the data, the JSON value indicates it’s super confusing to figure that out which is why I wrote the script to check on the first place.

Other things I would attempt to do is potentially make a script for Home Depot as well, at the best I could use an ID from Lowe’s to pull a search term, but a lot of work is needed to do that.

Thanks for the read!

--

--

Senior Brogrammer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store