Created
November 16, 2015 15:28
-
-
Save jronallo/dac62d51128905339d5e to your computer and use it in GitHub Desktop.
Quick Ruby script to get libnotify desktop notifications of the current vote tally of your talk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# To add this to cron do something like this to use the ruby wrapper script: | |
# */15 8-17 * * 1-5 env DISPLAY=:0.0 /home/jnronall/.rvm/wrappers/ruby-2.1.1/ruby /home/jnronall/bin/code4lib-vote > $HOME/tmp/code4lib-vote-cron.log 2>&1 | |
require 'httpclient' | |
require 'json' | |
require 'date' | |
require 'libnotify' | |
require 'slop' | |
require 'fileutils' | |
opts = Slop.parse do |o| | |
o.banner = %Q{ | |
Usage: code4lib-vote --vote 33 --mytalk 655 --top 20 --timeout 15 --icon /home/jnronall/bin/images/ballot-box.png | |
} | |
o.integer '--vote', 'Number identifying vote', default: 39 | |
o.integer '--mytalk', 'Number identifying your talk', default: 869 | |
o.integer '--top', 'Number of results to show in notification', default: 10 | |
o.integer '--timeout', 'Number of seconds to show the desktop notification for', default: 60 | |
o.string '--icon', 'Path to the icon to show in the notification area', default: '/home/jnronall/bin/images/ballot-box.png' | |
o.on '--help', 'help', 'Show as: String,this help message' | |
end | |
if opts[:help] | |
puts opts | |
exit | |
end | |
http = HTTPClient.new | |
response = http.get "http://vote.code4lib.org/election/results/#{opts[:vote]}.json" | |
results = JSON.parse(response.body) | |
ordered_results = results.sort_by{|talk| talk['score']}.reverse | |
rank = nil | |
score = nil | |
talks_list = [] | |
ordered_results.each_with_index do |talk, index| | |
title = talk['title'].slice(0,30) | |
if talk['id'] == opts[:mytalk] | |
rank = index + 1 | |
score = talk['score'] | |
title = "<b>#{title}</b>" | |
end | |
talks_list << "#{talk['score']} #{title}" | |
end | |
top_talks = talks_list.slice(0,opts[:top]) | |
message = %Q|<a href="http://vote.code4lib.org/election/results/#{opts[:vote]}">Code4Lib Vote</a>| | |
rank_message = "\nRANK: #{rank}\nscore: #{score}\nhigh score: #{ordered_results.first['score']}\n\n" | |
message << rank_message | |
top_ten = top_talks.slice(0,10) | |
# top_rest = top_talks.slice(10,top_talks.length) | |
message << top_ten.join("\n") | |
# message << "\n\n--------------\n\n" | |
# message << top_rest.join("\n\n") | |
puts message | |
# Change this to whatever desktop notification system you use | |
Libnotify.show(body: message, :timeout => opts[:timeout], icon_path: opts[:icon]) | |
log_directory = "~/tmp/" | |
logfile = File.expand_path(File.join(log_directory, '/code4lib-vote.log')) | |
FileUtils.mkdir_p log_directory unless File.exist? log_directory | |
File.open(logfile, 'a') do |fh| | |
fh.puts DateTime.now.to_s | |
fh.puts message | |
fh.puts "\n----------------\n" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment