Skip to content

Instantly share code, notes, and snippets.

@fofr
Forked from craigeley/sifttter.rb
Last active November 8, 2015 14:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fofr/8922295 to your computer and use it in GitHub Desktop.
Save fofr/8922295 to your computer and use it in GitHub Desktop.
Format sifttter logs into daily markdown files
#!/usr/bin/ruby
# Convert Sifttter logs into markdown files, one per day <http://paulrhayes.com>
# Based on Sifttter: An IFTTT-to-Day One Logger by Craig Eley 2014 <http://craigeley.com>
# Based on tp-dailylog.rb by Brett Terpstra 2012 <http://brettterpstra.com>
#
# Notes:
# * Uses `mdfind` to locate a specific folder of IFTTT-generated text files
# * The location of your folder should be hardcoded in line 23
# * Scans leading timestamps in each line matching formatted_date's date
# * Does not alter text files in any way
# * Changes ampersand ('&') to 'and' so the script keeps running
# * Saves generated markdown into the specified directory
# * Creates one file per day in the format yyyy-mm-dd.md
# * Accepts options for specified date or range of dates, defaults to today
require 'time'
require 'erb'
require 'date'
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: sifttter.rb [options]"
opts.on('-d', '--date DATE', 'Date to generate - Any parseable date string') { |v| options[:date] = v }
opts.on('-s', '--start START', 'Start date - Use with end to generate a range of dates') { |v| options[:start_date] = v }
opts.on('-e', '--end END', 'End date - Use with start to generate a range of dates') { |v| options[:end_date] = v }
end.parse!
if options[:start_date] && options[:end_date]
start_date = Date.parse(options[:start_date])
end_date = Date.parse(options[:end_date])
date_range = (start_date..end_date).map { |date| date }
else
date = options[:date] ? Date.parse(options[:date]) : Time.now()
date_range = [date]
end
output_path = '/Users/USERNAME/Dropbox/IFTTT/Sifttter/Markdown/'
files = %x{mdfind -onlyin /Users/USERNAME/Dropbox/IFTTT/Sifttter -name '.txt' | grep -v -i daily | sort}
date_range.each do |date|
formatted_date = date.strftime('%B %d, %Y')
filename = date.strftime('%Y-%m-%d')
projects = []
files.split("\n").each do |file|
if File.exists?(file.strip)
f = File.open(file.strip, encoding: 'UTF-8')
lines = f.read
f.close
# Uses filename as header for section, strips out filetype
project = "## " + File.basename(file).gsub(/^.*?\/([^\/]+)$/,"\\1").capitalize + "\n"
found_completed = false
lines.each_line do |line|
if line =~ /&/
line.gsub!(/[&]/, 'and')
end
if line =~ /#{formatted_date}/
found_completed = true
# Removes formatted date and @done pattern from entry
project += line.gsub(/@done/,'').gsub(/#{formatted_date}.../,'').strip + "\n"
end
end
end
if found_completed
projects.push(project)
end
end
if projects.length > 0
entrytext = "# Things done on #{formatted_date}\n\n"
projects.each do |project|
entrytext += project.gsub(/.txt/, ' ') + "\n"
end
file = "#{output_path}#{filename}.md"
fh = File.new(File.expand_path(file),'w+')
fh.puts entrytext
fh.close
puts "#{formatted_date} - #{file} generated"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment