Skip to content
module.rst 24.2 KiB
Newer Older
Romain Bignon's avatar
Romain Bignon committed
Write a new module
==================

This guide aims to learn how to write a new module for `Weboob <http://weboob.org>`_.

Before read it, you should :doc:`setup your development environment </guides/setup>`.

What is a module
****************

A module is an interface between a website and Weboob. It represents the python code which is stored
Romain Bignon's avatar
Romain Bignon committed
in repositories.

Weboob applications need *backends* to interact with websites. A *backend* is an instance of a *module*, usually
Romain Bignon's avatar
Romain Bignon committed
with several parameters like your username, password, or other options. You can create multiple *backends*
for a single *module*.

Select capabilities
*******************

Each module implements one or many :doc:`capabilities </api/capabilities/index>` to tell what kind of features the
Florent's avatar
Florent committed
website provides. A capability is a class derived from :class:`weboob.capabilities.base.Capability` and with some abstract
Romain Bignon's avatar
Romain Bignon committed
methods (which raise ``NotImplementedError``).

A capability needs to be as generic as possible to allow a maximum number of modules to implement it.
Romain Bignon's avatar
Romain Bignon committed
Anyway, if you really need to handle website specificities, you can create more specific sub-capabilities.

For example, there is the :class:`CapMessages <weboob.capabilities.messages.CapMessages>` capability, with the associated
:class:`CapMessagesPost <weboob.capabilities.messages.CapMessagesPost>` capability to allow answers to messages.
Romain Bignon's avatar
Romain Bignon committed

Pick an existing capability
---------------------------

When you want to create a new module, you may have a look at existing capabilities to decide which one can be
Romain Bignon's avatar
Romain Bignon committed
implemented. It is quite important, because each already existing capability is supported by at least one application.
So if your new module implements an existing capability, it will be usable from the existing applications right now.

Create a new capability
-----------------------

If the website you want to manage implements some extra-features which are not implemented by any capability,
you can introduce a new capability.

You should read the related guide to know :doc:`how to create a capability </guides/capability>`.

The module tree
***************

Create a new directory in ``modules/`` with the name of your module. In this example, we assume that we want to create a
module for a bank website which URL is http://www.example.com. So we will call our module **example**, and the selected
capability is :class:`CapBank <weboob.capabilities.bank.CapBank>`.
Romain Bignon's avatar
Romain Bignon committed

It is recommended to use the helper tool ``tools/boilerplate.py`` to build your
module tree. There are several templates available:
Romain Bignon's avatar
Romain Bignon committed

* **base** - create only base files
* **comic** - create a comic module
* **cap** - create a module for a given capability

For example, use this command::

    $ tools/boilerplate.py cap example CapBank
Romain Bignon's avatar
Romain Bignon committed

In a module directory, there are commonly these files:

* **__init__.py** - needed in every python modules, it exports your :class:`Module <weboob.tools.backend.Module>` class.
* **module.py** - defines the main class of your module, which derives :class:`Module <weboob.tools.backend.Module>`.
* **browser.py** - your browser, derived from :class:`Browser <weboob.browser.browsers.Browser>`, is called by your module to interact with the supported website.
Romain Bignon's avatar
Romain Bignon committed
* **pages.py** - all website's pages handled by the browser are defined here
* **test.py** - functional tests
* **favicon.png** - a 64x64 transparent PNG icon
Romain Bignon's avatar
Romain Bignon committed

.. note::

    A module can implement multiple capabilities, even though the ``tools/boilerplate.py`` script can only generate a
    template for a single capability. You can freely add inheritance from other capabilities afterwards in
    ``module.py``.

Romain Bignon's avatar
Romain Bignon committed
Update modules list
-------------------

As you are in development mode, to see your new module in ``weboob-config``'s list, you have to update ``modules/modules.list`` with this command::

Romain Bignon's avatar
Romain Bignon committed

To be sure your module is correctly added, use this command::

    $ weboob-config info example
    .------------------------------------------------------------------------------.
    | Module example                                                               |
    +-----------------.------------------------------------------------------------'
Romain Bignon's avatar
Romain Bignon committed
    | Maintainer      | John Smith <john.smith@example.com>
    | License         | LGPLv3+
    | Description     | Example bank website
    | Capabilities    | CapBank, CapCollection
Romain Bignon's avatar
Romain Bignon committed
    | Installed       | yes
    | Location        | /home/me/src/weboob/modules/example
    '-----------------'

If the last command does not work, check your :doc:`repositories setup
</guides/setup>`. In particular, when you want to edit an already existing
module, you should take great care of setting your development environment
correctly, or your changes to the module will not have any effect. You can also
use ``./tools/local_run.sh`` script as a quick and dirty method of forcing
Weboob applications to use local modules rather than remote ones.

Edit ``module.py``. It contains the main class of the module derived from :class:`Module <weboob.tools.backend.Module>` class::
    from weboob.tools.backend import Module
    from weboob.capabilities.bank import CapBank

    class ExampleModule(Module, CapBank):
        NAME = 'example'                         # The name of module
        DESCRIPTION = u'Example bank website'    # Description of your module
        MAINTAINER = u'John Smith'               # Name of maintainer of this module
        EMAIL = 'john.smith@example.com'         # Email address of the maintainer
        LICENSE = 'LGPLv3+'                      # License of your module
        # Version of weboob
Romain Bignon's avatar
Romain Bignon committed
        VERSION = '2.0'
In the code above, you can see that your ``ExampleModule`` inherits :class:`CapBank <weboob.capabilities.bank.CapBank>`, as
we have selected it for the supported website.

Romain Bignon's avatar
Romain Bignon committed
Configuration
-------------

When a module is instanced as a backend, you probably want to ask parameters to user. It is managed by the ``CONFIG`` class
Romain Bignon's avatar
Romain Bignon committed
attribute. It supports key/values with default values and some other parameters. The :class:`Value <weboob.tools.value.Value>`
class is used to define a value.

Available parameters of :class:`Value <weboob.tools.value.Value>` are:
Romain Bignon's avatar
Romain Bignon committed

* **label** - human readable description of a value
* **required** - if ``True``, the backend can't be loaded if the key isn't found in its configuration
Romain Bignon's avatar
Romain Bignon committed
* **default** - an optional default value, used when the key is not in config. If there is no default value and the key
  is not found in configuration, the **required** parameter is implicitly set
* **masked** - if ``True``, the value is masked. It is useful for applications to know if this key is a password
* **regexp** - if specified, the specified value is checked against this regexp upon loading, and an error is raised if
  it doesn't match
Romain Bignon's avatar
Romain Bignon committed
* **choices** - if this parameter is set, the value must be in the list

.. note::

    There is a special class, :class:`ValueBackendPassword <weboob.tools.value.ValueBackendPassword>`, which is used to manage
Adrien's avatar
Adrien committed
    private parameters of the config (like passwords or sensitive information).
Romain Bignon's avatar
Romain Bignon committed

.. note::

    Other classes are available to store specific types of configuration options. See :mod:`weboob.tools.value
    <weboob.tools.value>` for a full list of them.

Romain Bignon's avatar
Romain Bignon committed
For example::

    from weboob.tools.backend import Module, BackendConfig
    from weboob.capabilities.bank import CapBank
Romain Bignon's avatar
Romain Bignon committed
    from weboob.tools.value import Value, ValueBool, ValueInt, ValueBackendPassword

    # ...
    class ExampleModule(Module, CapBank):
Romain Bignon's avatar
Romain Bignon committed
        # ...
        CONFIG = BackendConfig(Value('username',                label='Username', regexp='.+'),
                               ValueBackendPassword('password', label='Password'),
                               ValueBool('get_news',            label='Get newspapers', default=True),
                               Value('choice',                  label='Choices', choices={'value1': 'Label 1',
                                                                                          'value2': 'Label 2'}, default='1'),
                               Value('regexp',                  label='Birthday', regexp='^\d+/\d+/\d+$'),
                               ValueInt('integer',              label='A number', required=True))


Implement capabilities
----------------------

You need to implement each method of all of the capabilities your module implements. For example, in our case::

    from weboob.tools.backend import Module
    from weboob.capabilities.bank import CapBank

Romain Bignon's avatar
Romain Bignon committed
    # ...
    class ExampleModule(Module, CapBank):
Romain Bignon's avatar
Romain Bignon committed
        # ...

Romain Bignon's avatar
Romain Bignon committed
            raise NotImplementedError()

        def get_account(self, id):
Romain Bignon's avatar
Romain Bignon committed
            raise NotImplementedError()

        def iter_history(self, account):
Romain Bignon's avatar
Romain Bignon committed
            raise NotImplementedError()

        def iter_coming(self, account):
Romain Bignon's avatar
Romain Bignon committed
            raise NotImplementedError()

Florent's avatar
Florent committed
If you ran the ``boilerplate`` script command ``cap``, every methods are already in ``module.py`` and documented.

Read :class:`documentation of the capability <weboob.capabilities.bank.CapBank>` to know what are types of arguments,
what are expected returned objects, and what exceptions it may raise.

When you are done writing your module, you should remove all the not implemented methods from your module, as the base
capability code will anyway ``raise NotImplementedError()``.
Romain Bignon's avatar
Romain Bignon committed


Browser
*******

Most of modules use a class derived from :class:`PagesBrowser <weboob.browser.browsers.PagesBrowser>` or
:class:`LoginBrowser <weboob.browser.browsers.LoginBrowser>` (for authenticated websites) to interact with a website or
:class:`APIBrowser <weboob.browser.browsers.APIBrowser>` to interact with an API.
Romain Bignon's avatar
Romain Bignon committed

Romain Bignon's avatar
Romain Bignon committed

    # -*- coding: utf-8 -*-

    from weboob.browser import PagesBrowser
Romain Bignon's avatar
Romain Bignon committed

    __all__ = ['ExampleBrowser']

    class ExampleBrowser(PagesBrowser):
        BASEURL = 'https://www.example.com'
Romain Bignon's avatar
Romain Bignon committed

There are several possible class attributes:
Romain Bignon's avatar
Romain Bignon committed

* **BASEURL** - base url of website used for absolute paths given to :class:`open() <weboob.browser.browsers.PagesBrowser.open>` or :class:`location() <weboob.browser.browsers.PagesBrowser.location>`
* **PROFILE** - defines the behavior of your browser against the website. By default this is Firefox, but you can import other profiles
* **TIMEOUT** - defines the timeout for requests (defaults to 10 seconds)
* **VERIFY** - SSL verification (if the protocol used is **https**)
Romain Bignon's avatar
Romain Bignon committed

Pages
-----

For each page you want to handle, you have to create an associated class derived from one of these classes:

* :class:`HTMLPage <weboob.browser.pages.HTMLPage>` - a HTML page
* :class:`XMLPage <weboob.browser.pages.XMLPage>` - a XML document
* :class:`JsonPage <weboob.browser.pages.JsonPage>` - a Json object
* :class:`CsvPage <weboob.browser.pages.CsvPage>` - a CSV table
Romain Bignon's avatar
Romain Bignon committed

In the file ``pages.py``, you can write, for example::
Romain Bignon's avatar
Romain Bignon committed

    # -*- coding: utf-8 -*-

    from weboob.browser.pages import HTMLPage
Romain Bignon's avatar
Romain Bignon committed

    __all__ = ['IndexPage', 'ListPage']

    class IndexPage(HTMLPage):
Romain Bignon's avatar
Romain Bignon committed
        pass

    class ListPage(HTMLPage):
        def iter_accounts():
Romain Bignon's avatar
Romain Bignon committed
            return iter([])

``IndexPage`` is the class we will use to get information from the home page of the website, and ``ListPage`` will handle pages
Romain Bignon's avatar
Romain Bignon committed

Then, you have to declare them in your browser, with the :class:`URL <weboob.browser.url.URL>` object::
    from weboob.browser import PagesBrowser, URL
Romain Bignon's avatar
Romain Bignon committed
    from .pages import IndexPage, ListPage

    # ...
    class ExampleBrowser(PagesBrowser):
Romain Bignon's avatar
Romain Bignon committed
        # ...

        home = URL('/$', IndexPage)
        accounts = URL('/accounts$', ListPage)

Easy, isn't it? The first parameters are regexps of the urls (if you give only a path, it uses the ``BASEURL`` class attribute), and the last one is the class used to handle the response.
Romain Bignon's avatar
Romain Bignon committed

.. note::

    You can handle parameters in the URL using ``(?P<someName>)``. You can then use a keyword argument `someName` to
    bind a value to this parameter in :func:`stay_or_go() <weboob.browser.url.URL.stay_or_go>`.

Each time you will go on the home page, ``IndexPage`` will be instanced and set as the ``page`` attribute.
Romain Bignon's avatar
Romain Bignon committed

For example, we can now implement some methods in ``ExampleBrowser``::
Romain Bignon's avatar
Romain Bignon committed

    from weboob.browser import PagesBrowser

    class ExampleBrowser(PagesBrowser):
Romain Bignon's avatar
Romain Bignon committed
        # ...
        def go_home(self):
            self.home.go()

            assert self.home.is_here()
Romain Bignon's avatar
Romain Bignon committed

        def iter_accounts_list(self):
            self.accounts.stay_or_go()
Romain Bignon's avatar
Romain Bignon committed

            return self.page.iter_accounts()
Romain Bignon's avatar
Romain Bignon committed

When calling the :func:`go() <weboob.browser.url.URL.go>` method, it reads the first regexp url of our :class:`URL <weboob.browser.url.URL>` object, and go on the page.
Romain Bignon's avatar
Romain Bignon committed

:func:`stay_or_go() <weboob.browser.url.URL.stay_or_go>` is used when you want to relocate on the page only if we aren't already on it.

Once we are on the ``ListPage``, we can call every methods of the ``page`` object.
Romain Bignon's avatar
Romain Bignon committed

Use it in backend
-----------------

Now you have a functional browser, you can use it in your class ``ExampleModule`` by defining it with the ``BROWSER`` attribute::
Romain Bignon's avatar
Romain Bignon committed

    from weboob.tools.backend import Module
    from weboob.capabilities.bank import CapBank

Romain Bignon's avatar
Romain Bignon committed
    from .browser import ExampleBrowser

    # ...
    class ExampleModule(Module, CapBank):
Romain Bignon's avatar
Romain Bignon committed
        # ...
        BROWSER = ExampleBrowser

You can now access it with member ``browser``. The class is instanced at the first call to this attribute.
Romain Bignon's avatar
Romain Bignon committed

For example, we can now implement :func:`CapBank.iter_accounts <weboob.capabilities.bank.CapBank.iter_accounts>`::
Romain Bignon's avatar
Romain Bignon committed

    def iter_accounts(self):
        return self.browser.iter_accounts_list()
Romain Bignon's avatar
Romain Bignon committed

For this method, we only call immediately ``ExampleBrowser.iter_accounts_list``, as there isn't anything else to do around.
Romain Bignon's avatar
Romain Bignon committed

Login management
----------------

When the website requires to be authenticated, you have to give credentials to the constructor of the browser. You can redefine
the method :func:`create_default_browser <weboob.tools.backend.Module.create_default_browser>`::
Romain Bignon's avatar
Romain Bignon committed

    from weboob.tools.backend import Module
    from weboob.capabilities.bank import CapBank

    class ExampleModule(Module, CapBank):
Romain Bignon's avatar
Romain Bignon committed
        # ...
        def create_default_browser(self):
            return self.create_browser(self.config['username'].get(), self.config['password'].get())

On the browser side, you need to inherit from :func:`LoginBrowser <weboob.browser.browsers.LoginBrowser>` and to implement the function
:func:`do_login <weboob.browser.browsers.LoginBrowser.do_login>`::
Romain Bignon's avatar
Romain Bignon committed

    from weboob.browser import LoginBrowser
    from weboob.exceptions import BrowserIncorrectPassword

    class ExampleBrowser(LoginBrowser):
        login = URL('/login', LoginPage)
Romain Bignon's avatar
Romain Bignon committed
        # ...

        def do_login(self):
            self.login.stay_or_go()
Romain Bignon's avatar
Romain Bignon committed

            self.page.login(self.username, self.password)

            if self.login_error.is_here():
                raise BrowserIncorrectPassword(self.page.get_error())
Romain Bignon's avatar
Romain Bignon committed

You may provide a custom :func:`do_logout <weboob.browser.browsers.LoginBrowser.do_logout>`:: function if you need to customize the default logout process, which simply clears all cookies.

Also, your ``LoginPage`` may look like::
Romain Bignon's avatar
Romain Bignon committed

    from weboob.browser.pages import HTMLPage

    class LoginPage(HTMLPage):
        def login(self, username, password):
            form = self.get_form(name='auth')
            form['username'] = username
            form['password'] = password
            form.submit()
Romain Bignon's avatar
Romain Bignon committed

Then, each method on your browser which needs your user to be authenticated may be decorated by :func:`need_login <weboob.browser.browsers.need_login>`::
Romain Bignon's avatar
Romain Bignon committed

    from weboob.browser import LoginBrowser, URL
    from weboob.browser import need_login

    class ExampleBrowser(LoginBrowser):
        accounts = URL('/accounts$', ListPage)
Romain Bignon's avatar
Romain Bignon committed

        @need_login
        def iter_accounts(self):
            self.accounts.stay_or_go()
            return self.page.get_accounts()
Romain Bignon's avatar
Romain Bignon committed

You finally have to set correctly the :func:`logged <weboob.browser.pages.Page.logged>` attribute of each page you use.  The
:func:`need_login <weboob.browser.browsers.need_login>` decorator checks if the current page is a logged one by reading the attribute
:func:`logged <weboob.browser.pages.Page.logged>` of the instance. This attributes defaults to  ``False``, which means that :func:`need_login
<weboob.browser.browsers.need_login>` will first call :func:`do_login <weboob.browser.browsers.LoginBrowser.do_login>` before calling the
decorated method.

You can either define it yourself, as a class boolean attribute or as a property, or inherit your class from :class:`LoggedPage <weboob.browser.pages.LoggedPage>`.
In the latter case, remember that Python inheritance requires the :class:`LoggedPage <weboob.browser.pages.LoggedPage>` to be placed first such as in::
    from weboob.browser.pages import LoggedPage, HTMLPage

    class OnlyForLoggedUserPage(LoggedPage, HTMLPage):
        # ...
Romain Bignon's avatar
Romain Bignon committed


Parsing of pages
****************
Romain Bignon's avatar
Romain Bignon committed

    Depending of the base class you use for your page, it will parse html, json, csv, etc. In this section, we will
    describe the case of HTML documents.
Romain Bignon's avatar
Romain Bignon committed


When your browser locates on a page, an instance of the class related to the
:class:`URL <weboob.browser.url.URL>` attribute which matches the url
is created. You can declare methods on your class to allow your browser to
interact with it.
Romain Bignon's avatar
Romain Bignon committed

The first thing to know is that page parsing is done in a descriptive way. You
don't have to loop on HTML elements to construct the object. Just describe how
to get correct data to construct it. It is the ``Browser`` class work to actually
construct the object.
Romain Bignon's avatar
Romain Bignon committed

For example::

    from weboob.browser.pages import LoggedPage, HTMLPage
    from weboob.browser.filters.html import Attr
    from weboob.browser.filters.standard import CleanDecimal, CleanText
    from weboob.capabilities.bank import Account
    from weboob.browser.elements import method, ListElement, ItemElement

    class ListPage(LoggedPage, HTMLPage):
        @method
        class get_accounts(ListElement):
            item_xpath = '//ul[@id="list"]/li'
Romain Bignon's avatar
Romain Bignon committed

            class item(ItemElement):
Romain Bignon's avatar
Romain Bignon committed

                obj_id = Attr('id')
                obj_label = CleanText('./td[@class="name"]')
                obj_balance = CleanDecimal('./td[@class="balance"]')
As you can see, we first set ``item_xpath`` which is the xpath string used to iterate over elements to access data. In a
second time we define ``klass`` which is the real class of our object. And then we describe how to fill each object's
attribute using what we call filters. To set an attribute `foobar` of the object, we should fill `obj_foobar`. It can
either be a filter, a constant or a function.

Some example of filters:

* :class:`Attr <weboob.browser.filters.html.Attr>`: extract a tag attribute
* :class:`CleanText <weboob.browser.filters.standard.CleanText>`: get a cleaned text from an element
* :class:`CleanDecimal <weboob.browser.filters.standard.CleanDecimal>`: get a cleaned Decimal value from an element
* :class:`Date <weboob.browser.filters.standard.Date>`: read common date formats
* :class:`DateTime <weboob.browser.filters.standard.Date>`: read common datetime formats
* :class:`Env <weboob.browser.filters.standard.Env>`: typically useful to get a named parameter in the URL (passed as a
  keyword argument to :func:`stay_or_go() <weboob.browser.url.URL.stay_or_go>`)
* :class:`Eval <weboob.browser.filters.standard.Eval>`: evaluate a lambda on the given value
* :class:`Format <weboob.browser.filters.standard.Format>`: a formatting filter, uses the standard Python format string
  notations.
* :class:`Link <weboob.browser.filters.html.Link>`: get the link uri of an element
* :class:`Regexp <weboob.browser.filters.standard.Regexp>`: apply a regex
* :class:`Time <weboob.browser.filters.standard.Time>`: read common time formats
* :class:`Type <weboob.browser.filters.standard.Type>`: get a cleaned value of any type from an element text

The full list of filters can be found in :doc:`weboob.browser.filters </api/browser/filters/index>`.

Filters can be combined. For example::

    obj_id = Link('./a[1]') & Regexp(r'id=(\d+)') & Type(type=int)

This code do several things, in order:

#) extract the href attribute of our item first ``a`` tag child
#) apply a regex to extract a value
#) convert this value to int type
Romain Bignon's avatar
Romain Bignon committed


When you want to access some attributes of your :class:`HTMLPage <weboob.browser.pages.HTMLPage>` object to fill an
attribute in a Filter, you should use the function construction for this attribute. For example::

	def obj_url(self):
		return (
			u'%s%s' % (
				self.page.browser.BASEURL,
				Link(
					u'//a[1]'
				)(self)
			)
	)

which will return a full URL, concatenating the ``BASEURL`` from the browser
with the (relative) link uri of the first ``a`` tag child.

Romain Bignon's avatar
Romain Bignon committed
.. note::

   All objects ID must be unique, and useful to get more information later

Your module is now functional and you can use this command::

    $ boobank -b example list
Romain Bignon's avatar
Romain Bignon committed

.. note::

	You can pass ``-a`` command-line argument to any Weboob application to log
	all the possible debug output (including requests and their parameters, raw
	responses and loaded HTML pages) in a temporary directory, indicated at the
	launch of the program.

Romain Bignon's avatar
Romain Bignon committed
Tests
*****

Every modules must have a tests suite to detect when there are changes on websites, or when a commit
breaks the behavior of the module.

Edit ``test.py`` and write, for example::
Romain Bignon's avatar
Romain Bignon committed

    # -*- coding: utf-8 -*-
    from weboob.tools.test import BackendTest

    __all__ = ['ExampleTest']
Romain Bignon's avatar
Romain Bignon committed

    class ExampleTest(BackendTest):
Florent's avatar
Florent committed
        MODULE = 'example'
Romain Bignon's avatar
Romain Bignon committed

        def test_iter_accounts(self):
            accounts = list(self.backend.iter_accounts())
Romain Bignon's avatar
Romain Bignon committed

            self.assertTrue(len(accounts) > 0)
Romain Bignon's avatar
Romain Bignon committed

To try running test of your module, launch::

    $ tools/run_tests.sh example

Laurent Bachelier's avatar
Laurent Bachelier committed
For more information, look at the :doc:`tests` guides.
Romain Bignon's avatar
Romain Bignon committed
Advanced topics
***************

Filling objects
---------------

.. note::

    Filling objects using ``fillobj`` should be used whenever you need to fill some fields automatically based on data
    fetched from the scraping. If you only want to fill some fields automatically based on some static data, you should
    just inherit the base object class and set these fields.

Romain Bignon's avatar
Romain Bignon committed
An object returned by a method of a capability can be not fully completed.

The class :class:`Module <weboob.tools.backend.Module>` provides a method named
:func:`fillobj <weboob.tools.backend.Module.fillobj>`, which can be called by an application to
Romain Bignon's avatar
Romain Bignon committed
fill some unloaded fields of a specific object, for example with::

    backend.fillobj(video, ['url', 'author'])

The ``fillobj`` method will check on the object which fields (in the ones given in the list argument) are not loaded
(equal to ``NotLoaded``, which is the default value), to reduce the list to the real uncompleted fields, and call the
method associated to the type of the object.
Romain Bignon's avatar
Romain Bignon committed

To define what objects are supported to be filled, and what method to call, define the ``OBJECTS``
class attribute in your ``ExampleModule``::
Romain Bignon's avatar
Romain Bignon committed

    from weboob.tools.backend import Module
    from weboob.capabilities.video import CapVideo

    class ExampleModule(Module, CapVideo):
        # ...

        OBJECTS = {Video: fill_video}
Romain Bignon's avatar
Romain Bignon committed

The prototype of the function might be::

Romain Bignon's avatar
Romain Bignon committed

Then, the function might, for each requested fields, fetch the right data and fill the object. For example::

    from weboob.tools.backend import Module
    from weboob.capabilities.video import CapVideo

    class ExampleModule(Module, CapVideo):
Romain Bignon's avatar
Romain Bignon committed

        def fill_video(self, video, fields):
            if 'url' in fields:
                return self.backend.get_video(video.id)
Romain Bignon's avatar
Romain Bignon committed

Romain Bignon's avatar
Romain Bignon committed

Here, when the application has got a :class:`Video <weboob.capabilities.video.BaseVideo>` object with
:func:`search_videos <weboob.capabilities.video.CapVideo.search_videos>`, in most cases, there are only some meta-data, but not the direct link to the video media.
Romain Bignon's avatar
Romain Bignon committed

As our method :func:`get_video <weboob.capabilities.video.CapVideo.get_video>` will get all
Laurent Bachelier's avatar
Laurent Bachelier committed
of the missing data, we just call it with the object as parameter to complete it.
Florent's avatar
Florent committed


Storage
-------

The application can provide a storage to let your backend store data. So, you can define the structure of your storage space::

    STORAGE = {'seen': {}}

To store and read data in your storage space, use the ``storage`` attribute of your :class:`Module <weboob.tools.backend.Module>`
Florent's avatar
Florent committed
object.

It implements the methods of :class:`BackendStorage <weboob.tools.backend.BackendStorage>`.