<commit_before>#!/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__ = "<NAME> <<EMAIL>>"
__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) <NAME> 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))<commit_msg>Read from stdin or args<commit_after>#!/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__ = "<NAME> <<EMAIL>>"
__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) <NAME> 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))
{'fraction_non_alphanumeric': 0.13535791757049892, 'fraction_numerical': 0.006507592190889371, 'mean_word_length': 2.700642054574639, 'pattern_counts': {'":': 4, '<': 12, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1381980', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1444, 'n_words': 441}