Created
June 17, 2010 04:21
-
-
Save kwharrigan/441676 to your computer and use it in GitHub Desktop.
Manage Kindle 2.5 collections.json file
This file contains 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 python | |
import optparse | |
import simplejson as json | |
import time | |
LONGNAME = "%s@en-US" | |
def load_file(filename): | |
''' | |
Load a json file and return the resulting object | |
''' | |
return json.loads(open(filename, 'r').read()) | |
def write_file(coll, filename): | |
open(filename, 'w').write(json.dumps(coll)) | |
def add(coll, item): | |
''' | |
Add an item to collection | |
''' | |
print "Adding item %s" % item | |
time_ms = int(time.time()*1000) | |
new_item = {} | |
name = LONGNAME % item | |
if not ((name) in coll): | |
coll[name] = {'items':[],'lastAccess':time_ms} | |
def delete(coll, item): | |
name = LONGNAME % item | |
if name in coll: | |
del coll[name] | |
else: | |
print 'item %s not found' % item | |
def main(): | |
# Generate options and parse arguments | |
list = False | |
usage = "usage: %prog [options] filename" | |
p = optparse.OptionParser(usage=usage) | |
p.add_option('--add', '-a', help="add an item") | |
p.add_option('--delete', '-d', help="delete an item") | |
p.add_option('-l', action="store_true", dest="list", help="list item names") | |
options, args = p.parse_args() | |
# Must have only 1 argument, the filename | |
if len(args) != 1: | |
p.print_help() | |
return | |
# Load the json file | |
coll = load_file(args[0]) | |
# Process options | |
if options.add: | |
add(coll, options.add) | |
if options.delete: | |
delete(coll, options.delete) | |
if options.list: | |
for key in coll.keys(): | |
print str(key.split('@')[0]) | |
# Write back to file | |
write_file(coll, args[0]) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment