Skip to content

Instantly share code, notes, and snippets.

@Dan-Q
Last active September 25, 2017 15:03
Show Gist options
  • Save Dan-Q/e879ebd8ba5b854569a9a353829bc626 to your computer and use it in GitHub Desktop.
Save Dan-Q/e879ebd8ba5b854569a9a353829bc626 to your computer and use it in GitHub Desktop.
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