Created
June 19, 2014 23:09
-
-
Save fancyremarker/f1012f6b79431a0fe6b8 to your computer and use it in GitHub Desktop.
Load Momentum TODO list from a text file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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(','))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To install:
To use:
TODOs should be entered into the text file one per line.