Me and my friends were very enthusiastic about watching the movie "Kabali" on its first day itself, and considering the release day rush I decided to write an alerting job which will alert me as soon as the tickets are opened in book my show(BMS) any of our preferred theatres.
The algorithm was simple enough :
1) get the movie booking page HTML from BMS.
2) Parse the page and check for new theatres listed.
3) If any new theatre opened, fire a mail. Mark that theatre as alerted
4) sleep 10 minutes.
5) Go back to step 1
Code Snippets :
Step 1: get the movie booking page HTML from BMS
code:
code:
public static String getHTML(String urlToRead) throws Exception { StringBuilder result = new StringBuilder(); URL url = new URL(urlToRead); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = rd.readLine()) != null) { result.append(line); } rd.close(); return result.toString(); }
Step 2: Parse the page and check for new theatres listed. I observed that whenever a new theater is opened a html section is added which contains something like this
data-id="PVBM"
data-name="PVR: Market City, Bengaluru"
data-sub-region-id="BANG"
data-sub-region-name="Bengaluru"
data-lat="12.9969"
So basically I did split the string with the ids for stripping out the new theatre name.
code:
private static HashSet<String> parseTheatres(String temp) { String theatres[] = temp.split("data-name="); HashSet<String> nowOpen = new HashSet<>(); for (String temp2 : theatres) { if (!temp2.contains("data-sub-region")) { continue; } String theatre = temp2.split("data-sub-region")[0]; nowOpen.add(theatre); } return nowOpen; }
Step 3 : If any new theatre opened, fire a mail. Mark that theatre as alerted
Code for firing a mail using Gmail's SMTP server. Thanks to this stackoverflow.com link.
Do include the javax.mail jar in your classpath.
Code for firing a mail using Gmail's SMTP server. Thanks to this stackoverflow.com link.
Do include the javax.mail jar in your classpath.
End result :
Mails started coming in, and as soon as a new theatre is opened I started receiving a mail.
Email notifications helped to track them, and as soon as the tickets opened, we booked tickets.
This is how my inbox looked like after I ran the job in an infinite loop:
And later I realized that the movie got released in so many theatres,
such that we could have got into some theatre for the show.
But I am glad that we got tickets for the desired show in desired theatre.
Mail me sreedishps@gmail.com if you are trying to setup a similar alert in JAVA and is facing any issues.
Good.. You can also use Selenium Webdriver for the Same :P
ReplyDeleteThank you. Yup, Selenium can be used as well. but can get more implementation heavy compared to this. Me and one of my colleague was discussing about selenium today in office. good coincidence.
Delete