Skip to content

Instantly share code, notes, and snippets.

@olegykz
Created October 24, 2022 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olegykz/0d491884fac902cd1e4dff4588cd7629 to your computer and use it in GitHub Desktop.
Save olegykz/0d491884fac902cd1e4dff4588cd7629 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'watir'
require 'logger'
require 'pry'
require 'telegram/bot'
TOKEN = '<TG_TOKEN>'
CHAT_ID = -12345678
logger = Logger.new($stdout)
Telegram.bots_config = { default: TOKEN }
def appointment_available?(b, logger)
url = 'https://service.berlin.de/terminvereinbarung/termin/tag.php?termin=1&anliegen[]=327537&dienstleisterlist=122217,122219,122227,122231,122243,122252,122260,122262,122254,122271,122273,122277,122280,122282,122284,327539,122291,122285,122286,122296,150230,122301,122297,122294,122312,122314,122304,122311,122309,122281,122279,122276,122274,122267,122246,122251,122257,122208,122226&herkunft=http%3A%2F%2Fservice.berlin.de%2Fdienstleistung%2F327537%2F'
puts '-' * 80
logger.debug 'Trying again'
b.goto url
logger.debug 'Page loaded'
link = b.element css: '.calendar-month-table td.buchbar a'
if link.exists?
# link.click
appointment_date = Time.at(link.attributes[:href][/\d+/].to_i).to_date
days_to_appointment = (appointment_date - Date.today).to_i
logger.info "Appointment available for #{appointment_date} (in #{days_to_appointment} days)"
if (0..7).include?(days_to_appointment)
text =
"Доступна запись через #{days_to_appointment} дней (#{appointment_date})"
Telegram.bots[:default].send_message(
chat_id: CHAT_ID, text: text
)
# Use Siri to pronounce the text
# `say -v Milena "#{text}"`
end
else
logger.debug 'No luck this time.'
end
false
rescue StandardError => e
logger.fatal e.inspect
false
end
b = Watir::Browser.new :chrome # , headless: true
until appointment_available?(b, logger)
logger.debug 'Sleeping.'
sleep(rand(60..120))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment