Metadata-Version: 2.1
Name: pocketsmith-web-client
Version: 0.1.0
Summary: Pocketsmith web/realtime client, for things the API does not provide
Home-page: https://github.com/theY4Kman/python-pocketsmith-api
License: MIT
Author: Zach "theY4Kman" Kanzler
Author-email: they4kman@gmail.com
Requires-Python: >=3.6.1
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Financial and Insurance Industry
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Topic :: Internet :: WWW/HTTP
Requires-Dist: aiostream (>=0.4.1,<0.5.0)
Requires-Dist: beautifulsoup4 (>=4.7.1,<5.0.0)
Requires-Dist: httpx (>=0.15.5,<0.16.0)
Requires-Dist: lifter (>=0.4.1,<0.5.0)
Requires-Dist: pyotp (>=2.3.0,<3.0.0)
Requires-Dist: websockets (>=8.1,<9.0)
Description-Content-Type: text/markdown

# pocketsmith-web-client

A web-based client for Pocketsmith, which adds support for a few things missing from the API:

 - Searching transactions
 - Syncing institutions, including those requiring MFA!
 - Real-time events through [Pusher](https://pusher.com/) (just like the web UI)


# Installation

```bash
pip install pocketsmith-web-client
```


# Usage

```python
import asyncio
from pocketsmith_web import PocketsmithWebClient

pwc = PocketsmithWebClient(
    username='hambob',
    password='Myspace123',
    # If 2fa is enabled on the account — NOTE: this is the KEY, not a one-time code!
    totp_key='81r0dq0815u88qi2',
)

async def main():
    # Check login — NOTE: API methods requiring auth will automatically call this
    await pwc.login()

    # Search for some transactions and print them out
    async for transaction in pwc.search_transactions('Merchant, inc'):
        print(f'[{transaction["id"]:>8}] {transaction["date"]:%Y-%m-%d} ${transaction["amount"]:.2f}')

    # Sync some institutions
    # NOTE: these parameters can be scraped from the Account Summary page, 
    #       in URLs of the format: "/feeds/user_institutions/<uys_id>/refresh?item_ids%5B%5D=<item_id>
    await pwc.sync_institution(162303, 91821548)

asyncio.run(main())
```

If you have an institution requiring MFA info, the Pusher client can be used to provide this info when requested. It's up to you to figure out how to acquire the MFA info, though — whether it's from user input, a generated TOTP, a text message, email, etc.

```python
import asyncio
import json
from pocketsmith_web import PocketsmithWebClient, PusherEvent

pwc = PocketsmithWebClient('hambob', 'Myspace123', totp_key='81r0dq0815u88qi2')


async def sync_my_mfa_bank():
    uys_id = 162303
    item_id = 91821548

    await pwc.sync_institution(uys_id, item_id)

    async with pwc.pusher() as pusher:
        # Wait for an MFA event for our bank
        await pusher.events.expect(
            PusherEvent.MfaChanged(pwc.pusher_channel),
            matches_uys_item(uys_id, item_id),
        )

        # Grab the MFA popup form details
        mfa_req = await pwc.get_mfa_form_info()

        # Ask the user for the MFA deets, please
        print(f'MFA deets required: {mfa_req["label"]}')
        token = input('Token: ')

        # Now shoot the token back to Pocketsmith
        await pwc.provide_feed_mfa(uys_id, item_id, token)

def matches_uys_item(uys_id, item_id):
    uys_id = str(uys_id)
    item_id = str(item_id)

    def does_event_match_uys_item(event: PusherEvent):
        if not isinstance(event.data, dict):
            return False

        event_uys_id = event.data.get('user_yodlee_site_id')

        event_items = event.data.get('new_mfa_items', ())
        if isinstance(event_items, str):
            try:
                event_items = json.loads(event_items)
            except (TypeError, ValueError):
                pass

        return uys_id == event_uys_id and item_id in event_items

    return does_event_match_uys_item


asyncio.run(sync_my_mfa_bank())
```

