Skip to content

Instantly share code, notes, and snippets.

@7Z0nE
Created May 6, 2021 15:51
Show Gist options
  • Save 7Z0nE/468893610a435d92b8b552984fe6f7f6 to your computer and use it in GitHub Desktop.
Save 7Z0nE/468893610a435d92b8b552984fe6f7f6 to your computer and use it in GitHub Desktop.
buergerbot-telegram with command line arguments for limited treis. Meant to run as a cron job
#!/usr/bin/env ruby
require 'watir'
require 'telegram/bot'
Telegram.bots_config = {
default: '1737278742:AAG5hgFUafYf1f_ad4gAkYz3wl6ysh2DboE',
}
Telegram.bot.get_updates
chat_id = '340761911'
def log (message) puts " #{message}" end
def success (message) puts "+ #{message}" end
def fail (message) puts "- #{message}" end
def notify (message)
success message.upcase
system 'osascript -e \'Display notification "Bürgerbot" with title "%s"\'' % message
rescue StandardError => e
end
def appointmentAvailable? (b)
url = 'https://service.berlin.de/terminvereinbarung/termin/tag.php?termin=1&dienstleister=122282&anliegen[]=120703&herkunft=1'
puts '-'*80
log 'Trying again'
b.goto url
log 'Page loaded'
link = b.element css: '.calendar-month-table:first-child td.buchbar a'
if link.exists?
link.click
notify 'An appointment is available.'
log 'Enter y to keep searching or anything else to quit.'
Telegram.bot.send_message(chat_id: chat_id, text: 'Found an appointment! Book here: https://service.berlin.de/terminvereinbarung/termin/tag.php?termin=1&dienstleister=122282&anliegen[]=120703&herkunft=1')
return gets.chomp.downcase != 'y'
else
fail 'No luck this time.'
return false
end
rescue StandardError => e
fail 'Error encountered.'
puts e.inspect
return false
end
repetitions = ARGV[0].to_i
sleep_duration = ARGV[1].to_i
b = Watir::Browser.new
Telegram.bot.send_message(chat_id: chat_id, text: "checking %d times with %d second pauses" % [repetitions, sleep_duration])
for i in 0..repetitions do
if appointmentAvailable? b
break
end
log 'Sleeping'
sleep sleep_duration
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment