Skip to content

Instantly share code, notes, and snippets.

@fancyremarker
Created June 19, 2014 23:09
Show Gist options
  • Save fancyremarker/f1012f6b79431a0fe6b8 to your computer and use it in GitHub Desktop.
Save fancyremarker/f1012f6b79431a0fe6b8 to your computer and use it in GitHub Desktop.
Load Momentum TODO list from a text file
#!/usr/bin/env ruby
require 'sqlite3'
require 'json'
require 'securerandom'
EXTENSION_ID = 'laookkfknpbbblfpciffpaejjkokdgca'
DBFILE = "#{ENV['HOME']}/Library/Application Support/Google/Chrome/Default/" \
"Local Storage/chrome-extension_#{EXTENSION_ID}_0.localstorage"
fail 'Momentum database file not found' unless File.exist?(DBFILE)
fail "Usage: #{$0} <todo.txt>" unless ARGV.count == 1
fail "File not found: #{ARGV[0]}" unless File.exist?(ARGV[0])
DB = SQLite3::Database.new(DBFILE)
def lookup(key)
query = "SELECT value FROM ItemTable WHERE key = ?"
DB.execute(query, key)[0][0].force_encoding('UTF-16LE')
rescue
nil
end
def insert(key, value)
DB.execute "INSERT INTO ItemTable VALUES (?, ?)", key, value
end
def update(key, value)
DB.execute "UPDATE ItemTable SET value = ? WHERE key = ?", value, key
end
def convert(string)
converter = Encoding::Converter.new('UTF-8', 'UTF-16LE')
converter.convert(string)
end
existing_todos = (lookup('momentum-todo') || '').split(convert(','))
File.open(ARGV[0]).each_line do |line|
line.chomp!
next if line.empty?
key = "#{SecureRandom.uuid}"
data = { title: line, order: 100, done: false, archive: false, id: key }
insert("momentum-todo-#{key}", convert(data.to_json))
existing_todos << convert(key)
end
update('momentum-todo', existing_todos.join(convert(',')))
@fancyremarker
Copy link
Author

To install:

curl https://gist.githubusercontent.com/fancyremarker/f1012f6b79431a0fe6b8/raw/momentum-todo.rb > /usr/local/bin/momentum-todo
chmod +x /usr/local/bin/momentum-todo

To use:

momentum-todo <todo.txt>

TODOs should be entered into the text file one per line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment