Ejemplo n.º 1
0
class PersonalCap(object):
    def __init__(self, pc_username, pc_password, email_username,
                 email_password):
        self.client = PersonalCapitalClient()

        self.pc_username = pc_username
        self.pc_password = pc_password
        self.email_username = email_username
        self.email_password = email_password

        self.session = None

    def _login(self):
        if self.session:
            self.client.set_session(self.session)
            return
        else:
            try:
                self.client.login(username=self.pc_username,
                                  password=self.pc_password)
            except:
                self.client.two_factor_challenge(
                    TwoFactorVerificationModeEnum.EMAIL)

                ## get 2FA code magic via SMS or email tbd.
                code = self._get_code_from_email()

                self.client.two_factor_authenticate(
                    TwoFactorVerificationModeEnum.EMAIL, code)
                self.client.authenticate_password(self.pc_password)
            self.session = self.client.get_session()
            self.client.set_session(self.session)

    def _get_code_from_email(self):
        mailbox = imaplib.IMAP4_SSL("imap.gmail.com")
        mailbox.login(self.email_username, self.email_password)
        mailbox.select('inbox')
        result, data = mailbox.search(None, "ALL")
        mail_ids = data[0]  # data is a list.
        mail_ids_list = mail_ids.split()  # ids is a space separated string
        latest_email_id = mail_ids_list[-1]  # get the latest
        result, data = mailbox.fetch(
            latest_email_id,
            "(RFC822)")  # fetch the email body (RFC822) for the given ID
        raw_email = str(data[0][1])

        code = re.search(TWOFA_CODE_REGEX, raw_email)[1].split()[0]
        return code
Ejemplo n.º 2
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Set up the Personal Capital component."""
    from personalcapital import PersonalCapital, RequireTwoFactorException, TwoFactorVerificationModeEnum
    pc = PersonalCapital()
    session = load_session(hass)

    if len(session) > 0:
        pc.set_session(session)

        try:
            pc.login(config.get(CONF_EMAIL), config.get(CONF_PASSWORD))
            continue_setup_platform(hass, config, pc, add_devices, discovery_info)
        except RequireTwoFactorException:
            request_app_setup(hass, config, pc, add_devices, discovery_info)
    else:
        request_app_setup(hass, config, pc, add_devices, discovery_info)
Ejemplo n.º 3
0
from personalcapital import PersonalCapital, RequireTwoFactorException, TwoFactorVerificationModeEnum
import curses
from curses import wrapper
import json

_COOKIES_FILE = 'cookies.json'

pc = PersonalCapital()

email, password = "******", "password"
try:
    with open('cookies.json') as fd:
        cookies = json.load(fd)
        pc.set_session(cookies)
        pc.login(email, password)
except FileNotFoundError:
    try:
        pc.login(email, password)
    except RequireTwoFactorException:
        pc.two_factor_challenge(TwoFactorVerificationModeEnum.SMS)
        pc.two_factor_authenticate(TwoFactorVerificationModeEnum.SMS,
                                   input('code: '))
        pc.authenticate_password(password)
        cookies = pc.get_session()
        with open('cookies.json', 'w') as fd:
            json.dump(cookies, fd)

accounts_response = pc.fetch('/newaccount/getAccounts')
accounts = accounts_response.json()['spData']

print('Networth: {0}'.format(accounts['networth']))