Skip to content

Instantly share code, notes, and snippets.

@ryanmasondavies
Last active August 29, 2015 14:16
Show Gist options
  • Save ryanmasondavies/21ec319dbc74734a1e77 to your computer and use it in GitHub Desktop.
Save ryanmasondavies/21ec319dbc74734a1e77 to your computer and use it in GitHub Desktop.
This is a script for retrieving the songs played by Absolute Radio from their main website. It writes the songs out to a log and opens up a browser with an alert if they play the same song twice. I wrote this to test their 'no repeat guarantee'. They weren't lying!
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'date'
require 'fileutils'
def show_alert
File.open('alert_file.html', 'w') do |file|
file.puts "<html><head><title>PAY ATTENTION</title></head><body onload=\"alert('They played the same song! Check the logs!');\"></body></html>"
end
system("open -a safari alert_file.html")
end
def check_currently_playing
puts "Checking..."
filename = Time.now.strftime("%Y_%m_%d") + "_absolute_radio.log"
page = Nokogiri::HTML(open("http://absoluteradio.co.uk/absolute-radio/"))
currently_playing = page.css('#flexslide-item-0 a').first['title'].strip
if File.file?(filename) == false
FileUtils.touch(filename)
end
most_recent = IO.readlines(filename)[-1].strip
puts "Most recent: #{most_recent}"
unless most_recent == currently_playing
File.open(filename, 'a+') do |file|
# search for this track in the file already
file.any? do |line|
if line.strip == currently_playing
puts "FOUND THE SAME TRACK!"
puts "No repeat violated."
show_alert
end
end
# add this track to the file
file.puts currently_playing
puts "Added #{currently_playing} to file."
end
else
puts "Most recent matches currently playing."
puts "They wouldn't play the same song twice in a row!"
end
end
while true
check_currently_playing
puts "Will check again in 60 seconds at #{Time.now + 60}."
sleep 60
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment