Skip to content

Instantly share code, notes, and snippets.

@Dan-Q Dan-Q/jack-fm-logger.rb
Last active Sep 25, 2017

Embed
What would you like to do?
Polls the Jack FM website every 60 seconds to see what song they're playing, and records it to an SQLite database. More details at https://danq.me/2017/03/13/jack-fm/.
#!/usr/local/rvm/wrappers/ruby-2.3.0@jack-fm-logger/ruby
require 'open-uri'
require 'sqlite3'
require 'nokogiri'
# Load DB, create schema if absent
db = SQLite3::Database.new 'log.db'
db.execute "CREATE TABLE IF NOT EXISTS plays (time INTEGER, artist TEXT, song TEXT);"
# Utility functions
def wait(seconds = 60)
tx = Time.now + seconds
sleep(tx - Time.now) while(tx > Time.now)
end
# Logger
last = [nil, nil]
printf "%-23s | %-20s | %s\n", 'Time', 'Artist', 'Song'
while true do
html = Nokogiri::HTML(open("http://www.jackfm.co.uk/oxfordshire/"))
artist, song = html.at_css('.now-playing__artist strong').text, html.at_css('.now-playing__song').text
if last != [artist, song]
# song has changed since we last checked
printf "%-23s | %-20s | %s\n", Time.now.utc, artist, song
db.execute "INSERT INTO plays (time, artist, song) VALUES (?, ?, ?)", Time.now.utc.to_i, artist, song
last = [artist, song]
end
wait # for a minute
end
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.