Skip to content

Instantly share code, notes, and snippets.

@craigeley
Last active April 6, 2018 02:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigeley/f023715cf09f569ff5a4fe00b6127693 to your computer and use it in GitHub Desktop.
Save craigeley/f023715cf09f569ff5a4fe00b6127693 to your computer and use it in GitHub Desktop.
When used with Hazel, a script that parses data from the Reporter app and starts a new time entry in Toggl.
#!/usr/bin/ruby
# Scipt for Using Reporter to start time entries in Toggl
require 'time'
require 'json'
# Prevent encoding errors on OSX
if RUBY_VERSION =~ /2.*.*/
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
end
# Set home folder username
user = "USERNAME"
# Set Filepath - This is a fudge to avoid hidden and Dropbox files from being pulled in
filepath = %x{ls -t "/Users/#{user}/Dropbox/Apps/Reporter-App" | sed -n 1p}
if filepath !~ /json/
filepath = %x{ls -t "/Users/#{user}/Dropbox/Apps/Reporter-App" | sed -n 2p}
end
filepath = "/Users/#{user}/Dropbox/Apps/Reporter-App/#{filepath}"
filepath = filepath.strip
# Get the current file and parse it
file = File.open(filepath, encoding: 'UTF-8')
json = file.read
data = JSON.parse(json)
# Grab the data - generic answers for sleep (imp=3) and wake (imp=4), user input for everything else
imp = data["snapshots"][-1]["reportImpetus"].to_i
if imp == 3
tag = "Sleep"
act = "Heading to bed"
elsif imp == 4
tag = "Sleep"
act = "Waking up"
else
data["snapshots"][-1]["responses"].each do |response|
if response["questionPrompt"] =~ /tag/
tag = response["tokens"][0]["text"]
elsif response["questionPrompt"] =~ /doing/
act = response["tokens"][0]["text"]
end
end
end
# Start a toggle task - using full paths for Hazel
`/usr/local/bin/python /usr/local/bin/toggl.py start '#{act}' @#{tag}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment