Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'rubygems'
require 'mechanize'
FIRST_NAME = 'FIRST_NAME'
LAST_NAME = 'LAST_NAME'
PHONE = 'PHONE'
EMAIL = 'EMAIL@provider.com'
PARTY_SIZE = 2
SCHEDULE_RANGE = { :start_time => '19:00', :end_time => '20:30' }
def start_url(restaurant=2086)
return "http://rez.urbanspoon.com/reservation/start/#{restaurant}"
end
def to_minutes(time)
hour, minutes = time.split(':')
raise "Malformed time: #{time}. Should be in the HH:MM format." if hour.nil? || minutes.nil?
return (hour.to_i * 60) + minutes.to_i
end
url = start_url()
agent = Mechanize.new { |agent|
agent.user_agent_alias = 'Mac Safari'
}
# Get the start page
start_page = agent.get(url)
# Bail if there are no reservations
exit if start_page.forms.count != 1
# Fill in the details for the reservation
start_form = start_page.forms.first
start_form.size = PARTY_SIZE
# Verify if the available times are in the allowed range
available_times = start_form.field_with(:name => 'seating_time').options
possible_times = available_times.select do |time|
(to_minutes(SCHEDULE_RANGE[:start_time])..to_minutes(SCHEDULE_RANGE[:end_time])).member?(time.value.to_i)
end
# Select the first of the possible times for the reservation
start_form.seating_time = possible_times.first
# Submit the details and get back the contact form
contact_info_page = start_form.submit
# Check for the existence and get the contact form
exit if contact_info_page.forms.count != 1
contact_form = contact_info_page.forms.first
# Fill in the contact details
contact_form["user[first_name]"] = FIRST_NAME
contact_form["user[last_name]"] = LAST_NAME
contact_form["user[phone]"] = PHONE
contact_form["user[email]"] = EMAIL
# Submit the contact details and get confirmation page
confirmation_page = contact_form.submit
# Confirm the reservation
exit if confirmation_page.forms.count != 1
confirmation_form = confirmation_page.forms.first
final_page = confirmation_form.submit
puts "Got reservation for: #{start_form.seating_time}"
@frankcaron

This comment has been minimized.

Copy link

commented Jul 25, 2013

This is genius. Awesome job.

@anthonybishopric

This comment has been minimized.

Copy link

commented Jul 25, 2013

Well done. I also wrote (and grabbed) a SBP reservation using phantomjs, but I definitely did not post it because there is zero incentive to share it. :P

http://www.youtube.com/watch?v=A8I9pYCl9AQ

@jkaihsu

This comment has been minimized.

Copy link

commented Jul 25, 2013

nicely done

@maxubd

This comment has been minimized.

Copy link

commented Jul 25, 2013

Nice work! Just don't tell anyone else in Chicago, please!

@mattkenefick

This comment has been minimized.

Copy link

commented Jul 26, 2013

I can't wait to tell Chicago about this!

@linuxbiker

This comment has been minimized.

Copy link

commented Jul 26, 2013

we have a place that sends out reservations on a web page like that here in Athens,GA. I tried a few times to get a reservation as soon as it came online, but always failed. It isn't a restaurant, but a small once a month table of people. I even tried copy & paste data and it wasn't fast enough. I might try making this work for our site - http://www.thefourcoursemen.com/about/

@loafeater

This comment has been minimized.

Copy link

commented Jul 26, 2013

Very cool!

@baiIey

This comment has been minimized.

Copy link

commented Jul 26, 2013

Great job, here.

@chuckreynolds

This comment has been minimized.

Copy link

commented Jul 26, 2013

claps awesome

@pixelyunicorn

This comment has been minimized.

Copy link

commented Jul 27, 2013

Genius idea! If only UrbanSpoon was programmed to be completely botproof... :P

@chrisloughnane

This comment has been minimized.

Copy link

commented Jul 28, 2013

cheers :)

@sarahduve

This comment has been minimized.

Copy link

commented Jul 29, 2013

Do you have similar code for OpenTable?

@diogomonica

This comment has been minimized.

Copy link
Owner Author

commented Jul 29, 2013

@plc

This comment has been minimized.

Copy link

commented Jul 29, 2013

how often did you run this?

@diogomonica

This comment has been minimized.

Copy link
Owner Author

commented Jul 29, 2013

I started out by doing it once a minute. That seemed enough to get reservations initially (it was a once a minute crontab).

The new bot that I'm developing does checks every 5 seconds.

@ghost

This comment has been minimized.

Copy link

commented Jul 29, 2013

genius

@kiquenet

This comment has been minimized.

Copy link

commented Jul 30, 2013

Great! It would be interesting powershell script or .NET :-)

@spencerthayer

This comment has been minimized.

Copy link

commented Jul 30, 2013

So how does this work? Is a cron job constantly running in the background? Is this running locally or on a server?

@ghost

This comment has been minimized.

Copy link

commented Aug 17, 2013

Genius.

@azadibogolubov

This comment has been minimized.

Copy link

commented Jul 21, 2014

I love this. Did something similar in Python for registering for high-demand classes in college. This is a great time to be a programmer.

@Someguy01

This comment has been minimized.

Copy link

commented Aug 14, 2014

Can someone tell me how to use this? Also does it work on ticketmaster as i might try to get some tickets for minecon. Also should i make 1 bot on a computer and one on the other to buy tickets for 2 people such as me and my friend? Help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.