<commit_before>from conf import config
from util import *
import settings
import base64

qpat = re.compile(r'\?')

if settings.DEBUG:
    import logging
    logging.basicConfig()
    log = logging.getLogger('PyGoogleVoice')
    log.setLevel(logging.DEBUG)
else:
    log = None


class Voice(object):
    """
    Main voice instance for interacting with the Google Voice service
    Handles login/logout and most of the baser HTTP methods
    """
    def __init__(self):
        install_opener(build_opener(HTTPCookieProcessor(CookieJar())))

        for name in settings.FEEDS:
            setattr(self, name, self.__get_xml_page(name))

        setattr(self, 'message', self.__get_xml_page('message'))

    ######################
    # Some handy methods
    ######################
    def special(self):
        """
        Returns special identifier for your session (if logged in)
        """
        if hasattr(self, '_special') and getattr(self, '_special'):
            return self._special
        try:
            try:
                regex = bytes("('_rnr_se':) '(.+)'", 'utf8')
            except TypeError:
                regex = bytes("('_rnr_se':) '(.+)'")
        except NameError:
            regex = r"('_rnr_se':) '(.+)'"
        try:
            sp = re.search(regex, urlopen(settings.INBOX).read()).group(2)
        except AttributeError:
            sp = None
        self._special = sp
        return sp
    special = property(special)

    def login(self, email=None, passwd=None, smsKey=None):
        """
        Login to the service using your Google Voice account
        Credentials will be propmpted for if not given as args or in the ``~/.gvoice`` config file
        """
        if hasattr(self, '_special') and getattr(self, '_special'):
            return self

        if email is None:
            email = config.email
        if email is None:
            email = input('Email address: ')

        if passwd is None:
            passwd = config.password
        if passwd is None:
            from getpass import getpass
            passwd = getpass()

        content = self.__do_page('login').read()
        # holy hackjob
        galx = re.search(r"name=\"GALX\"\s+type=\"hidden\"\s+value=\"(.+)\"", content).group(1)
        result = self.__do_page('login', {'Email': email, 'Passwd': <PASSWORD>, 'GALX': galx})

        if result.geturl().startswith(getattr(settings, "SMSAUTH")):
            content = self.__smsAuth(smsKey)

            try:
                smsToken = re.search(r"name=\"smsToken\"\s+value=\"([^\"]+)\"", content).group(1)
                galx = re.search(r"name=\"GALX\"\s+value=\"([^\"]+)\"", content).group(1)
                content = self.__do_page('login', {'smsToken': smsToken, 'service': "grandcentral", 'GALX': galx})
            except AttributeError:
                raise LoginError

            del smsKey, smsToken, galx

        del email, passwd

        try:
            assert self.special
        except (AssertionError, AttributeError):
            raise LoginError

        return self

    def __smsAuth(self, smsKey=None):
        if smsKey is None:
            smsKey = config.smsKey

        if smsKey is None:
            from getpass import getpass
            smsPin = getpass("SMS PIN: ")
            content = self.__do_page('smsauth', {'smsUserPin': smsPin}).read()

        else:
            smsKey = base64.b32decode(re.sub(r' ', '', smsKey), casefold=True).encode("hex")
            content = self.__oathtoolAuth(smsKey)

            try_count = 1

            while "The code you entered didn&#39;t verify." in content and try_count < 5:
                sleep_seconds = 10
                try_count += 1
                print 'invalid code, retrying after {0} seconds (attempt {1})'.format(sleep_seconds, try_count)
                import time
                time.sleep(sleep_seconds)
                content = self.__oathtoolAuth(smsKey)

        del smsKey<commit_msg>Updated voice.py to use % for formatting, compiler errors otherwise<commit_after>from conf import config
from util import *
import settings
import base64

qpat = re.compile(r'\?')

if settings.DEBUG:
    import logging
    logging.basicConfig()
    log = logging.getLogger('PyGoogleVoice')
    log.setLevel(logging.DEBUG)
else:
    log = None


class Voice(object):
    """
    Main voice instance for interacting with the Google Voice service
    Handles login/logout and most of the baser HTTP methods
    """
    def __init__(self):
        install_opener(build_opener(HTTPCookieProcessor(CookieJar())))

        for name in settings.FEEDS:
            setattr(self, name, self.__get_xml_page(name))

        setattr(self, 'message', self.__get_xml_page('message'))

    ######################
    # Some handy methods
    ######################
    def special(self):
        """
        Returns special identifier for your session (if logged in)
        """
        if hasattr(self, '_special') and getattr(self, '_special'):
            return self._special
        try:
            try:
                regex = bytes("('_rnr_se':) '(.+)'", 'utf8')
            except TypeError:
                regex = bytes("('_rnr_se':) '(.+)'")
        except NameError:
            regex = r"('_rnr_se':) '(.+)'"
        try:
            sp = re.search(regex, urlopen(settings.INBOX).read()).group(2)
        except AttributeError:
            sp = None
        self._special = sp
        return sp
    special = property(special)

    def login(self, email=None, passwd=None, smsKey=None):
        """
        Login to the service using your Google Voice account
        Credentials will be propmpted for if not given as args or in the ``~/.gvoice`` config file
        """
        if hasattr(self, '_special') and getattr(self, '_special'):
            return self

        if email is None:
            email = config.email
        if email is None:
            email = input('Email address: ')

        if passwd is None:
            passwd = config.password
        if passwd is None:
            from getpass import getpass
            passwd = getpass()

        content = self.__do_page('login').read()
        # holy hackjob
        galx = re.search(r"name=\"GALX\"\s+type=\"hidden\"\s+value=\"(.+)\"", content).group(1)
        result = self.__do_page('login', {'Email': email, 'Passwd': <PASSWORD>, 'GALX': galx})

        if result.geturl().startswith(getattr(settings, "SMSAUTH")):
            content = self.__smsAuth(smsKey)

            try:
                smsToken = re.search(r"name=\"smsToken\"\s+value=\"([^\"]+)\"", content).group(1)
                galx = re.search(r"name=\"GALX\"\s+value=\"([^\"]+)\"", content).group(1)
                content = self.__do_page('login', {'smsToken': smsToken, 'service': "grandcentral", 'GALX': galx})
            except AttributeError:
                raise LoginError

            del smsKey, smsToken, galx

        del email, passwd

        try:
            assert self.special
        except (AssertionError, AttributeError):
            raise LoginError

        return self

    def __smsAuth(self, smsKey=None):
        if smsKey is None:
            smsKey = config.smsKey

        if smsKey is None:
            from getpass import getpass
            smsPin = getpass("SMS PIN: ")
            content = self.__do_page('smsauth', {'smsUserPin': smsPin}).read()

        else:
            smsKey = base64.b32decode(re.sub(r' ', '', smsKey), casefold=True).encode("hex")
            content = self.__oathtoolAuth(smsKey)

            try_count = 1

            while "The code you entered didn&#39;t verify." in content and try_count < 5:
                sleep_seconds = 10
                try_count += 1
                print('invalid code, retrying after %s seconds (attempt %s)' % sleep_seconds, try_count)
                import time
                time.sleep(sleep_seconds)
                content = self.__oathtoolAuth(smsKey)

        del smsKey
{'fraction_non_alphanumeric': 0.13140423399724416, 'fraction_numerical': 0.004760115244895403, 'mean_word_length': 2.0554917719096824, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '4078753', 'n_tokens_mistral': 2387, 'n_tokens_neox': 2265, 'n_words': 599}