Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create QR Codes from a Google Authenticator SQLite Database
#!/usr/bin/env python2.7
import argparse
import os
import qrcode
import qrcode.image.pil
import sqlite3
import sys
import urllib
class AuthenticatorAccount(object):
def __init__(self, account_name, account_desc, secret):
self.account_name = account_name
self.account_desc = account_desc
self.secret = secret
def __repr__(self):
return "AuthenticatorAccount@%s%s" % (hex(id(self))[2:], self.__dict__)
def __main__():
parser = argparse.ArgumentParser()
parser.add_argument("database", help="The SQLite database file.")
args = parser.parse_args()
if not os.path.isfile(args.database):
sys.stderr.write("Unable to open %s.\n" % (args.database,))
sys.stderr.flush()
sys.exit(1)
conn = sqlite3.connect(args.database)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute("SELECT * FROM accounts ORDER BY _id;")
row = None
while True:
row = cursor.fetchone()
if row is None:
break
account = AuthenticatorAccount(row['issuer'] or row['original_name'], row['email'],
row['secret'])
print """Saving "%s" to "qrcode-account-%02d.svg" """[:-1] % (account.account_desc,
row['_id'])
qr = qrcode.make("otpauth://totp/%s?secret=%s&issuer=%s" % (account.account_desc,
account.secret, account.account_name), image_factory=qrcode.image.pil.PilImage)
with open("qrcode-account-%02d.png" % (row['_id'],), "wb") as f:
qr.save(f)
if __name__ == "__main__":
__main__()
@Gerifield

This comment has been minimized.

Copy link

@Gerifield Gerifield commented Oct 29, 2014

Exactly what I wanted, thanks! :) 👍

@zekus

This comment has been minimized.

Copy link

@zekus zekus commented Feb 10, 2018

to make this work:
pip2 install pillow qrcode
thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment