Skip to content

Instantly share code, notes, and snippets.

@iamgroot42
Last active April 23, 2019 13:35
Show Gist options
  • Save iamgroot42/85c1707f142dbca6758a325407cb0113 to your computer and use it in GitHub Desktop.
Save iamgroot42/85c1707f142dbca6758a325407cb0113 to your computer and use it in GitHub Desktop.
Movie (and city/format) specific callback bot for BookMyShow. Update movie/format links in code and run. Ideal usage: add bot to channel, bot will send update messages whenever a cinema is added to the list
from telegram.ext import Updater, CommandHandler
import requests
import time
from bs4 import BeautifulSoup
import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
cities = ["national-capital-region-ncr", "hyderabad", "bengaluru"]
BOOKING_URL = "https://in.bookmyshow.com/CITY/movies/avengers-endgame/ET00090482"
SPECIFIC_URLS = { "3D English": "https://in.bookmyshow.com/buytickets/avengers-endgame-hyderabad/movie-hyd-ET00100559-MT/20190428",
"2D English": "https://in.bookmyshow.com/buytickets/avengers-endgame-hyderabad/movie-hyd-ET00090482-MT/20190428"}
TOKEN = "<INSERT_TELEGRAM_BOT_TOKEN_HERE>"
ALREADY_TOLD = 0
CITYWISE_CINEMAS = {}
def check_if_booking_started():
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
available_in = []
for city in cities:
url = BOOKING_URL.replace("CITY", city)
r = requests.get(url, headers=headers)
if "book tickets" in str(r.content).lower():
available_in.append(city)
return available_in
def has_booking_started(bot, update):
available = check_if_booking_started()
if len(available) > 0:
update.message.reply_text("YES! Available in %s" % ",".join(available))
else:
update.message.reply_text("Nope :(")
def callback_check(bot, job):
global ALREADY_TOLD
available = check_if_booking_started()
if len(available) > 0 and ALREADY_TOLD != len(available):
bot.send_message(chat_id="@endgameBMS", text="Pre-booking has started in %s! HURRY!!" % ",".join(available))
ALREADY_TOLD = len(available)
def new_cinema_added(bot, job):
global CITYWISE_CINEMAS, SPECIFIC_URLS
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
available = check_if_booking_started()
if len(available) > 0:
for cat, url in SPECIFIC_URLS.items():
try:
data = requests.get(url, headers=headers)
data = str(data.content)
soup = BeautifulSoup(data, features="lxml")
cinemas = soup.findAll("ul", {"id": "venuelist"})
cinemas = soup.findAll("a", {"class": "__venue-name"})
cinemas = [x.find('strong').contents[0] for x in cinemas]
current_cinemas = CITYWISE_CINEMAS.get(cat, [])
if set(current_cinemas) != set(cinemas):
added = list(set(cinemas) - set(current_cinemas))
if len(added) == 0:
continue
bot.send_message(chat_id="@endgameBMS", text="New Cinemas Added for %s (%d) : %s" % (cat, len(added), " , ".join(added)))
CITYWISE_CINEMAS[cat] = cinemas
except Exception as ex:
print(ex)
continue
updater = Updater(TOKEN)
job_queue = updater.job_queue
job_queue.run_repeating(callback_check, interval=60, first=0)
job_queue.run_repeating(new_cinema_added, interval=20, first=0)
updater.dispatcher.add_handler(CommandHandler('ask', has_booking_started))
updater.start_polling()
updater.idle()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment