Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Hack for rescuetime-beeminder integration
require 'rubygems'
require 'json'
gem 'beeminder', "0.2.2"
require 'beeminder'
require 'open-uri'
require 'uri'
require 'ruby-debug'
# Pulls data from Rescuetime and sends it to Beeminder.
# Maybe run it in an hourly cron job.
# Enter your keys and tokens below.
# (c) 2012 Reto Stamm
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
@rescuetime_key = "RESCUETIME_KEY"
@beeminder_token = "BEEMINDER_TOKEN"
# Current seconds that rescuetime has on file
initial_offset = {}
initial_offset.default = 0
initial_offset[2] = 51700
####
# Singlify updates...
$datafile = '/home/beemind/rescuetime.json'
$history = {}
if File.exists?($datafile)
json = File.read($datafile) || "{}"
$history = JSON.parse(json)
end
def save
if File.exists?($datafile)
File.delete($datafile)
end
File.open($datafile,"w") do |f|
puts "Keys of json : #{$history.keys}"
f.write($history.to_json)
end
end
def get_value(date, key)
if not $history.has_key?(date.to_s)
return 0
end
if not $history[date.to_s].has_key?(key)
return 0
end
return $history[date.to_s][key]
end
def store_in_file(date, key, value)
if not $history.has_key?(date.to_s)
$history[date.to_s] = {}
end
$history[date.to_s][key] = value
save
end
####
def getRescueTime(date)
uri = URI.parse("https://www.rescuetime.com/anapi/data?"+
"format=json&key=#{@rescuetime_key}&taxonomy=productivity&restrict_begin=#{date}&restrict_end=#{date}")
stream = open(uri)
raise 'web service error' if (stream.status.first != '200')
rt = JSON.parse(stream.read)
categories = {}
rt['rows'].each do |row|
cat = row[3]
val = row[1]
categories[row[3]] = row[1]
end
return categories
end
def post(date, name, value)
value = 1.0*value/60/60
value = (value*100).round.to_f/100
new_value = value
value = value - get_value(date, name)
m = "Additional #{name} time for #{date}: #{value} for a total of #{new_value} at #{Time.now}"
p m
@bee = Beeminder::User.new @beeminder_token, {}
goal = @bee.goal name
if (value.abs > 0.01)
dp = Beeminder::Datapoint.new("comment" => m,
"timestamp" => DateTime.parse(date.to_s)+0.5,
"value" => value)
goal.add dp
store_in_file(date, name, new_value)
end
end
[Date.today, Date.today-1].each do |date|
categories = getRescueTime(date)
post(date, "rescue-time-productive", categories[1]+categories[2])
post(date, "rescue-time-unproductive", categories[-1]+categories[-2])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment