Created
August 25, 2014 22:37
-
-
Save kenkeiter/f9e353f93dc0d7d8e935 to your computer and use it in GitHub Desktop.
Get your RescueTime productivity using Ruby and the RescueTime API.
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
require 'rest_client' | |
require 'json' | |
class RescueTimeProfile | |
API_BASE_URL = 'https://www.rescuetime.com/anapi' | |
TIME_SCORE_SCALE = { | |
2 => 1, | |
1 => 0.75, | |
0 => 0.50, | |
-1 => 0, | |
-2 => 0 | |
} | |
def initialize(api_key) | |
@api_key = api_key | |
end | |
def productivity | |
query_params = { | |
key: @api_key, | |
format: 'json', | |
op: 'select', | |
vn: 0, | |
pv: 'interval', | |
rs: 'day', | |
rb: Time.now.strftime('%Y-%m-%d'), | |
re: (Time.now + (60 * 60 * 24)).strftime('%Y-%m-%d'), | |
rk: 'productivity' | |
} | |
resp = RestClient.get(API_BASE_URL + "/data", params: query_params) | |
data = JSON.parse(resp) | |
# go through and parse the data | |
total_seconds = 0 | |
productive_seconds = 0 | |
data['rows'].each do |row| | |
seconds = row[1].to_i | |
score = row[3].to_i | |
total_seconds += seconds | |
p = seconds * TIME_SCORE_SCALE.fetch(score) | |
productive_seconds += p | |
end | |
productivity = (100 * productive_seconds / total_seconds).round | |
return [productivity, 99].min | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment