sky-y
12/24/2012 - 9:45 PM

A little script to dump your Twitter .zip archive into an Evernote notebook on OS X. For, you know, cloudiness. Loads tweets at about one pe

A little script to dump your Twitter .zip archive into an Evernote notebook on OS X. For, you know, cloudiness. Loads tweets at about one per second, so don't expect it to be super-zippy. pip install appscript if you haven't, yet, which will give you aem.

#!/usr/bin/env python

import json, appscript, aem, sys, os, glob, datetime

def usage(called_name):
    print 'Usage: %s <path to unzipped twitter archive> <name of Evernote notebook>' % called_name
    print 'e.g., %s tweets/ "My Tweets"' % called_name

def main(tweet_path, notebook_name):
    if not os.path.isdir(tweet_path):
        raise Exception, "%s: not a directory" % tweet_path

    try:
        en = appscript.app('Evernote')
    except aem.findapp.ApplicationNotFoundError, e:
        print 'Are you sure Evernote is installed? :('
        raise e

    if notebook_name in [nb.name() for nb in en.notebooks.get()]:
        raise Exception, 'Notebook %s already exists' % notebook_name

    filespec = os.path.join(tweet_path, 'data/js/tweets/*js')
    twitter_files = glob.glob(filespec)
    if not twitter_files:
        raise Exception, 'Could not find any files matching %s' % filespec

    nb = en.create_notebook(notebook_name)

    for filename in twitter_files:
        f = open(filename, 'rb')
        buf = f.readlines()[1:]
        f.close()
        try:
            archive = json.loads(''.join(buf))
        except Exception, e:
            print 'Uh oh! Hit a rough patch in %s' % filename
            raise e
        for tweet in archive:
            datelist = tweet['created_at'].split(' ')
            datestring = ' '.join(datelist[:4] + datelist[-1:]) # erase timezone info
            date = datetime.datetime.strptime(datestring, '%a %b %d %H:%M:%S %Y')
            en.create_note(with_text = json.dumps(tweet), title=tweet['text'], notebook=nb, created=date)

if __name__ == '__main__':
    if len(sys.argv) != 3:
        usage(sys.argv[0])
        sys.exit(1)
    try:
        main(sys.argv[1], sys.argv[2])
    except Exception, e:
        usage(sys.argv[0])
        print e
        sys.exit(1)