First demo of JSON export for Amazon

This commit is contained in:
Lucas Verney 2016-09-28 15:38:55 -04:00
commit 0abbf06225
8 changed files with 138 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.pyc

2
TODO Normal file
View File

@ -0,0 +1,2 @@
* history (Detail) vs Bill?
* _url ?

0
capabilities/__init__.py Normal file
View File

14
capabilities/base.py Normal file
View File

@ -0,0 +1,14 @@
from weboob.capabilities.base import empty
def clean_object(o):
"""
Returns a JSON-serializable dict from a Weboob object.
"""
o = o.to_dict()
for k, v in o.items():
if empty(v):
# Replace empty values by None, avoid "NotLoaded is not
# serializable" error
o[k] = None
return o

21
capabilities/bill.py Normal file
View File

@ -0,0 +1,21 @@
from base import clean_object
def to_cozy(document):
"""
Export a CapDocument object to JSON, to pass it to Cozy instance.
"""
# Fetch the list of subscriptions
subscriptions = list(document.iter_subscription())
# Return a formatted dict with all the infos
return {
"subscriptions": [ # List of subscriptions
clean_object(subscription) for subscription in subscriptions
],
"bills": { # List of bills for each subscription
subscription.id: [
clean_object(bill) for bill in document.iter_bills(subscription)
]
for subscription in subscriptions
}
}

73
cozyweboob.py Executable file
View File

@ -0,0 +1,73 @@
#!/usr/bin/env python2
from __future__ import print_function
import getpass
from weboob.core import Weboob
from capabilities import bill
from tools.jsonwriter import pretty_json
class Connector(object):
"""
Connector is a tool that connects to common websites like bank website,
phone operator website... and that grabs personal data from there.
Credentials are required to make this operation.
Technically, connectors are weboob backend wrappers.
"""
@staticmethod
def version():
return Weboob.VERSION
def update(self):
return self.weboob.update()
def __init__(self, modulename, parameters):
"""
Create a Weboob handle and try to load the modules.
"""
self.weboob = Weboob()
# Careful: this is extracted from weboob's code.
# Install the module if necessary and hide the progress.
class DummyProgress:
def progress(self, a, b):
pass
repositories = self.weboob.repositories
minfo = repositories.get_module_info(modulename)
if minfo is not None and not minfo.is_installed():
repositories.install(minfo, progress=DummyProgress())
# Calls the backend.
self.backend = self.weboob.build_backend(modulename, parameters)
def main(email, password=None):
"""
Main code
"""
if password is None:
# Ask for password if not provided
password = getpass.getpass("Password? ")
connector = Connector(
"amazon",
{
"website": "www.amazon.fr",
"email": email,
"password": password
}
)
return bill.to_cozy(connector.backend)
if __name__ == '__main__':
print(
pretty_json(
main(raw_input("Email? "))
)
)

0
tools/__init__.py Normal file
View File

27
tools/jsonwriter.py Normal file
View File

@ -0,0 +1,27 @@
import json
from datetime import datetime
from decimal import Decimal
class CustomJSONEncoder(json.JSONEncoder):
"""
Custom JSONEncoder to support more types.
"""
def default(self, o):
if isinstance(o, datetime):
# Serialize datetime objects to ISO dates
return o.isoformat()
elif isinstance(o, Decimal):
# Serialize Decimal objects to string
return str(o)
return json.JSONEncoder.default(self, o)
def pretty_json(foo):
"""
Pretty printing of JSON output, using the custom JSONEncoder.
"""
return json.dumps(foo, sort_keys=True,
indent=4, separators=(',', ': '),
cls=CustomJSONEncoder)