This is an excerpt from the original firepwd textfile

In [ ]:
firepwd.py, an open source tool to decrypt Mozilla protected passwords 
----------------------------------------------------------------------
Introduction
************
This educational tool was written to illustrate how Mozilla passwords (Firefox, Thunderbird) are protected
using contents of files key3.db (or key4.db), logins.json (or signons.sqlite).
Reference documents are:
- Into the Black Box: A Case Study in Obtaining Visibility into Commercial Software, 
D. Plakosh, S. Hissam, K. Wallnau, March 1999, Carnegie Mellon University :
http://www.sei.cmu.edu/library/abstracts/reports/99tn010.cfm
- Dr. Stephen Henson, August 4th 1999 :
http://arc.info/?l=openssl-dev&m=93378860132031&w=2
This code is released under GPL license.
Now part of LaZagne project:
https://github.com/AlessandroZ/LaZagne
You can also read the related article, in french:
http://connect.ed-diamond.com/MISC/MISC-069/Protection-des-mots-de-passe-par-Firefox-et-Thunderbird-analyse-par-la-pratique
Usage
****
By default, firepwd.py processes key3.db (or key4.db) and signons.sqlite files in current directory, 
but an alternative directory can be provided using the -d option. Do not forget the '/' 
at the end.
If a master password has been set, provide it using the -p option.
Valid verbose levels (-v) are from 0 (default) to 2.
$ python firepwd.py -h
Usage: firepwd.py [options] 

firepwd2text.py has a slight addition at the end to create a text file "keys3.txt" with the results.
Then by using cleantext.py the text is cleaned enough to place in the database.
cleantext.py worked with my results it may need to be further cleaned if your results differ too much

In [173]:
%%writefile firepwd2text.py
# decode Firefox passwords (https://github.com/lclevy/firepwd)
# lclevy@free.fr (28 Aug 2013: initial version, Oct 2016: support for logins.json, Feb 2018: support for key4.db)
# for educational purpose only, not production level
# now integrated into https://github.com/AlessandroZ/LaZagne
# tested with python 2.7
# key3.db is read directly, the 3rd party bsddb python module is NOT needed
# NSS library is NOT needed

from struct import unpack
import sys
from binascii import hexlify, unhexlify
import sqlite3
from base64 import b64decode
# https://pypi.python.org/pypi/pyasn1/
from pyasn1.codec.der import decoder
from hashlib import sha1
import hmac
from Crypto.Cipher import DES3
from Crypto.Util.number import long_to_bytes
from optparse import OptionParser
import json


def getShortLE(d, a):
    return unpack('<H', (d)[a:a + 2])[0]


def getLongBE(d, a):
    return unpack('>L', (d)[a:a + 4])[0]


# minimal 'ASN1 to string' function for displaying Key3.db contents
asn1Types = {
    0x30: 'SEQUENCE',
    4: 'OCTETSTRING',
    6: 'OBJECTIDENTIFIER',
    2: 'INTEGER',
    5: 'NULL'}
oidValues = {'2a864886f70d010c050103': '1.2.840.113549.1.12.5.1.3',
             '2a864886f70d0307': '1.2.840.113549.3.7',
             '2a864886f70d010101': '1.2.840.113549.1.1.1'}


def printASN1(d, l, rl):
    type = ord(d[0])
    length = ord(d[1])
    if length & 0x80 > 0:  # http://luca.ntop.org/Teaching/Appunti/asn1.html,
        nByteLength = length & 0x7f
        length = ord(d[2])
        # Long form. Two to 127 octets. Bit 8 of first octet has value "1" and
        # bits 7-1 give the number of additional length octets.
        skip = 1
    else:
        skip = 0
    #print '%x:%x' % ( type, length )
    print '  ' * rl, asn1Types[type],
    if type == 0x30:
        print '{'
        seqLen = length
        readLen = 0
        while seqLen > 0:
            #print seqLen, hexlify(d[2+readLen:])
            len2 = printASN1(d[2 + skip + readLen:], seqLen, rl + 1)
            #print 'l2=%x' % len2
            seqLen = seqLen - len2
            readLen = readLen + len2
        print '  ' * rl, '}'
        return length + 2
    elif type == 6:  # OID
        print oidValues[hexlify(d[2:2 + length])]
        return length + 2
    elif type == 4:  # OCTETSTRING
        print hexlify(d[2:2 + length])
        return length + 2
    elif type == 5:  # NULL
        print 0
        return length + 2
    elif type == 2:  # INTEGER
        print hexlify(d[2:2 + length])
        return length + 2
    else:
        if length == l - 2:
            printASN1(d[2:], length, rl + 1)
            return length

# extract records from a BSD DB 1.85, hash mode
# obsolete with Firefox 58.0.2 and NSS 3.35, as key4.db (SQLite) is used


def readBsddb(name):
    f = open(name, 'rb')
    # http://download.oracle.com/berkeley-db/db.1.85.tar.gz
    header = f.read(4 * 15)
    magic = getLongBE(header, 0)
    if magic != 0x61561:
        print 'bad magic number'
        sys.exit()
    version = getLongBE(header, 4)
    if version != 2:
        print 'bad version, !=2 (1.85)'
        sys.exit()
    pagesize = getLongBE(header, 12)
    nkeys = getLongBE(header, 0x38)
    if options.verbose > 1:
        print 'pagesize=0x%x' % pagesize
        print 'nkeys=%d' % nkeys

    readkeys = 0
    page = 1
    nval = 0
    val = 1
    db1 = []
    while (readkeys < nkeys):
        f.seek(pagesize * page)
        offsets = f.read((nkeys + 1) * 4 + 2)
        offsetVals = []
        i = 0
        nval = 0
        val = 1
        keys = 0
        while nval != val:
            keys += 1
            key = getShortLE(offsets, 2 + i)
            val = getShortLE(offsets, 4 + i)
            nval = getShortLE(offsets, 8 + i)
            #print 'key=0x%x, val=0x%x' % (key, val)
            offsetVals.append(key + pagesize * page)
            offsetVals.append(val + pagesize * page)
            readkeys += 1
            i += 4
        offsetVals.append(pagesize * (page + 1))
        valKey = sorted(offsetVals)
        for i in range(keys * 2):
            #print '%x %x' % (valKey[i], valKey[i+1])
            f.seek(valKey[i])
            data = f.read(valKey[i + 1] - valKey[i])
            db1.append(data)
        page += 1
        #print 'offset=0x%x' % (page*pagesize)
    f.close()
    db = {}

    for i in range(0, len(db1), 2):
        db[db1[i + 1]] = db1[i]
    if options.verbose > 1:
        for i in db:
            print '%s: %s' % (repr(i), hexlify(db[i]))
    return db


def decrypt3DES(globalSalt, masterPassword, entrySalt, encryptedData):
    # see http://www.drh-consultancy.demon.co.uk/key3.html
    hp = sha1(globalSalt + masterPassword).digest()
    pes = entrySalt + '\x00' * (20 - len(entrySalt))
    chp = sha1(hp + entrySalt).digest()
    k1 = hmac.new(chp, pes + entrySalt, sha1).digest()
    tk = hmac.new(chp, pes, sha1).digest()
    k2 = hmac.new(chp, tk + entrySalt, sha1).digest()
    k = k1 + k2
    iv = k[-8:]
    key = k[:24]
    if options.verbose > 0:
        print 'key=' + hexlify(key), 'iv=' + hexlify(iv)
    return DES3.new(key, DES3.MODE_CBC, iv).decrypt(encryptedData)


def decodeLoginData(data):
    # first base64 decoding, then ASN1DERdecode
    asn1data = decoder.decode(b64decode(data))
    # for login and password, keep :(key_id, iv, ciphertext)
    return asn1data[0][0].asOctets(
    ), asn1data[0][1][1].asOctets(), asn1data[0][2].asOctets()


def getLoginData():
    conn = sqlite3.connect(options.directory + 'signons.sqlite')
    logins = []
    c = conn.cursor()
    try:
        c.execute("SELECT * FROM moz_logins;")
    except sqlite3.OperationalError:  # since Firefox 32, json is used instead of sqlite3
        loginf = open(options.directory + 'logins.json', 'r').read()
        jsonLogins = json.loads(loginf)
        if 'logins' not in jsonLogins:
            print 'error: no \'logins\' key in logins.json'
            return []
        for row in jsonLogins['logins']:
            encUsername = row['encryptedUsername']
            encPassword = row['encryptedPassword']
            logins.append(
                (decodeLoginData(encUsername),
                 decodeLoginData(encPassword),
                 row['hostname']))
        return logins
    # using sqlite3 database
    for row in c:
        encUsername = row[6]
        encPassword = row[7]
        if options.verbose > 1:
            print row[1], encUsername, encPassword
        logins.append(
            (decodeLoginData(encUsername),
             decodeLoginData(encPassword),
             row[1]))
    return logins


