#!/usr/bin/env python

import os
import sys
from optparse import OptionParser
from urlparse import urlparse, parse_qs

# Import from 'dist' directory
curdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "dist")
sys.path.append(curdir)

import onetimepass as otp

__author__ = "Mark Embling <mark@markembling.info>"
__version__ = "0.1"

def _get_secret(input):
    '''Attempts to get the secret from the given string - either the whole
    string itself, or the secret from within an otpauth:// URI.
    '''
    if input.startswith("otpauth://"):
        u = urlparse(input)
        q = parse_qs(u.query)
        return (q["secret"][0], u.hostname)
    return (input, None)


if __name__ == "__main__":
    parser = OptionParser()
    parser.description = "Prints the TOTP auth code for the given secret. Can be either the raw secret string or a otpauth:// URI; the script will attempt to auto-detect which is given."
    parser.usage = "%prog [options] secret"
    parser.epilog = "Copyright (c) Mark Embling 2013"

    parser.add_option("-t", "--type", dest="type", 
                      choices=["TOTP", "HOTP"], default="TOTP", 
                      help="Token type (HOTP or TOTP). If a URI is provided, the type will be determined from there. [default: %default]")
    # parser.add_option("-d", "--digits", dest="digits", 
    #                   choices=['6','8'], default='6',
    #                   help="Number of digits to display (6 or 8) [default: %default]")
    parser.add_option("-c", "--count", dest="count", 
                      type="int", default=1,
                      help="Counter for HOTP [default: %default]")

    (options, args) = parser.parse_args()
    ARGS_REQD = 1
    if len(args) != ARGS_REQD:
        parser.error("incorrect number of arguments (%d required, %d given)" % (ARGS_REQD, len(args)))

    # Determine the type and secret
    (secret, type_) = _get_secret(args[0])
    if type_ is None:
        type_ = options.type

    # Get the token and print
    if type_.upper() == "HOTP":
        print(otp.get_hotp(secret, intervals_no=options.count))
    else:
        print(otp.get_totp(secret))

#!/usr/bin/env python

import os
import sys
from optparse import OptionParser
from urlparse import urlparse, parse_qs
import fileinput

# Import from 'dist' directory
curdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "dist")
sys.path.append(curdir)

import onetimepass as otp

__author__ = "Mark Embling <mark@markembling.info>"
__version__ = "0.1"

def _get_secret(input):
    '''Attempts to get the secret from the given string - either the whole
    string itself, or the secret from within an otpauth:// URI.
    '''
    input = input.strip()
    if input.startswith("otpauth://"):
        u = urlparse(input)
        q = parse_qs(u.query)
        return (q["secret"][0], u.hostname)
    return (input, None)


if __name__ == "__main__":
    parser = OptionParser()
    parser.description = "Prints the TOTP auth code for the given secret. Can be either the raw secret string or a otpauth:// URI; the script will attempt to auto-detect which is given."
    parser.usage = "%prog [options] secret OR %prog [options] < secret.txt"
    parser.epilog = "Copyright (c) Mark Embling 2013"

    parser.add_option("-t", "--type", dest="type", 
                      choices=["TOTP", "HOTP"], default="TOTP", 
                      help="Token type (HOTP or TOTP). If a URI is provided, the type will be determined from there. [default: %default]")
    parser.add_option("-c", "--count", dest="count", 
                      type="int", default=1,
                      help="Counter for HOTP [default: %default]")
    # parser.add_option("-d", "--digits", dest="digits", 
    #                   choices=['6','8'], default='6',
    #                   help="Number of digits to display (6 or 8) [default: %default]")

    (options, args) = parser.parse_args()

    # Get the secret/URI
    data_in = ""
    if sys.stdin.isatty():
        # Use argument
        ARGS_REQD = 1
        if len(args) != ARGS_REQD:
            parser.error("incorrect number of arguments (%d required, %d given)" % (ARGS_REQD, len(args)))
        data_in = args[0]
    else:
        # Read first line from stdin
        data_in = sys.stdin.readline()

    # Determine the type and secret
    (secret, type_) = _get_secret(data_in)
    if type_ is None:
        type_ = options.type

    # Get the token and print
    if type_.upper() == "HOTP":
        print(otp.get_hotp(secret, intervals_no=options.count))
    else:
        print(otp.get_totp(secret))

Read from stdin or args