How a Python script satisfied my craving with the help of Zomato

A sweet python script which sends you email when your favourite restaurant and/or dish comes online on Zomato

Ananth Raj Singh
CodeX
6 min readSep 3, 2021

--

Photo by Anna Tarazevich from Pexels

In my hometown of Bareilly, there is this restaurant Momo Hut, which serves one of the best and most heavenly momos I have ever had. Trust me, I have had a lot of momos at a lot of places; don’t tell my gym trainer though! You might think during work from home, I have this opportunity to order them any time to my heart’s content right? Wrong!

This restaurant is popular, therefore due to load, frequently goes offline and most of the time the desired food items are not available, even if online. I have much important things to do than to keep refreshing on the Zomato app/website. I had to think of an optimal solution to satisfy this craving of mine. I thought to myself that this might be a great opportunity to try scraping out, write a delicious Python script.

Let us break down the task into smaller problems:

  1. Get HTML of the desired web address
  2. Check if restaurant online and desired item(s) available
  3. Notify me when the condition is reached
  4. Run this script uninterrupted at regular intervals

Get HTML code

To get the HTML of a static website, we would require two things:

  • The web address of the HTML page
  • Requests library to make the network connection and requests

Assuming that Python (python3) will be installed in the system we are working on, we can proceed to get the HTML. I had the web address to the restaurant: https://www.zomato.com/bareilly/momo-hut-subhash-nagar-colony/order

To install the requests library, go to the terminal window and use this command to get the library installed:

Now we should be able to get the page HTML using my meagre code:

But wait, we got an Access Denied instead:

When going to this web address using a browser, we don’t face this, so what could be the difference? Let us check.

Open the URL in an incognito window of browser, then Inspect the page. We can see that a lot of extra information is being sent from browser, which we are not sending from our script.

Headers being sent

Let us assume we understand none of this, why and what extra things are sent, we just want to imitate this behaviour from our script. This handy website can give us Python Request params from cURL.

This website (https://curl.trillworks.com) will also guide you how to extract cURL, in case you need help with that. Now we have all the headers we require and we can add to our script. (Side note: We can get rid of cookies in headers):

Kudos! We are able to receive the HTML:

HTML retrieved from the script

Check if restaurant is online and item available

From this large HTML data, we need to cherrypick the information which is required to us. For me, two conditions need to be satisfied in order to call it a success event:

  • Restaurant is online
  • Momos are available for ordering

This snippet from the HTML response will tell us if the restaurant is offline:

To check if the desired item is available, we simply need to search it’s name in the HTML response. Now, to pull data out of HTML, we will be using this handy library Beautiful Soup.

Running this will now give us something like:

This looks great to a foodie! Now just running this script will tell us if we are good to order or not.

Notifying via Email

How sweet would it be if we get the callback online via mail when our success conditions are met? Deliciously sweet! Let us use Python’s built in module smtplib to send mail from our script to the specified destination.

Note: We will be sending sensitive information (like email’s password) from our script, so we should be vigilant about encryption. You can also create a throwaway email account for this purpose.

The code below creates a secure connection with Gmail’s SMTP server with TLS-encrypted connection. SMTP is not secure inherently hence ssl library will help us secure our communication.

Try running this script and check if you are receiving mail. In case you are not receiving mail to a Gmail account, try allowing permission to connect to less secure apps.

Let us incorporate this functionality into our craving script and give it final touches before serving.

This is how the fully functional script should now look like for us:

Schedule script execution

There is still not much achieved from this script if we have to run it explicitly again and again. Let us try to schedule it to run on regular intervals.

The better way would be to host it onto some hosting platform like pythonanywhere.com. I tried, but this failed because free accounts usually allow calls to some predefined and specified web addresses and zomato.com was not one of them.

Instead, I used cron to schedule our script to be run at regular interval at own machine, your laptop or desktop. If you have a UNIX based machine, this will be quite simple for you. You need to tell the crontab file about the script you want to run. You can edit the crontab file using:

A Vi window will open into the terminal, use the Vi editor to add path to your script and specify the interval (5 minutes here):

Then press Esc key to enter Normal mode in Vi, and type :wq then Enter to write to file and quit. There you go, script scheduled!

There, high-in-demand momos problem solved. This script is already paying it’s dividends, many fold increase in the consumption witnessed since the launch of the script. Pardon my obsession.

You can find the code gist here as well. Do reach out to me in case of any questions, feedback, suggestions or notifying about some great eateries.

Email: ananthrajsingh@gmail.com

--

--