Noel Varghese

@noel.varghese.22

How I coded my way to Early Tickets for Avengers Endgame

https://www.marvel.com/movies/avengers-endgame

On 26th April 2019, the most hyped and most anticipated movie of our generation was released Avengers Endgame, and I managed to grab the best seats in the hall for the first day first show in IMAX, Bangalore.

When the release date for the movie was announced, I found out that I had booked a flight to go home in the evening for the exact same day three months ago. Living in Bangalore, with the airport being almost 3hrs away I knew that If I’m not able to get the tickets for the morning show, the movie I was waiting for almost a year will most probably get spoiled & I won’t be able to view it in IMAX which I was dying for.

I was in a tough spot, this is a city where you need to book tickets at least 3–4 days in advance for the weekends, and with the added hype around Avengers, something had to be done. My friends and I would be regularly refreshing bookmyshow, a popular event ticket booking portal so that we don’t miss out on the tickets, but I knew this strategy would never work.

The only solution was to automate this somehow, write a script that periodically refreshes the site and when the tickets are available to alert us in a way that we don’t miss it.

Finding out when the tickets are out

So every movie which is coming soon on bookmyshow gets a page of its own, for every city it is different. For Avengers Endgame for Bangalore, it was this https://in.bookmyshow.com/bengaluru/movies/avengers-endgame/ET00090482

Screenshot from bookmyshow.com portal

The only difference was it didn’t have the Book Tickets button at the time when it was still in the coming soon phase. So all I needed to do was periodically load the page and check when it finally gets the button element. The shell script for it was pretty simple.

# curl the bookmyshow website
output=`curl -s https://in.bookmyshow.com/bengaluru/movies/avengers-endgame/ET00090482 | grep -c 'Book Tickets'`
if [ $output -gt 0 ]
then
  # Alert somehow, will come to that later
fi

To periodically run it, I had to run the script as a cron job. But adding it in my own machine meant I had to keep it on at all times which wasn’t feasible. I needed to add this cron job in some remote server which will keep running no matter what. So I added it in a remote AWS server that I had provisioned for testing. You can refer to the first part of my other medium article in case you need to provision an ec2 AWS instance for yourself. And I added the below crontab entry

# Run the alert_ticket script every minute as a scheduled cron
*/1 * * * * sh /home/ec2-user/check_ticket.sh

Alert when the tickets are available

Now I needed some way in which the script could alert us after the tickets were available. The only thing I have on my person at all times is my mobile, so either sending a text message or triggering an automated call seemed the best option for me. I figured out that I can use Twilio APIs to achieve the same for free since they provide enough credits at the start to make quite a lot of calls & messages.

The first part was to get a Twilio number and to register a few mobile numbers with them which would be getting the calls & messages using the docs provided here. The quickstart guides for both SMS and Voice were simple enough to follow. I ended up with one script which would alert both by calling & messaging different numbers to ensure at least someone in my group is alerted.

#!/usr/bin/python
from twilio.rest import Client
# Your Account SID from twilio.com/console
account_sid = "xxxx"
# Your Auth Token from twilio.com/console
auth_token  = "xxxx"
client = Client(account_sid, auth_token)
message = client.messages.create(
    to="+xxxxxxxxxxxx",
    from_="+zzzzzzzzzzzz",
    body="Book Tickets for avengers endgame")
call = client.calls.create(
    to="+yyyyyyyyyyyy",
    from_="+zzzzzzzzzzzz",
    url="https://handler.twilio.com/twiml/xxxx")
# Send message
print(message.sid)
# Trigger a call
print(call.sid)

And my check_ticket.sh script changed to calling the above script when it figures out the tickets are available.

# Curl the bookmyshow website
output=`curl -s https://in.bookmyshow.com/bengaluru/movies/avengers-endgame/ET00090482 | grep -c 'Book Tickets'`
if [ $output -gt 0 ]
then
  /usr/bin/python /home/ec2-user/alert_by_twilio.sh
fi

Conclusion

And that’s all folks — two small scripts on a remote server are all it takes to set everything up. When I set it up for Avengers I & my friend got a call & message respectively, we immediately head over to the site. It took some time for the portal to upload the tickets for the various halls in different locations. After multiple failed payments we finally got the best seats in the hall and had an amazing spoiler-free, first time, IMAX experience for the movie.

Topics of interest

More Related Stories