#!/usr/bin/python """ ttMakeKvtml.py version 0.3 - Released XX-XX-2009 Copyright (c) 2006-2009 Eric Hielscher Released under the GPL License http://www.gnu.org/copyleft/gpl.html ToolTipTranslation homepage: http://ehielscher.org/toolTip.php This program is used in conjunction with the ToolTipTranslation Greasemonkey UserScript. The script translates words from one language into another while they are viewed in the Firefox web browser, caching the results in a series of cookies. This program extracts the information from these cookies (deleting them in the process), creating a .kvtml file to facilitate practicing the words looked up using a flashcard program such as KWordQuiz. In order to use this program, you must configure it by creating a .ttMakeKvtml file in your home directory. This file must contain as its first line the complete path to a Firefox user profile in which the cookies are to be found. Usage: ./ttMakeKvtml.py ttMakeKvtml.py overwrites if it already exists. """ import sys import os import sqlite3 # Usage Information def usage(): "Print usage message and exit." print """Usage: ./ttMakeKvtml.py ttMakeKvtml.py pulls information from the cookies generated by the ToolTipTranslation UserScript and saves them into a .kvtml file. In order to use it, you must configure it by creating a .ttMakeKvtml file in your home directory. This file must contain as its first line the complete path to a Firefox user profile in which the cookies are to be found. ttMakeKvtml.py overwrites if it already exists. """ sys.exit(1) # Compress the words in the word list into a comma-delimited string def compressWords(words): "Compress the words in the word list into a comma-delimited string." strWords = words[1].decode("utf-8") for word in words[2:]: strWords = strWords + ", " + word.decode("utf-8") return strWords # Check for the proper number of arguments arglen = len(sys.argv) if arglen < 2: usage() outputFile = sys.argv[1] # Try to compile a list of new words from the lang.user.js cookies try: configFile = open(os.path.expanduser("~/.ttMakeKvtml")) except: print "Error opening .ttMakeKvtml file." usage() profile = configFile.readline()[:-1] configFile.close() try: conn = sqlite3.connect(profile + "/cookies.sqlite") conn.text_factory = str c = conn.cursor() except: print "Error opening cookie file: " + profile + "/cookies.sqlite" sys.exit(1) cookies = c.execute('SELECT * FROM moz_cookies ' +\ 'WHERE name = "lang.user.js"') words = [] try: while True: cookie = cookies.fetchone()[2].decode("iso-8859-1") cookie = cookie[1:].split("[") for word in cookie: words.append(word.split("|")) except: pass conn.execute('DELETE FROM moz_cookies WHERE name = "lang.user.js"') conn.commit() conn.close() # Make sure we have something to do if len(words) < 1: print "No words found to process." sys.exit(0) # Try to open the output file try: oFile = open(outputFile, "w") except: print "Error: unable to open output file." sys.exit(1) # Write the header oFile.write("\n") oFile.write("\n") oFile.write("\n") # Write the first row oFile.write(" \n") oFile.write(" " + words[0][0].encode("utf-8") + "\n") oFile.write(" " + compressWords(words[0]) +\ "\n") oFile.write(" \n") # Write the remaining rows for match in words[1:]: oFile.write(" \n") oFile.write(" " + match[0].encode("utf-8") + "\n") oFile.write(" " + compressWords(match) + "\n") oFile.write(" \n") # Write footer oFile.write(" \n") oFile.write(" \n") oFile.write(" \n") oFile.write(" \n") oFile.write("") # Close the file oFile.close()