def extractSecretKey(masterPassword, keyData):
    # see http://www.drh-consultancy.demon.co.uk/key3.html
    pwdCheck = keyData['password-check']
    if options.verbose > 1:
        print 'password-check=' + hexlify(pwdCheck)
    entrySaltLen = ord(pwdCheck[1])
    entrySalt = pwdCheck[3: 3 + entrySaltLen]
    if options.verbose > 1:
        print 'entrySalt=%s' % hexlify(entrySalt)
    encryptedPasswd = pwdCheck[-16:]
    globalSalt = keyData['global-salt']
    if options.verbose > 1:
        print 'globalSalt=%s' % hexlify(globalSalt)
    cleartextData = decrypt3DES(
        globalSalt,
        masterPassword,
        entrySalt,
        encryptedPasswd)
    if cleartextData != 'password-check\x02\x02':
        print 'password check error, Master Password is certainly used, please provide it with -p option'
        sys.exit()

    if unhexlify('f8000000000000000000000000000001') not in keyData:
        return None
    privKeyEntry = keyData[unhexlify('f8000000000000000000000000000001')]
    saltLen = ord(privKeyEntry[1])
    nameLen = ord(privKeyEntry[2])
    #print 'saltLen=%d nameLen=%d' % (saltLen, nameLen)
    privKeyEntryASN1 = decoder.decode(privKeyEntry[3 + saltLen + nameLen:])
    data = privKeyEntry[3 + saltLen + nameLen:]
    printASN1(data, len(data), 0)
    # see
    # https://github.com/philsmd/pswRecovery4Moz/blob/master/pswRecovery4Moz.txt
    entrySalt = privKeyEntryASN1[0][0][1][0].asOctets()
    if options.verbose > 0:
        print 'entrySalt=%s' % hexlify(entrySalt)
    privKeyData = privKeyEntryASN1[0][1].asOctets()
    if options.verbose > 0:
        print 'privKeyData=%s' % hexlify(privKeyData)
    privKey = decrypt3DES(globalSalt, masterPassword, entrySalt, privKeyData)
    print 'decrypting privKeyData'
    if options.verbose > 0:
        print 'decrypted=%s' % hexlify(privKey)
    printASN1(privKey, len(privKey), 0)

    privKeyASN1 = decoder.decode(privKey)
    prKey = privKeyASN1[0][2].asOctets()
    print 'decoding %s' % hexlify(prKey)
    printASN1(prKey, len(prKey), 0)
    prKeyASN1 = decoder.decode(prKey)
    id = prKeyASN1[0][1]
    key = long_to_bytes(prKeyASN1[0][3])
    if options.verbose > 0:
        print 'key=%s' % (hexlify(key))
    return key


def getKey():
    # firefox 58.0.2 / NSS 3.35 with key4.db in SQLite
    conn = sqlite3.connect(options.directory + 'key4.db')
    c = conn.cursor()
    try:
        # first check password
        c.execute("SELECT item1,item2 FROM metadata WHERE id = 'password';")
        row = c.next()
        globalSalt = row[0]  # item1
        item2 = row[1]
        printASN1(item2, len(item2), 0)
        """
     SEQUENCE {
       SEQUENCE {
         OBJECTIDENTIFIER 1.2.840.113549.1.12.5.1.3
         SEQUENCE {
           OCTETSTRING entry_salt_for_passwd_check
           INTEGER 01
         }
       }
       OCTETSTRING encrypted_password_check
     }
    """
        decodedItem2 = decoder.decode(item2)
        entrySalt = decodedItem2[0][0][1][0].asOctets()
        cipherT = decodedItem2[0][1].asOctets()
        clearText = decrypt3DES(
            globalSalt,
            options.masterPassword,
            entrySalt,
            cipherT)  # usual Mozilla PBE
        print 'password check?', clearText == 'password-check\x02\x02'
        if clearText == 'password-check\x02\x02':
            # decrypt 3des key to decrypt "logins.json" content
            c.execute("SELECT a11,a102 FROM nssPrivate;")
            row = c.next()
            a11 = row[0]  # CKA_VALUE
            a102 = row[1]  # f8000000000000000000000000000001, CKA_ID
            printASN1(a11, len(a11), 0)
            """
       SEQUENCE {
         SEQUENCE {
           OBJECTIDENTIFIER 1.2.840.113549.1.12.5.1.3
           SEQUENCE {
             OCTETSTRING entry_salt_for_3des_key
             INTEGER 01
           }
         }
         OCTETSTRING encrypted_3des_key (with 8 bytes of PKCS#7 padding)
       }
      """
            decodedA11 = decoder.decode(a11)
            entrySalt = decodedA11[0][0][1][0].asOctets()
            cipherT = decodedA11[0][1].asOctets()

            key = decrypt3DES(
                globalSalt,
                options.masterPassword,
                entrySalt,
                cipherT)
            print '3deskey', hexlify(key)
    except BaseException:
        keyData = readBsddb(options.directory + 'key3.db')
        key = extractSecretKey(options.masterPassword, keyData)
    return key[:24]


parser = OptionParser(usage="usage: %prog [options]")
parser.add_option(
    "-v",
    "--verbose",
    type="int",
    dest="verbose",
    help="verbose level",
    default=0)
parser.add_option(
    "-p",
    "--password",
    type="string",
    dest="masterPassword",
    help="masterPassword",
    default='')
parser.add_option(
    "-d",
    "--dir",
    type="string",
    dest="directory",
    help="directory",
    default='')
(options, args) = parser.parse_args()

key = getKey()
logins = getLoginData()
f = open('keys3.txt', "a")
if len(logins) == 0:
    print 'no stored passwords'
else:
    print 'decrypting login/password pairs'
for i in logins:
    site = '%20s:' % i[2],  # site URL
    iv = i[0][1]
    ciphertext = i[0][2]  # login (PKCS#7 padding not removed)
    reprout = repr(DES3.new(key, DES3.MODE_CBC, iv).decrypt(ciphertext)), ',',
    iv = i[1][1]
    ciphertext = i[1][2]  # passwd (PKCS#7 padding not removed)
    reprout2 = repr(DES3.new(key, DES3.MODE_CBC, iv).decrypt(ciphertext))
    site = str(site)
    reprout = str(reprout)
    reprout2 = str(reprout2)
    f.write(site)
    f.write(reprout)
    f.write(reprout2)
    f.write("\n")
print ("keys3.txt Containing Passwords Created")
Overwriting firepwd2text.py
In [ ]:
!python firepwd2text.py 

Running the code above will have a similar result as below

SEQUENCE { SEQUENCE { OBJECTIDENTIFIER 1.22.340.113439.3.312.35.4.43 SEQUENCE { OCTETSTRING 76egrhjr76e8ejej847584jdjury78fhfjssss INTEGER 03 } } OCTETSTRING 76d7ejr8490t0gktifdjd8746373839d0d0ds-s0d9d8d7f8f9d0s-s-dd-0d9d8d7d7d7d8d9d0dd-d0s8s7s8s } decrypting privKeyData SEQUENCE { INTEGER 02 SEQUENCE { OBJECTIDENTIFIER 340.113439.3.312.35.4.43 NULL 0 } OCTETSTRING 7364537638383984948f80000000000000000000000000000026378dhdkf857fg07696587dhdhg3527383939040494 } decoding 304554443333400f8000000000000000000000000000001020126378dhdkf857fg076965404900201000287dhdhg35273839 SEQUENCE { INTEGER 00 INTEGER 00f5000000000000000000000000000005 INTEGER 00 INTEGER 645dgfhjru7rheekdkdjdjhsdkdkjdj INTEGER 00 INTEGER 00 INTEGER 00 INTEGER 00 INTEGER 25 } decrypting login/password pairs keys3.txt Containing Passwords Created

EXAMPLE OF keys3.txt generated by firepwd2text.py

