Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created July 28, 2012 11:51
Show Gist options
  • Save ttscoff/3193003 to your computer and use it in GitHub Desktop.
Save ttscoff/3193003 to your computer and use it in GitHub Desktop.
Log my public gists for the last 24 hours to Day One and nvALT
#!/usr/bin/env ruby
require 'open-uri'
require 'time'
require 'erb'
require 'rubygems'
require 'json'
user = 'ttscoff'
dayone = true # log to day one? (true or false)
textlog = "~/Dropbox/nvALT2.2/GitLogger.md" # set to false to disable
def e_sh(str)
str.to_s.gsub(/(?=[^a-zA-Z0-9_.\/\-\x7F-\xFF\n])/, '\\').gsub(/\n/, "'\n'").sub(/^$/, "''")
end
res = open(URI.parse("https://api.github.com/users/#{user}/gists")).read
json = JSON.parse(res)
now = Time.now()
yesterday = now - (60 * 60 * 24)
output = ""
json.each {|gist|
date = Time.parse(gist['created_at'])
if date > yesterday
output += "* Created [Gist ##{gist['id']}](#{gist["html_url"]})\n"
output += " * #{gist["description"]}\n"
else
break
end
}
exit if output.strip == ""
if dayone
uuid = %x{uuidgen}.gsub(/-/,'').strip
datestamp = Time.now.utc.iso8601
starred = false
dayonedir = %x{ls ~/Library/Mobile\\ Documents/|grep dayoneapp}.strip
dayonepath = "~/Library/Mobile\ Documents/#{dayonedir}/Documents/Journal_dayone/entries/"
entry = "## Gists for #{now.strftime("%D")}:\n\n#{output}"
template = ERB.new <<-XMLTEMPLATE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Creation Date</key>
<date><%= datestamp %></date>
<key>Entry Text</key>
<string><%= entry %></string>
<key>Starred</key>
<<%= starred %>/>
<key>UUID</key>
<string><%= uuid %></string>
</dict>
</plist>
XMLTEMPLATE
fh = File.new(File.expand_path(dayonepath+uuid+".doentry"),'w+')
fh.puts template.result(binding)
fh.close
# puts "ENTRY ADDED"
# puts "------------------------"
# puts "Time: " + datestamp
# puts "UUID: " + uuid
# puts "Starred: " + starred.to_s
# puts "Entry: " + entrytext
end
if textlog
entry = "---\n\n### #{now.strftime('%D')} [gist]:\n\n" + output + "\n\n"
open(File.expand_path(textlog), 'a') { |f|
f.puts entry
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment