Automated 40 Swiggy Invoices download using VBA

Analytics and Automation
  • Pulkit Saraf
  • 23-11-2020
Automated 40 Swiggy Invoices download using VBA

I won 2nd prize in an algorithm development contest at office few months ago and won a voucher for 7500/-

The voucher could be redeemed against any food bill. I kept it for few weeks hoping to rake up a big bill from some party but didn’t find one. Mohit pointed out that Swiggy bills would also count towards this. I do order out regularly and suspected that I might have enough bills from after the voucher issue date.

However, to verify this, I’d have to do quite a bit of manual effort. At an average price of 150/- per order, I’d have to trace back around 50 orders to confirm if the bills totalled to 7500/-

The swiggy android app shows 2.5 orders per screen. That’s a lot of scrolling and I’d need to switch to calculator app to add. It’s time consuming and not really an efficient way.

Swiggy Android app

The screen shows 2.5 orders at a time.

I tried asking Swiggy themselves but the chat support couldn’t help either. (To be honest, Zomato chat has lowered the bar on customer service so much, even this failed communication with Swiggy came as a pleasant surprise)

Next, I opened their website in Chrome and it showed 5 orders on the front screen. However, there was a button to load more and it worked flawlessly. 10 clicks and few seconds later, I had all the 50 orders in one scroll-able window. At this point in time, I tried my luck with basic excel and editing commands. I pressed (Ctrl+A) on the website, and (Ctrl+V) on an excel cell and hit jackpot. The entire website content got pasted in 2000 rows and 1 column in editable format! Few manipulations later, I had a neat summary of the orders with date and amount.

Guess what? The orders did total up to the voucher value. A total of 35 orders.

So far, good. Next, I needed printed copies of all the 35 invoices. This proved a bit difficult for two reasons.

One, I don’t receive an email with a downloadable invoice copy from Swiggy upon order completion. (Maybe, there’s a send invoice option which I may have disabled, but in either case, it’s not gonna help me out with past orders). So any email automation option goes right out the window.

Two, invoices are available on the desktop website, but they’re buried behind few clicks and there’s no bulk download invoice option. Every order has an individual “View Details” button, which when clicked, opens a new screen in 1/3rd of the window with a button “Invoice” that downloads the receipt.

To download all 35 invoices, I’d need some really good browser automation tools. (I knew this is possible in Python from the time when Abhishek tricked an office poll into accepting 100s of entries of my name for a peer nominated award)

Swiggy-website-invoice-button

Swiggy Invoice download button

I started looking up browser automation tools and came across the Chrome ‘Web Scraper‘ extension tool. I was pleasantly surprised at the potential of this tool to make my life easy. It took an hour or two of watching free tutorials and I got started. I made decent progress and extracted all the restaurants’ names and Order IDs in a csv within moments.

Sitemap-selector-graph

Sitemap Selector Graph

web-scraper-selector-add

Defining the selectors

Getting to the invoices, however, was proving just a tad difficult and I was about to deep dive into the tutorials once again when I noticed this little piece of information; and it changed everything. (Yellow arrow)

Invoice-button-URL

The URL for invoice download is https://www.swiggy.com/invoice/download/xxxxxxxxxx

The URL, that the invoice button was pointing to, was a static text followed by the Order ID! (https://www.swiggy.com/invoice/download/xxxxxxxxxx) This wasn’t a security flaw as I was able to only access the invoices which were available in my account.

I already had the Order IDs and now, all I had to do was write a 4 line VBA code to open the links! (I knew this from the time I wrote a similar macro to read multiple patents easily while working at LexInnova).

excel-invoice-hyperlink-vba

Here’s the result:

Automated Swiggy Invoice Download GIF

Downloaded-invoices

And I thought this was it.

But another small hurdle was figuring out how to print 35 PDF files. I selected all 35 files, right-clicked to see if there was a Print option, but there wasn’t. I was about to prepare myself for the next sprint on figuring out how to print multiple PDFs when I found out that Windows/ Adobe lets you print a maximum of 10 PDFs at once.

I selected 10 PDFs, right – clicked and sure enough, there was a print option. 4 clicks later, I was able to submit the bills to my office.

Thanks for reading :)