Skip to content

Instantly share code, notes, and snippets.

@Merovex
Last active December 31, 2015 21:23
Show Gist options
  • Save Merovex/7acfee745aaf7b8d3fbe to your computer and use it in GitHub Desktop.
Save Merovex/7acfee745aaf7b8d3fbe to your computer and use it in GitHub Desktop.
#!/path/to/bin/ruby
Encoding.default_external = Encoding::UTF_8 # Stops LaunchD from carping
Encoding.default_internal = Encoding::UTF_8 # Ditto
require 'rubygems'
require 'json'
require 'date'
require "pathname"
require "erb"
require "yaml"
def config(root_dir = nil)
root_dir ||= Dir.pwd
path = Pathname.new(root_dir).join("_writing.yml")
raise "Invalid directory; couldn't find #{path} file." unless File.file?(path)
content = File.read(path)
erb = ERB.new(content).result
YAML.load(erb)
end
def read_json(f); JSON.parse(read_file(f)).clone; end
def read_file(f); f = File.join(f) if f.is_a? Array; File.open(File.expand_path(f),'r').read; end
@config = config('/path/to/config.d')
@log = "/path/to//writing-data/writing-#{Date.today.year}.log"
@today = Date.today.strftime('%Y-%m-%d')
@total = 0
puts "#{Time.now}: Running stats as-of #{@today}..."
@dates = read_json(@log)
@config['projects'].each do |project|
line = open(project[1]).grep(/PreviousSession/)[0]
words = (/Words="(\d+)"/.match(line)[1]).to_i
date = DateTime.parse(/Date=\"([^\"]+?)\"/.match(line)[1]).strftime("%Y-%m-%d")
# Ensure today's number is initialized
@dates[@today] = {} if @dates[@today].nil?
@dates[@today][:Total] = 0 if @dates[@today][:Total].nil?
@dates[@today][project[0]] = 0
# Set the last good word count from Scrivener, which might not be @today if we were lazy.
@dates[date] = {} if @dates[date].nil?
@dates[date][:Total] = 0 if @dates[date][:Total].nil?
@dates[date][project[0]] = words
@dates[date][:Total] += words
end
puts "...Total: #{count[:Total]}"
data = JSON.generate(@dates)
File.open(@log,'w').write(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment