Skip to content

Instantly share code, notes, and snippets.

@craigeley
Last active July 23, 2018 16:37

Revisions

  1. craigeley revised this gist Feb 23, 2014. 1 changed file with 70 additions and 53 deletions.
    123 changes: 70 additions & 53 deletions sifttter.rb
    Original file line number Diff line number Diff line change
    @@ -1,39 +1,35 @@
    #!/usr/bin/ruby
    # Sifttter: An IFTTT-to-Day One Logger by Craig Eley 2014 <http://craigeley.com>
    # SIFTTTER 1.5: 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>
    #
    # Multiple Date Function by Paul Hayes 2014 <http://paulrhayes.com>
    #
    # Notes:
    # * Uses `mdfind` to locate a specific folder of IFTTT-generated text files changed in the last day
    # * The location of your folder should be hardcoded in line 53
    # * Scans leading timestamps in each line matching today's date
    # * The location of your folder should be hardcoded in line 67, and the location of your Day One in line 66
    # * Scans leading timestamps in each line matching the selected dates
    # * Does not alter text files in any way
    # * Changes ampersand ('&') to 'and' so the script keeps running
    # * Does not require the Day One CLI tool
    # * Only generates report if there are completed tasks found
    # * Compiles all results into a single Day One entry
    # * Compiles each day into a single Day One entry
    # * It's configured to locate a Dropbox-synced journal, so
    # * If you use iCloud you'll can just uncomment lines 26 and 27, and comment line 28
    # * To set the Day One entries to starred, just change `starred = false` to true on line 24
    # * If you use iCloud you'll can just uncomment lines 64 and 65, and comment line 66
    # * To set the Day One entries to starred, just change false to true on line 24

    require 'time'
    require 'erb'
    require 'date'
    require 'optparse'

    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/"
    dayonepath = "/Users/USERNAME/Dropbox/Apps/Day\ One/Journal.dayone/entries/"

    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>
    <date><%= filename %>T04:58:00Z</date>
    <key>Entry Text</key>
    <string><%= entrytext %></string>
    <key>Starred</key>
    @@ -48,48 +44,69 @@
    </plist>
    XMLTEMPLATE

    today = Time.now().strftime('%B %d, %Y')
    cdate = Time.now().strftime('%m/%d/%Y')
    files = %x{mdfind -onlyin /Users/USERNAME/Dropbox/IFTTT/Sifttter 'kMDItemContentModificationDate >= "$time.today(-1)"' | grep -v -i daily | sort}
    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!

    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
    project = "### " + File.basename(file).gsub(/^.*?\/([^\/]+)$/,"\\1") + "\n"
    found_completed = false
    lines.each_line do |line|
    if line =~ /&/
    line.gsub!(/[&]/, 'and')
    end
    if line =~ /#{today}/
    found_completed = true
    project += line.gsub(/@done/,'').gsub(/#{today}.../,'').strip + "\n"
    end
    end
    end
    if found_completed
    projects.push(project)
    end
    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

    def e_sh(str)
    str.to_s.gsub(/(?=["\\])/, '\\')
    end
    # dayonedir = %x{ls ~/Library/Mobile\\ Documents/|grep dayoneapp}.strip
    # dayonepath = "~/Library/Mobile\ Documents/#{dayonedir}/Documents/Journal_dayone/entries/"
    dayonepath = "/Users/USERNAME/Dropbox/Apps/Day\ One/Journal.dayone/entries/"
    files = %x{mdfind -onlyin /Users/USERNAME/Dropbox/IFTTT -name '.txt' | grep -v -i daily | sort}

    if projects.length <=0
    abort "No entries found"
    end
    date_range.each do |date|
    formatted_date = date.strftime('%B %d, %Y')
    filename = (date + 1).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 #{today}\n\n"
    projects.each do |project|
    entrytext += project.gsub(/.txt/, ' ') + "\n\n"
    end
    fh = File.new(File.expand_path(dayonepath+uuid+".doentry"),'w+')
    fh.puts template.result(binding)
    fh.close
    puts "Entry logged for #{today}"
    if projects.length > 0
    entrytext = "# Things done on #{formatted_date}\n\n"
    uuid = %x{uuidgen}.gsub(/-/,'').strip
    projects.each do |project|
    entrytext += project.gsub(/.txt/, ' ') + "\n"
    end
    file = "#{dayonepath}#{uuid}.doentry"
    fh = File.new(File.expand_path(file),'w+')
    fh.puts template.result(binding)
    fh.close
    puts "Entry Generated for #{formatted_date}"
    end
    end
  2. @fieldnoise fieldnoise revised this gist Jan 9, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions sifttter.rb
    Original file line number Diff line number Diff line change
    @@ -79,6 +79,10 @@ def e_sh(str)
    str.to_s.gsub(/(?=["\\])/, '\\')
    end

    if projects.length <=0
    abort "No entries found"
    end

    if projects.length > 0
    entrytext = "# Things done on #{today}\n\n"
    projects.each do |project|
    @@ -87,4 +91,5 @@ def e_sh(str)
    fh = File.new(File.expand_path(dayonepath+uuid+".doentry"),'w+')
    fh.puts template.result(binding)
    fh.close
    puts "Entry logged for #{today}"
    end
  3. @fieldnoise fieldnoise revised this gist Jan 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sifttter.rb
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@
    starred = false

    # dayonedir = %x{ls ~/Library/Mobile\\ Documents/|grep dayoneapp}.strip
    # dayonepath = "~/Library/Mobile\Documents/#{dayonedir}/Documents/Journal_dayone/entries/"
    # dayonepath = "~/Library/Mobile\ Documents/#{dayonedir}/Documents/Journal_dayone/entries/"
    dayonepath = "/Users/USERNAME/Dropbox/Apps/Day\ One/Journal.dayone/entries/"

    template = ERB.new <<-XMLTEMPLATE
  4. @fieldnoise fieldnoise created this gist Jan 7, 2014.
    90 changes: 90 additions & 0 deletions sifttter.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    #!/usr/bin/ruby
    # 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 changed in the last day
    # * The location of your folder should be hardcoded in line 53
    # * Scans leading timestamps in each line matching today's date
    # * Does not alter text files in any way
    # * Changes ampersand ('&') to 'and' so the script keeps running
    # * Does not require the Day One CLI tool
    # * Only generates report if there are completed tasks found
    # * Compiles all results into a single Day One entry
    # * It's configured to locate a Dropbox-synced journal, so
    # * If you use iCloud you'll can just uncomment lines 26 and 27, and comment line 28
    # * To set the Day One entries to starred, just change `starred = false` to true on line 24

    require 'time'
    require 'erb'
    require 'date'

    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/"
    dayonepath = "/Users/USERNAME/Dropbox/Apps/Day\ One/Journal.dayone/entries/"

    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><%= entrytext %></string>
    <key>Starred</key>
    <<%= starred %>/>
    <key>Tags</key>
    <array>
    <string>daily logs</string>
    </array>
    <key>UUID</key>
    <string><%= uuid %></string>
    </dict>
    </plist>
    XMLTEMPLATE

    today = Time.now().strftime('%B %d, %Y')
    cdate = Time.now().strftime('%m/%d/%Y')
    files = %x{mdfind -onlyin /Users/USERNAME/Dropbox/IFTTT/Sifttter 'kMDItemContentModificationDate >= "$time.today(-1)"' | grep -v -i daily | sort}

    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
    project = "### " + File.basename(file).gsub(/^.*?\/([^\/]+)$/,"\\1") + "\n"
    found_completed = false
    lines.each_line do |line|
    if line =~ /&/
    line.gsub!(/[&]/, 'and')
    end
    if line =~ /#{today}/
    found_completed = true
    project += line.gsub(/@done/,'').gsub(/#{today}.../,'').strip + "\n"
    end
    end
    end
    if found_completed
    projects.push(project)
    end
    end

    def e_sh(str)
    str.to_s.gsub(/(?=["\\])/, '\\')
    end

    if projects.length > 0
    entrytext = "# Things done on #{today}\n\n"
    projects.each do |project|
    entrytext += project.gsub(/.txt/, ' ') + "\n\n"
    end
    fh = File.new(File.expand_path(dayonepath+uuid+".doentry"),'w+')
    fh.puts template.result(binding)
    fh.close
    end