In [225]:
%%writefile keys3.txt
(u'https://localhost:',)("'\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'MakeAPassword\x03\x03\x03'
(u'https://dr.agfkas.in:',)("'fgfg00l@yahoo.com\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'dfy#3444#6\x03\x03\x03'
(u'https://accounts.wired.com:',)("'joeesh@yahoo.com\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'Time554e4rf\x01'
(u'https://developer.vuereia.com:',)("'jahwe@yahoo\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'wewewewe\x08\x08\x08\x08\x08\x08\x08\x08'
(u'https://wwws5444.ibm.com:',)("'jahwe@yahoo.com\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'Time554e4rf\x05\x05\x05\x05\x05'
(u'https://isdsdty.com:',)("'sdff@yahoo.com\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'Time554e4rf\x04\x04\x04\x04'
(u'https://9ghpins.com:',)("'sdff@yahoo.com\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'dfe4544\x08\x08\x08\x08\x08\x08\x08\x08'
(u'https://passport.twitch.tv:',)("'davidfp\\x04\\x04\\x04\\x04'", ',')'dsddfff\x04\x04'
(u'https://www.noiiip.com:',)("'luekln\\x02\\x02'", ',')'Hgyt$3$4#RFg\x04\x04\x04\x04'
(u'https://postimage.org:',)("'sdff@yahoo.com\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'Time554e4rf\x04\x04\x04\x04'
(u'https://signup.wordpress.com:',)("'sdff@yahoo.com\\x07\\x07\\x07\\x07\\x07\\x07\\x07'", ',')'Time554e4rf\x02\x02'
(u'https://support.mozilla.org:',)("'ty7777@yahoo.com\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'de444453fgs445354$#@\x03\x03\x03'
(u'http://127.0.0.1:',)("'root\\x04\\x04\\x04\\x04'", ',')'dfg56565\x05\x05\x05\x05\x05'
(u'https://www.sitsdent.com:',)("'\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'ddfthtttt\x01'
(u'https://sgpadddy.com:',)("'madhggtt3\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'ddfthtttt\x06\x06\x06\x06\x06\x06'
(u'https://www.db4free.net:',)("'denterngine\\x06\\x06\\x06\\x06\\x06\\x06'", ',')'ddfthtttt\x03\x03\x03'
(u'https://www.rebornm.com:',)("'jlnmyer\\x06\\x06\\x06\\x06\\x06\\x06'", ',')'dfdf656\x06\x06\x06\x06\x06\x06'
(u' https://www.ibm.com:',)("'sdff@yahoo.com\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'dftrrr566\x05\x05\x05\x05\x05'
(u'http://www.gimpusers.com:',)("'jahhhjrup\\x04\\x04\\x04\\x04'", ',')'wewe455667#$##\x04\x04\x04\x04'
(u'https://apps.ionic.io:',)("'sdff@yahoo.com\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'nKnVe3C2nKnVe3C2\x08\x08\x08\x08\x08\x08\x08\x08'
(u'https://adobeid.services.adobe.com:',)("'jahral@yahoo.com\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'455667#$###32\x07\x07\x07\x07\x07\x07\x07'
(u'https://login.yahoo.com:',)("'sdff@yahoo.com\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'455667#$##\x06\x06\x06\x06\x06\x06'
(u' https://twitter.com:',)("'\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'455667#$##\x05\x05\x05\x05\x05'
(u'https://secure.tagged.com:',)("'biakira80@yahoo.com\\x01'", ',')'lovfirepwd2te333\x07\x07\x07\x07\x07\x07\x07'
(u'https://www.shdtopsh.com:',)("'jghhl@yahoo.com\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'455667#$##\x07\x07\x07\x07\x07\x07\x07'
(u'http://localhost:8000:',)("'sdff@yahoo.com\\x04\\x04\\x04\\x04'", ',')'455667#$##123p\x04\x04\x04\x04'
(u'http://www.opesdffy.com:',)("'@jackghhup\\x02\\x02'", ',')'5667#$##123p\x06\x06\x06\x06\x06\x06'
(u'https://www.redtobble.com:',)("'jacghhthrup\\x04\\x04\\x04\\x04'", ',')'5667#$##123p\x03\x03\x03'
(u'https://www.memord.com:',)("'madhattersom\\x03\\x03\\x03'", ',')'dseer5454\x06\x06\x06\x06\x06\x06'
(u' https://twitsyr.com:',)("'newdude\\x03\\x03\\x03'", ',')'45juuiu77\x06\x06\x06\x06\x06\x06'
(u'https://lognowhost.com:',)("'sdff@yahoo.com\\x07\\x07\\x07\\x07\\x07\\x07\\x07'", ',')'ddd5656522@1ff2\x03\x03\x03'
(u'https://lognowhost.com:',)("'joesm\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'fdddd5656522@1ff212\x05\x05\x05\x05\x05'
(u'https://www.evernote.com:',)("'sdff@yahoo.com\\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08'", ',')'ddsdff@yahoocomff2\x08\x08\x08\x08\x08\x08\x08\x08'
Overwriting keys3.txt

Prepare the keys3.tx for Database entry

New file is keysClean3.txt

In [22]:
%%writefile cleantext.py
import re
lines = open("keys3.txt").readlines()
filein = open("addto3.txt", "a")
for line in lines:
    line = line.lstrip()
    line = line.replace("(u'","")
    line = line.replace("\\x08","")
    line = line.replace(":',)(\"'"," ")
    line = line.replace("\\\\\\\\\\\\\\\\'\", ',')'"," ")
    line = line.replace(":',)(\"'","\n")
    line = line.replace("\\x05","");line = line.replace("\\x03","");line = line.replace("\\x06","")
    line = line.replace(":',)(\"'","\n")
    line = line.replace("\\\\\'\", ',')'","  ")
    line = line.replace("\\x07"," ")
    line = line.replace("\\x04","  ")
    line = line.replace("\  \  \  \  '\", ',')'","  ")
    line = line.replace("\\","  ");line = line.replace("'","")
    line = line.replace("\", ,)","");line = line.replace("x02","")
    line = line.replace("x01","");line = line.replace("x01","")
    line = re.sub(' +',' ',line)
    filein.write(line)
filein.close()    
Writing cleantext.py

Example of the clean text file created

In [229]:
fi = open('addto3.txt', "r").readlines()
for line in fi:
    line = line.replace("\n","")
    print line
https://localhost MakeAPassword
https://dr.agfkas.in fgfg00l@yahoo.com dfy#3444#6
https://accounts.wired.com joeesh@yahoo.com Time554e4rf 
https://developer.vuereia.com jahwe@yahoo wewewewe
https://wwws5444.ibm.com jahwe@yahoo.com Time554e4rf
https://isdsdty.com sdff@yahoo.com Time554e4rf 
https://9ghpins.com sdff@yahoo.com dfe4544
https://passport.twitch.tv davidfp dsddfff 
https://www.noiiip.com luekln Hgyt$3$4#RFg 
https://postimage.org sdff@yahoo.com Time554e4rf 
https://signup.wordpress.com sdff@yahoo.com Time554e4rf 
https://support.mozilla.org ty7777@yahoo.com de444453fgs445354$#@
http://127.0.0.1 root dfg56565
https://www.sitsdent.com ddfthtttt 
https://sgpadddy.com madhggtt3 ddfthtttt
https://www.db4free.net denterngine ddfthtttt
https://www.rebornm.com jlnmyer dfdf656
 https://www.ibm.com sdff@yahoo.com dftrrr566
http://www.gimpusers.com jahhhjrup wewe455667#$## 
https://apps.ionic.io sdff@yahoo.com nKnVe3C2nKnVe3C2
https://adobeid.services.adobe.com jahral@yahoo.com 455667#$###32 
https://login.yahoo.com sdff@yahoo.com 455667#$##
 https://twitter.com 455667#$##
https://secure.tagged.com biakira80@yahoo.com lovfirepwd2te333 
https://www.shdtopsh.com jghhl@yahoo.com 455667#$## 
http://localhost:8000 sdff@yahoo.com 455667#$##123p 
http://www.opesdffy.com @jackghhup 5667#$##123p
https://www.redtobble.com jacghhthrup 5667#$##123p
https://www.memord.com madhattersom dseer5454
 https://twitsyr.com newdude 45juuiu77
https://lognowhost.com sdff@yahoo.com ddd5656522@1ff2
https://lognowhost.com joesm fdddd5656522@1ff212
https://www.evernote.com sdff@yahoo.com ddsdff@yahoocomff2

Enter the list into a database line by line

In [231]:
import sqlite3
conn = sqlite3.connect('PassWords.db')
conn.text_factory = str
c = conn.cursor()
c.execute("CREATE VIRTUAL TABLE IF NOT EXISTS PASSWD USING FTS4(sites)")
conn.commit()
filein = open("addto3.txt", "r").readlines()
for line in filein:
    c.execute("INSERT INTO PASSWD VALUES (?)", (line,))
conn.commit()

Search for the passwords You may use any word or part or part of word Example: the database entry is: https://www.shdtopsh.com jghhl@yahoo.com 455667#$##

I want to get password for  https://www.shdtopsh.com
i could use shdto, motion or www.shdtopsh.com, even opsh would work
or you could copy past the url

if you know a password, but do not know what site it is for, search the password

Using part of a site URL -- shdto as a search term

In [233]:
import sqlite3
conn = sqlite3.connect('PassWords.db')
conn.text_factory = str
c = conn.cursor()
search = raw_input("SEARCH : ")
for row in c.execute("SELECT rowid,* FROM PASSWD"):
    if search in row[1]:
        print row[0],row[1]
    
SEARCH : shdto
25 https://www.shdtopsh.com jghhl@yahoo.com 455667#$## 

Using part of a password -- e444453fg as a search term

In [234]:
import sqlite3
conn = sqlite3.connect('PassWords.db')
conn.text_factory = str
c = conn.cursor()
search = raw_input("SEARCH : ")
for row in c.execute("SELECT rowid,* FROM PASSWD"):
    if search in row[1]:
        print row[0],row[1]
    
SEARCH : e444453fg
12 https://support.mozilla.org ty7777@yahoo.com de444453fgs445354$#@

Search by email

In [270]:
import sqlite3
conn = sqlite3.connect('PassWords.db')
conn.text_factory = str
c = conn.cursor()
search = raw_input("SEARCH : ")
for row in c.execute("SELECT rowid,* FROM PASSWD"):
    if search in row[1]:
        print row[0],row[1]
    
SEARCH : phpmyadmin
In [274]:
!ls
addto3.txt	 PW.db
firepwd2text.py  retrieve-firefox-passwords-enter-sqlite-database.ipynb
keys3.txt	 snippets.db
PassWords.db
In [273]:
!ls ../fire*
../firepwd:
addto3.txt	 PW.db
firepwd2text.py  retrieve-firefox-passwords-enter-sqlite-database.ipynb
keys3.txt	 snippets.db
PassWords.db

../firepword:
addto3.txt	 logins.json
addto.txt	 mozilla_pbe.pdf
cleanpw.txt	 mozilla_pbe.svg
firepwd2text.py  PassWords.db
firepwd.py	 PW3.db
key3.db		 PW.db
key4.db		 readme.txt
keys3.txt	 retrieve-firefox-passwords-enter-sqlite-database.ipynb
keysClean3.txt	 saved.txt
keysClean.txt	 signons.sqlite
keys.txt	 snippets.db
In [269]:
import sqlite3
conn = sqlite3.connect('PassWords.db')
conn.text_factory = str
count = 0
c = conn.cursor()
for row in c.execute("SELECT rowid,* FROM PASSWD"):
    row=str(row[1])
    row = row.split()
    for line in row:
        print line
https://localhost
MakeAPassword
https://dr.agfkas.in
fgfg00l@yahoo.com
dfy#3444#6
https://accounts.wired.com
joeesh@yahoo.com
Time554e4rf
https://developer.vuereia.com
jahwe@yahoo
wewewewe
https://wwws5444.ibm.com
jahwe@yahoo.com
Time554e4rf
https://isdsdty.com
sdff@yahoo.com
Time554e4rf
https://9ghpins.com
sdff@yahoo.com
dfe4544
https://passport.twitch.tv
davidfp
dsddfff
https://www.noiiip.com
luekln
Hgyt$3$4#RFg
https://postimage.org
sdff@yahoo.com
Time554e4rf
https://signup.wordpress.com
sdff@yahoo.com
Time554e4rf
https://support.mozilla.org
ty7777@yahoo.com
de444453fgs445354$#@
http://127.0.0.1
root
dfg56565
https://www.sitsdent.com
ddfthtttt
https://sgpadddy.com
madhggtt3
ddfthtttt
https://www.db4free.net
denterngine
ddfthtttt
https://www.rebornm.com
jlnmyer
dfdf656
https://www.ibm.com
sdff@yahoo.com
dftrrr566
http://www.gimpusers.com
jahhhjrup
wewe455667#$##
https://apps.ionic.io
sdff@yahoo.com
nKnVe3C2nKnVe3C2
https://adobeid.services.adobe.com
jahral@yahoo.com
455667#$###32
https://login.yahoo.com
sdff@yahoo.com
455667#$##
https://twitter.com
455667#$##
https://secure.tagged.com
biakira80@yahoo.com
lovfirepwd2te333
https://www.shdtopsh.com
jghhl@yahoo.com
455667#$##
http://localhost:8000
sdff@yahoo.com
455667#$##123p
http://www.opesdffy.com
@jackghhup
5667#$##123p
https://www.redtobble.com
jacghhthrup
5667#$##123p
https://www.memord.com
madhattersom
dseer5454
https://twitsyr.com
newdude
45juuiu77
https://lognowhost.com
sdff@yahoo.com
ddd5656522@1ff2
https://lognowhost.com
joesm
fdddd5656522@1ff212
https://www.evernote.com
sdff@yahoo.com
ddsdff@yahoocomff2

------------------------------------------------

In [2]:
!ls 
addto3.txt	 mozilla_pbe.pdf
firepwd2text.py  mozilla_pbe.svg
firepwd.py	 PassWords.db
keys3.txt	 readme.txt
LICENSE		 retrieve-firefox-passwords-enter-sqlite-database.ipynb
mozilla_db	 snippets.db
In [4]:
import sqlite3
conn = sqlite3.connect('PassWords.db')
conn.text_factory = str
c = conn.cursor()
c.execute("CREATE VIRTUAL TABLE IF NOT EXISTS PASSWD USING FTS4(sites)")
conn.commit()
filein = open("addMore.txt", "r").readlines()
for line in filein:
    c.execute("INSERT INTO PASSWD VALUES (?)", (line,))
conn.commit()
In [5]:
!ls
addMore.txt	 mozilla_pbe.pdf
addto3.txt	 mozilla_pbe.svg
firepwd2text.py  PassWords.db
firepwd.py	 readme.txt
keys3.txt	 retrieve-firefox-passwords-enter-sqlite-database.ipynb
LICENSE		 snippets.db
mozilla_db

Manually Add more sites

In [3]:
%%writefile addMore.txt
http://somesite.com joesmit002@badmail.com 746^56778 
http://some3site.com jsit002@badmail.com 746wewe6778
http://somesites.com joest0022@monkmail.com 746^dd778
http://somesite123.com joesmit002@badmail.com 233223dd
http://some122site.com joesmit002@somemail.com vvgfg$$6778
http://12somesite.com joe002@badmail.com 74sdffdf8
http://122somesite.com joesmit002@mymail.com vfvd56778
http://som22esite.com jo002@badmail.com &^hyy56778
http://somes222ite.com smit002@badmail.com IunGfr778     
Writing addMore.txt

Verify addMore.txt was entered

In [6]:
import sqlite3
conn = sqlite3.connect('PassWords.db')
conn.text_factory = str
c = conn.cursor()
search = raw_input("SEARCH : ")
for row in c.execute("SELECT rowid,* FROM PASSWD"):
    if search in row[1]:
        print row[0],row[1]
    
SEARCH : smit002@badmail.com
34 http://somesite.com joesmit002@badmail.com 746^56778 

37 http://somesite123.com joesmit002@badmail.com 233223dd

42 http://somes222ite.com smit002@badmail.com IunGfr778     
For security reasons you may wish to delete your original "keys3.txt" and "addto3.txt" you may reprint the files in text from the sqlite database at any time

Print Database to Text File

In [13]:
import sqlite3
conn = sqlite3.connect('PassWords.db')
conn.text_factory = str
c = conn.cursor()
filein = open("datbase2text.txt", "a")
for row in c.execute("SELECT rowid,* FROM PASSWD"):
        text = row[1]
        text = str(text)
        filein.write(text)
    
In [15]:
filein = open("datbase2text.txt", "r").readlines()
for line in filein:
    print line
https://localhost MakeAPassword

https://dr.agfkas.in fgfg00l@yahoo.com dfy#3444#6

https://accounts.wired.com joeesh@yahoo.com Time554e4rf 

https://developer.vuereia.com jahwe@yahoo wewewewe

https://wwws5444.ibm.com jahwe@yahoo.com Time554e4rf

https://isdsdty.com sdff@yahoo.com Time554e4rf 

https://9ghpins.com sdff@yahoo.com dfe4544

https://passport.twitch.tv davidfp dsddfff 

https://www.noiiip.com luekln Hgyt$3$4#RFg 

https://postimage.org sdff@yahoo.com Time554e4rf 

https://signup.wordpress.com sdff@yahoo.com Time554e4rf 

https://support.mozilla.org ty7777@yahoo.com de444453fgs445354$#@

http://127.0.0.1 root dfg56565

https://www.sitsdent.com ddfthtttt 

https://sgpadddy.com madhggtt3 ddfthtttt

https://www.db4free.net denterngine ddfthtttt

https://www.rebornm.com jlnmyer dfdf656

 https://www.ibm.com sdff@yahoo.com dftrrr566

http://www.gimpusers.com jahhhjrup wewe455667#$## 

https://apps.ionic.io sdff@yahoo.com nKnVe3C2nKnVe3C2

https://adobeid.services.adobe.com jahral@yahoo.com 455667#$###32 

https://login.yahoo.com sdff@yahoo.com 455667#$##

 https://twitter.com 455667#$##

https://secure.tagged.com biakira80@yahoo.com lovfirepwd2te333 

https://www.shdtopsh.com jghhl@yahoo.com 455667#$## 

http://localhost:8000 sdff@yahoo.com 455667#$##123p 

http://www.opesdffy.com @jackghhup 5667#$##123p

https://www.redtobble.com jacghhthrup 5667#$##123p

https://www.memord.com madhattersom dseer5454

 https://twitsyr.com newdude 45juuiu77

https://lognowhost.com sdff@yahoo.com ddd5656522@1ff2

https://lognowhost.com joesm fdddd5656522@1ff212

https://www.evernote.com sdff@yahoo.com ddsdff@yahoocomff2http://somesite.com joesmit002@badmail.com 746^56778 

http://some3site.com jsit002@badmail.com 746wewe6778

http://somesites.com joest0022@monkmail.com 746^dd778

http://somesite123.com joesmit002@badmail.com 233223dd

http://some122site.com joesmit002@somemail.com vvgfg$$6778

http://12somesite.com joe002@badmail.com 74sdffdf8

http://122somesite.com joesmit002@mymail.com vfvd56778

http://som22esite.com jo002@badmail.com &^hyy56778

http://somes222ite.com smit002@badmail.com IunGfr778     

SEARCH the TEXT FILE

In [17]:
filein = open("datbase2text.txt", "r").readlines()
search = raw_input("SEARCH : ")
for line in filein:
    if search in line:
        print line
SEARCH : Time
https://accounts.wired.com joeesh@yahoo.com Time554e4rf 

https://wwws5444.ibm.com jahwe@yahoo.com Time554e4rf

https://isdsdty.com sdff@yahoo.com Time554e4rf 

https://postimage.org sdff@yahoo.com Time554e4rf 

https://signup.wordpress.com sdff@yahoo.com Time554e4rf 

In [1]:
from time import sleep
alltext =open("cleanpw.txt", "r").readlines()
count = 1
for line in alltext:
    if len(line)>4:
        print count,line
        count = count +1
        line = line.replace("\n", "")
        count = count+1
        if count >30:break
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-1-17c77fb1dec8> in <module>()
      1 from time import sleep
----> 2 alltext =open("cleanpw.txt", "r").readlines()
      3 count = 1
      4 for line in alltext:
      5     if len(line)>4:

IOError: [Errno 2] No such file or directory: 'cleanpw.txt'

BONUS CREATE A SNIPPET DATABASE

In [55]:
import sqlite3
conn = sqlite3.connect('snippets.db')
conn.text_factory = str
c = conn.cursor()
c.execute('CREATE VIRTUAL TABLE IF NOT EXISTS snippet USING FTS3(code, keywords)')
code ="""

< paste your snippet here >

"""
keywords="""

< paste your desription / keywords here >

"""
c.execute("INSERT INTO PASSWD VALUES (?,?)", (code,keywords))
conn.commit()
In [205]:
import sqlite3
conn = sqlite3.connect('snippets.db')
conn.text_factory = str
c = conn.cursor()
c.execute('CREATE VIRTUAL TABLE IF NOT EXISTS snippet USING FTS3(code, keywords)')
code ="""
fi = open('addto3.txt', "r").readlines()
for line in fi:
    line = line.replace("\n","")
    print line
"""
keywords="""
open and view a text file
open a file to read
open text
view text
"""
c.execute("INSERT INTO snippet VALUES (?,?)", (code,keywords))
conn.commit()

Example search : open text

In [207]:
import sqlite3
conn = sqlite3.connect('snippets.db')
conn.text_factory = str
c = conn.cursor()
search = raw_input("SEARCH : ")
for row in c.execute("SELECT rowid,* FROM snippet WHERE snippet MATCH ?", (search,)):
    print row[0],row[1],"\nkeywords:\n",row[2]
    
SEARCH : open text
1 
fi = open('addto3.txt', "r").readlines()
for line in fi:
    line = line.replace("
","")
    print line

keywords:

open and view a text file
open a file to read
open text
view text

Search SNIPPETS.db

In [209]:
import sqlite3
conn = sqlite3.connect('/home/jack/hubiC/Databases/SNIPPETS.db')
conn.text_factory = str
c = conn.cursor()
search = raw_input("SEARCH : ")
for row in c.execute("SELECT rowid,* FROM snippets WHERE snippets MATCH ?", (search,)):
    print row[0],row[1],"\nkeywords:\n",row[2]
    
SEARCH : insert
2 
def insert_info(store):
    with sqlite3.connect("misc.db") as db:
        #use a text_factory that can interpret 8-bit bytestrings 
        db.text_factory = str
        cursor = db.cursor()
        #db.text_factory = str
        sql = "insert into storeit (data0, data1, data2) values (?, ?, ?)"
        cursor.execute(sql, store)
        db.commit()
        
        OR
        conn.text_factory = str

keywords:
text_factory, 8-bit bytestrings, 8-bit
8 
import sqlite3
import time
#account = "TEDTalks.txt"
account = "symmetrymag.txt"
#account = "elonmusk.txt"
#account = "realDonaldTrump.txt"
user = account[:-4]
lines = open(account,"r")
line = lines.readline()
for line in lines:
    conn = sqlite3.connect('collection.db')
    conn.text_factory = str
    c = conn.cursor()
    c.execute("INSERT INTO tweets VALUES (?,?)", (line, user)) 
    conn.commit()
    conn.close()        
    
    #print line         

conn.commit()
conn.close()                 

keywords:
insert data, text file to database, text to data
11 
%%writefile /home/jack/hidden/Authorize.py
def keys():
    ftp = "ftp.MYsite.com"
    username = "Josephine"
    password = "WhaWah2525"
    login = (ftp,username,password)
    return login
    ------------
import sys
sys.path.insert(0, "/home/jack/hidden"
import Authorize
ftp = Authorize.keys()[0]
username = Authorize.keys()[1]
password = Authorize.keys()[2]

print ftp, username, password    
    

keywords:
Authorize, ftp, hidden passwords, Authorize.keys
16 
import sqlite3
import feedparser
import time
import sqlite3
Dbase = 'bigfeedfts.db'
conn = sqlite3.connect(Dbase)
c = conn.cursor()
c.execute(
CREATE VIRTUAL TABLE IF NOT EXISTS bbctech 
USING FTS3(head, feed);
)
count=0
while count<35:
    count=count+1
    if count==1:feed='http://feeds.bbci.co.uk/news/technology/rss.xml'
    if count==2:feed='http://www.cbn.com/cbnnews/us/feed/'
    d = feedparser.parse(feed)
    for post in d.entries:
        aa = `d['feed']['title'],d['feed']['link'],d.entries[0]['link']`
        bb = `post.title + ": " + post.link + ""`
        conn = sqlite3.connect(Dbase)
        c = conn.cursor()
        c.execute("INSERT INTO bbctech VALUES (?,?)", (aa,bb))
        conn.commit()
        conn.close()
        
        
conn = sqlite3.connect(Dbase)
c = conn.cursor()# Never
count=0
for row in c.execute('SELECT * FROM bbctech ORDER BY rowid DESC'):
    row=str(row)
    row=row.replace("(u","");row=row.replace('", u"u',"
")
    row=row.replace("/', u'","   ");row=row.replace('"',"")
    row=row.replace("', u'","  ");row=row.replace("')","  ")
    row=row.replace("'","");row=row.replace("  , uu","
")
    count=count+1
    print"
Number :",count," -----
",(row)

keywords:
rss, RSS, rss feeds, sqlite3
21 
import tweepy #https://github.com/tweepy/tweepy
import csv
import sys
sys.path.insert(0,"/home/jack/anaconda2/envs/py27/lib/python2.7/site-packages")
import Key
from random import randint
consumer_key = Key.twiter()[0]
consumer_secret = Key.twiter()[1]
access_key = Key.twiter()[2]
access_secret = Key.twiter()[3]

def get_all_tweets(screen_name):
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_key, access_secret)
    api = tweepy.API(auth)
    alltweets = []	
    new_tweets = api.user_timeline(screen_name = screen_name,count=200)
    alltweets.extend(new_tweets)
    oldest = alltweets[-1].id - 1
    while len(new_tweets) > 0:
        print "getting tweets before %s" % (oldest)
        new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)
        alltweets.extend(new_tweets)
        oldest = alltweets[-1].id - 1
        print (len(alltweets))
        if (len(alltweets)) >200:
            outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in alltweets]
            with open('%s_tweets.csv' % screen_name, 'wb') as f:
                writer = csv.writer(f)
                writer.writerow(["id","created_at","text"])
                writer.writerows(outtweets)
            pass
if __name__ == '__main__':
    USER = raw_input("User  : ") or "CNN"
    get_all_tweets(USER)
---  
get all tweets, csv, get many tweets, tweets to csv, tweets.csv    

keywords:
get all tweets, csv, get many tweets, tweets to csv, tweets.csv
29 
# works 
import MySQLdb as db
import json 
import base64
    
con = db.connect("localhost","root","ThinkPadT$#", "searchdb01")
file = ""[mylist.jsn
while 1:
    line = file.readline()
    if not line:
        break
    pass # do something 
#listname='mylist.json'
#stringlistvalue=json.dumps(listname)
""
keywords = ""
database, code, python, lesson 1, Oh234
""
encodedlistvalue=base64.b64encode(file)
with con:
    cur = con.cursor()
    cur.execute("CREATE TABLE Code(Id INT PRIMARY KEY AUTO_INCREMENT,                   Name VARCHAR(2500), Keywords VARCHAR(500))")
    #cur.execute("INSERT INTO Code(Name) VALUES('%s')" % (encodedlistvalue))
    cur.execute("INSERT INTO Code(Name, Keywords) VALUES('%s','%s')" % (encodedlistvalue, keywords))

keywords:
MySQL, Creating a MySQL database, libmysqlclient-dev, searchdb01
33 
import sqlite3
import os.path
from os import listdir, getcwd
from IPython.core.display import Image 

def get_picture_list(rel_path):
    abs_path = os.path.join(os.getcwd(),rel_path)
    print 'abs_path =', abs_path
    dir_files = os.listdir(abs_path)
    #print dir_files
    return dir_files

picture_list = get_picture_list('snippets')
print picture_list
import sqlite3
import os.path
from os import listdir, getcwd
from IPython.core.display import Image 

def create_or_open_db(db_file):
    db_is_new = not os.path.exists(db_file)
    conn = sqlite3.connect(db_file)
    if db_is_new:
        print 'Creating schema'
        sql = '''create table if not exists PICTURES(
        ID INTEGER PRIMARY KEY AUTOINCREMENT,
        PICTURE BLOB,
        TYPE TEXT,
        FILE_NAME TEXT);'''
        conn.execute(sql) # shortcut for conn.cursor().execute(sql)
    else:
        print 'Schema exists
'
    return conn

def insert_picture(picture_file):
    with open(picture_file, 'rb') as input_file:
        conn = sqlite3.connect(dbname)
        c = conn.cursor()
        ablob = input_file.read()
        base=os.path.basename(picture_file)
        afile, ext = os.path.splitext(base)
        sql = '''INSERT INTO PICTURES
        (PICTURE, TYPE, FILE_NAME)
        VALUES(?, ?, ?);'''
        c.execute(sql,[sqlite3.Binary(ablob), ext, afile]) 
        conn.commit()

def loadimages(dbname, path):
    conn = sqlite3.connect(dbname)
    c = conn.cursor()
    #conn.execute("DELETE FROM PICTURES")
    for fn in picture_list:
        picture_file = path+"/"+fn
        insert_picture(picture_file)

    for r in c.execute("SELECT rowid, FILE_NAME FROM PICTURES"):
        print r[0],r[1]
   
    conn.commit()


def get_image(picture_id):
    conn = sqlite3.connect(dbname)
    c = conn.cursor()
    c.execute("SELECT PICTURE, TYPE, FILE_NAME FROM PICTURES WHERE id = ?;",(picture_id,))
    #sql = "SELECT PICTURE, TYPE, FILE_NAME FROM PICTURES WHERE id = 19"
    param = {'id': picture_id}
    #c.execute(sql, param)
    ablob, ext, afile = c.fetchone()
    filename = afile + ext
    with open(filename, 'wb') as output_file:
        output_file.write(ablob)
    return filename


dbname = "ImageC.db"
db_file = create_or_open_db(dbname)
path = "snippets/"
loadimages(dbname, path)
filename = get_image(16)
print filename
Image(filename=filename)

-----------------
store, retrieve images,SQLite Databasestore,
retrieve images, from SQLite , Database

keywords:
store, retrieve images, from SQLite Database
35 
%%writefile Image2SQLite.py
import sqlite3
import os.path
from os import listdir, getcwd
from IPython.core.display import Image 

def getImage_list(rel_path):
    abs_path = os.path.join(os.getcwd(),rel_path)
    print 'abs_path =', abs_path
    dir_files = os.listdir(abs_path)
    return dir_files

def create_or_open_db(dbname):
    db_is_new = not os.path.exists(db_file)
    conn = sqlite3.connect(db_file)
    if db_is_new:
        print 'Creating schema'
        sql = '''create table if not exists images(
        ID INTEGER PRIMARY KEY AUTOINCREMENT,
        image BLOB,
        TYPE TEXT,
        imagE TEXT);'''
        conn.execute(sql) # shortcut for conn.cursor().execute(sql)
    else:
        print 'Schema exists
'
        conn.commit()
        conn.close()
    return conn

def insertImage(dbname, imageFile):
    with open(imageFile, 'rb') as input_file:
        conn = sqlite3.connect(dbname)
        c = conn.cursor()
        ablob = input_file.read()
        base=os.path.basename(imageFile)
        afile, ext = os.path.splitext(base)
        sql = '''INSERT INTO images
        (image, TYPE, imagE)
        VALUES(?, ?, ?);'''
        c.execute(sql,[sqlite3.Binary(ablob), ext, afile]) 
        conn.commit()

def loadimagE(dbname, path):
    conn = sqlite3.connect(dbname)
    c = conn.cursor()
    #conn.execute("DELETE FROM images")
    for fn in image_list:
        imageFile = path+"/"+fn
        insertImage(imageFile)

    for r in c.execute("SELECT rowid, imagE FROM images"):
        print r[0],r[1]
   
    conn.commit()
    conn.close()

def image_id(dbname):
    conn = sqlite3.connect(dbname)
    c = conn.cursor()
    rows = c.execute("SELECT rowid, TYPE, imagE FROM images")
    for row in rows:
        print row[0],row[2]+row[1]    
    return
    
def get_image(dbname,image_id):
    conn = sqlite3.connect(dbname)
    c = conn.cursor()
    c.execute("SELECT image, TYPE, imagE FROM images WHERE id = ?;",(image_id,))
    #sql = "SELECT image, TYPE, imagE FROM images WHERE id = 19"
    param = {'id': image_id}
    #c.execute(sql, param)
    ablob, ext, afile = c.fetchone()
    filename = afile + ext
    with open(filename, 'wb') as output_file:
        output_file.write(ablob)
    return filename
---------------
USAGE:
import Image2Data
picture_list = Image2Data.get_picture_list('snippets')
print picture_list

import Image2Data
dbname = "ImageE.db"
Image2Data.create_or_open_db(dbname)

#insert one image
import Image2Data
dbname = "ImageD.db"
picture_file = "01.jpg"
Image2Data.insert_picture(dbname, picture_file)

import Image2Data
dbname = "ImageD.db"
path = "snippets"
loadimages(dbname, path)

def image_id(dbname):
    conn = sqlite3.connect(dbname)
    c = conn.cursor()
    rows = c.execute("SELECT rowid, TYPE, FILE_NAME FROM PICTURES")
    for row in rows:
        print row[0],row[2]+row[1]
    
#list images by id
dbname = "ImageD.db"
image_id(dbname)

#retrieve image by id
filename = get_image(dbname,1)
print filename
Image(filename=filename)



keywords:
wikipedia , bad_words, bad words, disambiguation, remove lines from text 
36 
%%writefile Image2SQLite.py
import sqlite3
import os.path
from os import listdir, getcwd
from IPython.core.display import Image 

def getImage_list(rel_path):
    abs_path = os.path.join(os.getcwd(),rel_path)
    print 'abs_path =', abs_path
    dir_files = os.listdir(abs_path)
    return dir_files

def create_or_open_db(dbname):
    db_is_new = not os.path.exists(db_file)
    conn = sqlite3.connect(db_file)
    if db_is_new:
        print 'Creating schema'
        sql = '''create table if not exists images(
        ID INTEGER PRIMARY KEY AUTOINCREMENT,
        image BLOB,
        TYPE TEXT,
        imagE TEXT);'''
        conn.execute(sql) # shortcut for conn.cursor().execute(sql)
    else:
        print 'Schema exists
'
        conn.commit()
        conn.close()
    return conn

def insertImage(dbname, imageFile):
    with open(imageFile, 'rb') as input_file:
        conn = sqlite3.connect(dbname)
        c = conn.cursor()
        ablob = input_file.read()
        base=os.path.basename(imageFile)
        afile, ext = os.path.splitext(base)
        sql = '''INSERT INTO images
        (image, TYPE, imagE)
        VALUES(?, ?, ?);'''
        c.execute(sql,[sqlite3.Binary(ablob), ext, afile]) 
        conn.commit()

def loadimagE(dbname, path):
    conn = sqlite3.connect(dbname)
    c = conn.cursor()
    #conn.execute("DELETE FROM images")
    for fn in image_list:
        imageFile = path+"/"+fn
        insertImage(imageFile)

    for r in c.execute("SELECT rowid, imagE FROM images"):
        print r[0],r[1]
   
    conn.commit()
    conn.close()

def image_id(dbname):
    conn = sqlite3.connect(dbname)
    c = conn.cursor()
    rows = c.execute("SELECT rowid, TYPE, imagE FROM images")
    for row in rows:
        print row[0],row[2]+row[1]    
    return
    
def get_image(dbname,image_id):
    conn = sqlite3.connect(dbname)
    c = conn.cursor()
    c.execute("SELECT image, TYPE, imagE FROM images WHERE id = ?;",(image_id,))
    #sql = "SELECT image, TYPE, imagE FROM images WHERE id = 19"
    param = {'id': image_id}
    #c.execute(sql, param)
    ablob, ext, afile = c.fetchone()
    filename = afile + ext
    with open(filename, 'wb') as output_file:
        output_file.write(ablob)
    return filename
---------------
USAGE:
import Image2Data
picture_list = Image2Data.get_picture_list('snippets')
print picture_list

import Image2Data
dbname = "ImageE.db"
Image2Data.create_or_open_db(dbname)

#insert one image
import Image2Data
dbname = "ImageD.db"
picture_file = "01.jpg"
Image2Data.insert_picture(dbname, picture_file)

import Image2Data
dbname = "ImageD.db"
path = "snippets"
loadimages(dbname, path)

def image_id(dbname):
    conn = sqlite3.connect(dbname)
    c = conn.cursor()
    rows = c.execute("SELECT rowid, TYPE, FILE_NAME FROM PICTURES")
    for row in rows:
        print row[0],row[2]+row[1]
    
#list images by id
dbname = "ImageD.db"
image_id(dbname)

#retrieve image by id
filename = get_image(dbname,1)
print filename
Image(filename=filename)
------------
images to database , image2data , store images, store images as data, SQLite images


keywords:
images to database , image2data , store images, store images as data, SQLite images
55 
import base64
from time import sleep
title = "FTP_and_Editor.ipynb"
import sqlite3
conn = sqlite3.connect('ftp.db')
conn.text_factory=str 
c = conn.cursor()
with open(title, 'r') as f:
    lines = f.readlines()
    for line in lines:
        encodedlistvalue=base64.b64encode(line)
        c.execute("INSERT INTO ipynb VALUES (?,?)", (encodedlistvalue, line)) 
        conn.commit()
        #print encodedlistvalue

conn.close()
f.close()
    
jupyter to view database from database to jupyter notebook
            

keywords:
store, retrieve images, from SQLite Database
58 
import base64
from time import sleep
import sqlite3
conn = sqlite3.connect('notebooks.db')
conn.text_factory=str 
c = conn.cursor()
line =" "
title = "index"
c.execute("INSERT INTO ipynb VALUES (?,?)", (title, line)) 
conn.commit()
        #print encodedlistvalue

conn.close()
f.close()

keywords:
create index column
69 
import sqlite3
import re
import sys
import time
database = "test.db"
#database = "junk2.db"
conn = sqlite3.connect(database)
conn.text_factory = lambda x: unicode(x, "utf-8", "ignore")
c = conn.cursor()
c.execute(""
CREATE VIRTUAL TABLE IF NOT EXISTS ipynb 
USING FTS4(file, content, description);
"")
conn.commit()
conn.close()
conn = sqlite3.connect(database)
c = conn.cursor()
count=1
titlelist = "ipynb-clean.list"
titles = open(titlelist,"r")
for title in titles.readlines():
    filename = os.path.basename(title)
    # Use for debug print filename,":"
    description = filename
    description = description.replace("-", " ")
    description = description.replace("_", " ")
    description = description.replace(".ipynb", " ")
    description = re.sub("([a-z])([A-Z])","\g<1> \g<2>",description)
    title = title.replace("
", "")
    
    dt=time.ctime(os.path.getctime(title))
    dt=str(dt)
    #dt = dt.replace(" ","")
    description = description+"Date :"+dt
    suf = title.replace("/home/jack/","")
    suf = suf.replace(".ipynb","_")
    suf = suf.replace("/","_")
    filename = suf+filename
    with open(title, "rb") as input_file:
                ablob = input_file.read()
                content  = sqlite3.Binary(ablob)
                c.execute("INSERT INTO ipynb (file, content, description) VALUES(?, ?, ?)", 
                      (filename, content, description))
                
                conn.commit()
    line = file
    #line ="Good-mouse-sizing-and-cropping.ipynb"
    index = "Index"
    c.execute("INSERT INTO ipynb VALUES (?,?,?)", (index, filename, description)) 
    conn.commit()
    count=count+1
    print count,filename, description

c.close()
conn.close() 
        

keywords:
BEST best create a database of jupyter notebooks notebook to database SQLite notebook 2 database
72 
import sqlite3
#database = "FTS4_IPYNB.db"
database = "FTS4_IPYNB_indexed.db"
conn = sqlite3.connect(database)
c = conn.cursor()
conn.text_factory=str 
c.execute(""
CREATE VIRTUAL TABLE IF NOT EXISTS ipynb 
USING FTS4(file, content, description);
"")
conn.commit()
conn.close()
conn = sqlite3.connect(database)
c = conn.cursor()
count=0
while count<19:
    count=count+1
    if count==1:PATH = "/home/jack/Desktop/deep-dream-generator/notebooks/"
    if count==2:PATH = "/home/jack/Desktop/text_stuff/"
    if count==3:PATH = "/home/jack/Desktop/imagebot/"
    if count==4:PATH = "/home/jack/Desktop/Snippet_Warehouse/"
    if count==5:PATH = "/home/jack/Desktop/gitjupyter/"
    if count==6:PATH = "/home/jack/Desktop/jack_watch/"
    if count==7:PATH = "/home/jack/Desktop/jack_watch/nltk/"
    if count==8:PATH = "/home/jack/Desktop/jack_watch/Python-Lectures/"
    if count==9:PATH = "/home/jack/Desktop/jack_watch/jupyter_examples-master/"
    if count==10:PATH = "/home/jack/Desktop/Books/numerical-python-book-code/"
    if count==11:PATH = "/home/jack/Desktop/Books/pydata-book/"
    if count==12:PATH = "/home/jack/Desktop/Ruby/"
    if count==13:PATH = "/home/jack/Desktop/alice/ChatterBot/"
    if count==14:PATH = "/home/jack/Desktop/deep-dream-generator/LOCAL-notebooks/"
    if count==15:PATH = "/home/jack/Desktop/numpy-array-filters/"
    if count==16:PATH = "/home/jack/Desktop/pycode/"
    if count==17:PATH = "/home/jack/Desktop/pycode/vpython2/TrigonometryBot/"
    if count==18:PATH = "/home/jack/Desktop/temp/args_csv_Twython_ImageBot/"
    if count==19:PATH = "/home/jack/python3-starter/notebooks/"
    for file in os.listdir(PATH):
        if file.endswith(".ipynb"):
            filename = PATH+file
            filein = PATH
            filein = filein.replace("/home/jack/", "")
            filein = filein.replace("/", "_")
            filein = filein+file
            description = filein
            description = description.replace("_", " ")
            description = description.replace("-", " ")
            description = description.replace("/", " ")
            description = description+"
"+PATH+file+"
"+file
            with open(filename, "rb") as input_file:
                    ablob = input_file.read()
                    content  = sqlite3.Binary(ablob)
                    c.execute("INSERT INTO ipynb (file, content, description) VALUES(?, ?, ?)", 
                              (filein, content, description))
                    print os.path.join(PATH, file, filein)
                    conn.commit()
            line = file
            #line ="Good-mouse-sizing-and-cropping.ipynb"
            title = "index"
            c.execute("INSERT INTO ipynb VALUES (?,?,?)", (title, file, description)) 
            conn.commit()

c.close()
conn.close() 
        
        

keywords:
SQLite inserting files by directory walk - adding files to database by extension
77 
import sqlite3
import feedparser
import time
import sqlite3
Dbase = 'bigfeedfts.db'
conn = sqlite3.connect(Dbase)
c = conn.cursor()
#c.execute('''
#CREATE TABLE IF NOT EXISTS bbctech
#(head text, feed text)
#''');
c.execute(""
CREATE VIRTUAL TABLE IF NOT EXISTS bbctech 
USING FTS3(head, feed);
"")
count=0
while count<35:
    count=count+1
    if count==1:feed='http://feeds.bbci.co.uk/news/technology/rss.xml'
    if count==2:feed='http://www.cbn.com/cbnnews/us/feed/'
    if count==3:feed='http://feeds.reuters.com/Reuters/worldNews'
    if count==4:feed='http://feeds.bbci.co.uk/news/technology/rss.xml'
    if count==5:feed='http://news.sky.com/info/rss'
    if count==6:feed='http://www.cbn.com/cbnnews/us/feed/'
    if count==7:feed='http://feeds.reuters.com/Reuters/domesticNews'
    if count==8:feed='http://news.yahoo.com/rss/'
    if count==9:feed='http://www.techradar.com/rss'
    if count==10:feed='https://www.wired.com/feed/rss'
    if count==11:feed='http://www.zdnet.com/zdnet.opml'
    if count==12:feed='http://www.computerweekly.com/rss/All-Computer-Weekly-content.xml'
    if count==13:feed='http://gadgets.ndtv.com/rss/feeds'
    if count==14:feed='http://feeds.arstechnica.com/arstechnica/index'        
    if count==15:feed='https://www.techworld.com/news/rss'
    if count==16:feed='https://www.infoworld.com/index.rss'        
    if count==18:feed='https://www.pcworld.com/index.rss'   
    if count==19:feed='http://tech.economictimes.indiatimes.com/rss/technology'
    if count==20:feed='https://www.technologyreview.com/stories.rss'        
    if count==21:feed='http://tech.economictimes.indiatimes.com/rss/topstories'
    if count==22:feed='http://feeds.feedburner.com/digit/latest-from-digit'
    if count==23:feed='http://feeds.techsoup.org/TechSoup_Articles'
    if count==24:feed='http://rss.sciam.com/ScientificAmerican-News?format=xml'
    if count==25:feed='https://www.sciencedaily.com/rss/all.xml'    
    if count==26:feed='http://feeds.nanowerk.com/nanowerk/agWB'
    if count==27:feed='http://feeds.nanowerk.com/NanowerkNanotechnologySpotlight'
    if count==28:feed='http://feeds.nanowerk.com/feedburner/NanowerkRoboticsNews'
    if count==29:feed='http://feeds.nanowerk.com/NanowerkSpaceExplorationNews'
    if count==30:feed='http://www.npr.org/rss/rss.php?id=1019'
    if count==31:feed='http://feeds.nature.com/news/rss/news_s16?format=xml'
    if count==32:feed='http://feeds.latimes.com/latimes/technology?format=xml'
    if count==33:feed='http://feeds.feedburner.com/BadAstronomyBlog?format=xml'
    if count==34:feed='http://feeds.newscientist.com/physics-math'
    if count==35:feed='http://rss.slashdot.org/Slashdot/slashdotMain'
    d = feedparser.parse(feed)
    for post in d.entries:
        aa = `d['feed']['title'],d['feed']['link'],d.entries[0]['link']`
        bb = `post.title + ": " + post.link + ""`
        conn = sqlite3.connect(Dbase)
        c = conn.cursor()
        c.execute("INSERT INTO bbctech VALUES (?,?)", (aa,bb))
        conn.commit()
        conn.close()
        
        
conn = sqlite3.connect(Dbase)
c = conn.cursor()# Never
count=0
for row in c.execute('SELECT * FROM bbctech ORDER BY rowid DESC'):
    row=str(row)
    row=row.replace("(u","");row=row.replace('", u"u',"
")
    row=row.replace("/', u'","   ");row=row.replace('"',"")
    row=row.replace("', u'","  ");row=row.replace("')","  ")
    row=row.replace("'","");row=row.replace("  , uu","
")
    count=count+1
    print"
Number :",count," -----
",(row)

keywords:
SQLite inserting files by list, rss feed, rss,feeds, rss to database sqlite - adding files to database by extension
78 
%%writefile HashCheck.py
"" USEAGE:
(insert hash)

for example to get hash of a filename:
def md5(Path):
    hash_md5 = hashlib.md5()
    with open(Path, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hash_md5.update(chunk)
    return hash_md5.hexdigest()

Hash = md5(title)

import HashCheck
hasht = "jsudsfhndsfjunjsd"
HashCheck.hashfill(Hash)
---------------
import HashCheck
HashCheck.hashcheck()

import HashCheck
HashCheck.killmem()
""
import sqlite3
import sys
import os

def md5(Path):
    hash_md5 = hashlib.md5()
    with open(Path, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hash_md5.update(chunk)
    return hash_md5.hexdigest()

def hashfill(Hash):
    import sqlite3
    database ="tmp-mem"
    conn = sqlite3.connect(database)
    c = conn.cursor()  
    c.execute("CREATE TABLE IF NOT EXISTS Hash(hasht TEXT);")
    c.execute("INSERT INTO Hash(hasht) VALUES(?)",(Hash,))
    conn.commit()
    conn.close()
    
def hashcheck():
    import sqlite3
    database ="tmp-mem"
    conn = sqlite3.connect(database)
    c = conn.cursor()  
    rows = c.execute("SELECT rowid, hasht FROM Hash")
    for row in rows:
        print row[1]
    return
   
def killmem():
    os.remove(database)
    print(database,"Removed!")

keywords:
verify files, hash , duplicate find duplicate files, hashfill (Hash),HashCheck .py,HashCheck.py
79 
#%%writefile GISTstore/post2gist.py
import os
from time import sleep
import sqlite3
import os, requests, sys, json
import GISTkey
username=GISTkey.gistkey()[0]
password=GISTkey.gistkey()[1]
database ="GISTstore/gist.db"
conn = sqlite3.connect(database)
c = conn.cursor()
c.execute(""
CREATE VIRTUAL TABLE IF NOT EXISTS gist 
USING FTS4(content, description, filename);
"")
c.close()
conn.close()
conn = sqlite3.connect(database)
conn.text_factory=str 
c = conn.cursor()
if not os.path.exists('GISTstore'):
    os.makedirs('GISTstore')    
#PATH = "GISTstore/"
#filename = "UnCamel.py"
#content=open(PATH+filename, 'r').read()
filename = "Post_To_Gist _PostToGist.py.ipynb"
content=open(filename, 'r').read()
post = 'https://api.github.com/gists'
r = requests.post(post,json.dumps({'files':{filename:{"content":content}}}),
                  auth=requests.auth.HTTPBasicAuth(username, password)) 
sleep(2)
Url= (r.json()['html_url'])
c.execute("INSERT INTO gist VALUES (?,?,?)", (content, Url, filename)) 
conn.commit()
title = "Index"
c.execute("INSERT INTO gist VALUES (?,?,?)", (title, Url, filename)) 
conn.commit()
for row in c.execute('SELECT rowid, content, description, filename FROM gist     WHERE filename MATCH ?', (filename,)):
        print Url,"
",row[0],row[1],row[2],row[3]
c.close()
conn.close()

keywords:
gist, gist.gisthub , post to gist, save file to sqlite database
125 
import sqlite3
import feedparser
import time
import sqlite3
Dbase = 'test0.db'
conn = sqlite3.connect(Dbase)
c = conn.cursor()
c.execute('''
CREATE VIRTUAL TABLE IF NOT EXISTS merge
USING FTS3(file, keyword);
''')

Dbase = 'test1.db'
con = sqlite3.connect(Dbase)
cc = con.cursor()
cc.execute('''
CREATE VIRTUAL TABLE IF NOT EXISTS merge 
USING FTS3(file0, keyword0);
''')

Dbase = 'test2.db'
co = sqlite3.connect(Dbase)
ccc = co.cursor()
ccc.execute('''
CREATE VIRTUAL TABLE IF NOT EXISTS merge 
USING FTS3(file1, keyword1);
''')
co.commit()

file = " ""
second time - This is data for database test0.db
" ""
keyword = "SQLite inserting one database into another"
c.execute("INSERT INTO merge VALUES (?,?)", (file, keyword))
conn.commit()
for row in c.execute("select * FROM merge"):
    print row[0],row[1]
    ccc.execute("INSERT INTO merge VALUES (?,?)", (row[0],row[1]))
    co.commit()
conn.close()

file0 = " ""
SECOND ENTRY - This is data for database test1.db
" ""
keyword0 = "SQLite inserting test0.db database into test1.db"
cc.execute("INSERT INTO merge VALUES (?,?)", (file0, keyword0))
con.commit()
for row1 in cc.execute("select * FROM merge"):
    print row1[0],row1[1]
    ccc.execute("INSERT INTO merge VALUES (?,?)", (row1[0],row1[1]))
    co.commit()

con.close()
print ("--------------------------------------")
Dbase = 'test2.db'
co = sqlite3.connect(Dbase)
ccc = co.cursor()
for rowz in ccc.execute("select * FROM merge"):
    print  rowz[0],rowz[1]
    
    
--------------------
joining databases merge sqlite database merge db join db merging databases

keywords:
joining databases merge sqlite database merge db join db merging databases
126 
c.execute('CREATE VIRTUAL TABLE IF NOT EXISTS snippet USING FTS3(encodedlistvalue, file, keywords);')

import sqlite3
import base64
conn = sqlite3.connect('snippet.db') 
c = conn.cursor()
conn.text_factory = str
file = ""

""
keywords = "delete sqlite by rowid delete id rowid ROWID"
encodedlistvalue=base64.b64encode(file)
c.execute("INSERT INTO snippet VALUES (?,?,?)", (encodedlistvalue, file, keywords))
conn.commit()
conn.close()

keywords:
create a snippet database, snippet.db snippet db create database insert into database
127 
import sqlite3
import sys
conn = sqlite3.connect('/home/jack/snippet.db')
conn.text_factory = str
c = conn.cursor()
count=0;req=200
search = raw_input("Search : ")
for row in c.execute('SELECT rowid, * FROM snippet WHERE snippet MATCH ?', (search,)):    
    count=count+1
    print "ID : ",(row)[0],(row)[2]," -- KEYWORDS",(row)[3],"
"
    if count > req:
        conn.close()
        sys.exit()



import sqlite3
import base64
conn = sqlite3.connect('home/jack/snippet.db') 
c = conn.cursor()
conn.text_factory = str
file = ""

""
keywords = "delete sqlite by rowid delete id rowid ROWID"
encodedlistvalue=base64.b64encode(file)
c.execute("INSERT INTO snippet VALUES (?,?,?)", (encodedlistvalue, file, keywords))
conn.commit()
conn.close()

keywords:
create a snippet database, snippet.db snippet db create database insert into database
In [18]:
!cp /home/jack/hubiC/Databases/SNIPPETS.db .
In [6]:
import sqlite3
conn = sqlite3.connect('SNIPPETS.db')
conn.text_factory = str
c = conn.cursor()
c.execute('CREATE VIRTUAL TABLE IF NOT EXISTS snippets USING FTS3(code, keywords)')
code ="""
# works 
import os
command = 'rm /usr/local/lib/python2.7/dist-packages/skpy/event.pyc'
os.popen("sudo -S %s"%(command), 'w').write('SA5454%$55')

# works
import getpass
import os
command = "sudo -S rm /usr/local/lib/python2.7/dist-packages/skpy/conn.pyc"
password = getpass.getpass()
os.system('echo %s | %s' % (password, command))

# works
command = "rm /usr/local/lib/python2.7/dist-packages/skpy/msg.pyc"
sudoPassword = 'gh8767hhh'
p = os.system('echo %s|sudo -S %s' % (sudoPassword, command))

----------
sudo in cell use sudo in jupyter use sudo cell 
using sudo cell 
"""
keywords="""
sudo in cell use sudo in jupyter use sudo cell using sudo
using sudo cell 
"""
c.execute("INSERT INTO snippets VALUES (?,?)", (code,keywords))
conn.commit()

Easy great search for small databases

In [19]:
import sqlite3
conn = sqlite3.connect('SNIPPETS.db')
conn.text_factory = str
c = conn.cursor()
search = raw_input("SEARCH : ")
for row in c.execute("SELECT rowid,* FROM snippets"):
    if search in row[1]:
        print row[0],row[1]
    
SEARCH : aggressive
144 
usage: autopep8 [-h] [--version] [-v] [-d] [-i] [--global-config filename]
                [--ignore-local-config] [-r] [-j n] [-p n] [-a]
                [--experimental] [--exclude globs] [--list-fixes]
                [--ignore errors] [--select errors] [--max-line-length n]
                [--line-range line line]
                [files [files ...]]

Automatically formats Python code to conform to the PEP 8 style guide.

positional arguments:
  files                 files to format or '-' for standard in

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -v, --verbose         print verbose messages; multiple -v result in more
                        verbose messages
  -d, --diff            print the diff for the fixed source
  -i, --in-place        make changes to files in place
  --global-config filename
                        path to a global pep8 config file; if this file does
                        not exist then this is ignored (default:
                        ~/.config/pep8)
  --ignore-local-config
                        don't look for and apply local config files; if not
                        passed, defaults are updated with any config files in
                        the project's root directory
  -r, --recursive       run recursively over directories; must be used with
                        --in-place or --diff
  -j n, --jobs n        number of parallel jobs; match CPU count if value is
                        less than 1
  -p n, --pep8-passes n
                        maximum number of additional pep8 passes (default:
                        infinite)
  -a, --aggressive      enable non-whitespace changes; multiple -a result in
                        more aggressive changes
  --experimental        enable experimental fixes
  --exclude globs       exclude file/directory names that match these comma-
                        separated globs
  --list-fixes          list codes for fixes; used by --ignore and --select
  --ignore errors       do not fix these errors/warnings (default: E24)
  --select errors       fix only these errors/warnings (e.g. E4,W)
  --max-line-length n   set maximum allowed line length (default: 79)
  --line-range line line, --range line line
                        only fix errors found within this inclusive range of
                        line numbers (e.g. 1 99); line numbers are indexed at
                        1
autopep8 --in-place --aggressive --aggressive firepwd2text.py
https://pypi.python.org/pypi/autopep8
autopep8 automatically formats Python code to conform to the PEP 8 style guide.
It uses the pycodestyle utility to determine what parts of the code needs to be 
formatted. autopep8 is capable of fixing most of the formatting issues that can 
be reported by pycodestyle.

In [7]:
import sqlite3
conn = sqlite3.connect('SNIPPETS.db')
conn.text_factory = str
c = conn.cursor()
Id = raw_input("SEARCH : ")
c.execute("DELETE FROM snippets where rowid = ?",(Id,))
conn.commit()
conn.close()
    
SEARCH : 147
In [9]:
import sqlite3
conn = sqlite3.connect('SNIPPETS.db')
conn.text_factory = str
c = conn.cursor()
search = raw_input("SEARCH : ")
for row in c.execute("SELECT rowid,* FROM snippets"):
    if search in row[1]:
        print row[0],row[1]
    
SEARCH : ppxy
In [41]:
!fuser SNIPPETS.db
/home/jack/Desktop/firepwd/SNIPPETS.db:  5139
In [ ]:
!kill 5139