Getting Avengers Endgame tickets with PythonAswin GBlockedUnblockFollowFollowingMay 14It’s not very often when a movie generates so much hype that you know ahead of time that the popular online ticket sales services are either going to crash or will be sold out within minutes of bookings opening.
And the gargantuan amount of hype propelling Marvel’s Avengers Endgame was far greater than any movie, for as long as I’ve been going to the movies.
As a person who had watched the 22 films that led up to Endgame, released over the course of a decade, watching the end of the saga at any day other than the the opening Friday was just unthinkable.
However, I knew I wouldn’t just be able to log on to BookMyShow, the most popular online ticket booking service in India, on Thursday morning and expect vacant seats for me and my friends.
Avengers Infinity War, released a year prior, had taught me just how difficult getting tickets can be.
When Infinity War was about to be released, I gathered up three of my friends and set up this ‘system’, where one of us would check to see if the bookings had opened, and then the next one would check 15 minutes later, and so on.
So each of us had to check once per hour, and we as a group would have someone checking in every 15 minutes.
It didn’t work.
Turns out, it’s really easy to overshoot the ‘check every hour’ target by several minutes when we are caught up with whatever else we’re doing, and it takes a lot less than 15 minutes for multiple theaters to get sold out.
In the end I had to settle for a Saturday afternoon show.
This time, I did things a little differently.
I already spend a lot of my free time working on ideas, applications, and tinkering with code and I’m a student of Computer Science & Engineering, so I figured, why not do some actual computer engineering?So I came up with a little script, made in Python — chosen because of it’s unmatched simplicity and speed for hacking together something small such as this — to get alerted when the bookings opened.
Identifying the requirementsInitially I thought of using the Beautiful Soup library, which should be familiar to anyone who has done web scraping with Python, but quickly realized I would just need to make a GET request and check the theater name in the response.
I used the requests library to make the GET request.
Next up was the alert.
I’m pretty sure there are ways to automate the ticket booking process all the way, including the payment as well, but I didn’t want to dig too deep and decided to stick with an email alert.
After looking through some e-mail delivery platforms, I settled on SendGrid.
Even though it was a service for ‘commercial email delivery’, it let me send up to 100 emails per day for free, which was more than enough for my purposes, and had a simple and straight-to-the-point integration with Python.
Creating the scriptAs always, I started off with importing the requirements.
import requestsfrom time import sleepfrom sendgrid import SendGridAPIClientfrom sendgrid.
mail import MailAs always, you can just pip install any libraries you don’t already have.
Next up was creating the function that would send the mail.
SENDGRID_KEY = 'YOUR-API-KEY-HERE'def send_mail(): message = Mail( from_email='endgame_alert@example.
com'], subject='BOOKINGS ARE NOW OPEN FOR ENDGAME', html_content='BOOK NOW! BOOK NOW!') try: sg = SendGridAPIClient(SENDGRID_KEY) response = sg.
status_code) except Exception as e: print(e.
message)Most of this is directly from SendGrid’s documentation: A Mail object that has a from_email representing the mail from which the email appears to be sent, a list of emails to which to the message would be sent, and of course, the subject and content of the mail itself.
Assign the API key you can generate on your SendGrid dashboard to SENDGRID_KEY .
(Ideally you should never put any API key inside your script, it should be loaded in from your environment to prevent others from potentially stealing it.
But I signed up with a throwaway account and would just take down the script soon after it served it’s purpose, so I didn’t bother.
Don’t be like me!)The Infinite LoopThe last part was an infinite loop that would keep running send_mail() every n minutes.
I chose n=3.
n = 3while(True): res = requests.
find('THEATER-NAME') != -1): print("BOOKINGS ARE NOW OPEN!") send_mail() else: print("Bookings not open yet") sleep(n*60)This part makes a GET request to the URL of the page for Endgame listing the theaters in which the movie was playing in my city.
I replaced THEATER-NAME with the theater that usually opens bookings first.
HostingPutting the script on Heroku’s free tier of hosting was the easiest step.
Create a requirements.
txt listing all the dependencies by running pip freeze > requirements.
txtCreate a Procfile :worker: python endgame_notify.
py was the name of my script.
What this does is start a worker dyno that executes the command python endgame_notify.
py , which as expected just runs the script with Python.
Create a new Heroku app and follow their instructions until git push heroku master , which should push your code and automatically install the dependencies listed in requirements.
Now start your worker dyno by running heroku ps:scale worker=1 , and make sure it’s running by heroku logs –tail .
That’s it!While testing this script I realized Gmail was recognizing the emails as spam, so you might want to tell Gmail to not do that by marking the mails as not spam.
Full CodeThis script worked as expected, and I was able to watch Endgame on Friday night (And as far as the movie goes, it was a delight to watch and definitely exceeded my high expectations.
)In the end this served as a lesson in how automating things could make my life easier, and I’ll be sure to cook up more little scripts for otherwise mundane tasks from now.
If you’re a fellow coder, I would recommend you try the same.
You get quality of life improvements while gaining insights and knowledge about your programming language of choice as well the task you are automating in one go.