PyBry is a Python 3 wrapper for the lbrynet and lbrycrd daemons from the LBRY project. It allows calling the methods of these daemons inside Python programs.
You must have the LBRY Desktop application or the lbrynet
client.
Get them from lbry.com/get.
The lbrycrd
blockchain can be downloaded from the
lbrycrd repository.
Clone the repository into a user-writeable directory:
git clone https://github.com/osilkin98/PyBRY
Change into the newly created repository, and run the Makefile
:
cd PyBRY/
make
The generated API wrapper will be composed of the newly created pybry/
directory, and the files contained in there.
The entire directory is a Python package which can be installed
in the Python path.
By default the lbrynet
API wrapper is created from the files
in the template/
directory, and the docs/api.json
file.
These are equivalent:
make
make build_local
To create the latest wrapper you must use the latest api.json
directly
from the lbry-sdk
repository. This can be done with a single instruction:
make build_online
However, if the online JSON file has errors, the generated wrapper
may also contain syntax errors, and it will not work
when used in a Python program.
In this case, clean the generated API, and use the local api.json
that is guaranteed to work:
make clean
make
Read the docs/README.md file for more information.
In contrast to the lbrynet
wrapper, the lbrycrd
API wrapper
is not created from any JSON file, as at the moment (2021)
there is no JSON document describing all methods from lbrycrd
.
This wrapper is mostly a copy from its template in the template/
directory.
To use the new wrapper code, copy the pybry/
directory,
and place it inside a site-packages
directory that is searched by Python.
This can be in the user's home directory,
/home/user/.local/lib/python3.8/site-packages/pybry
or in a system-wide directory:
/usr/local/lib/python3.8/dist-packages/pybry
/usr/lib/python3/dist-packages/pybry
You can also modify the PYTHONPATH
environmental variable
to include the parent directory where pybry
is located:
PYTHONPATH=/opt/git/PyBRY:$PYTHONPATH
Instead of using the Makefile
, we can use setuptools
as well:
python3 setup.py build_local
python3 setup.py build_online
python3 setup.py clean
python3 setup.py clean --all
Normally setuptools
will create a build/
directory where a copy
of the package will be placed.
There is a pybry
package available in PyPI.
However, this corresponds to the 1.6.4 version, and thus it is not up to date.
pip install --user pybry
Make sure the lbrynet
daemon is running either by launching
the full LBRY Desktop application, or by starting the console lbrynet
program.
lbrynet start
The wrapper generates all functions from the lbrynet
documentation,
and produces documented Python code.
Import the library, initialize the main class, and then call its methods, which have the same name and arguments as described in the documentation.
import pybry
lbry = pybry.LbrydApi()
response1 = lbry.claim_search(name="LBRYPlaylists")
response2 = lbry.support_list()
response3 = lbry.file_list(sort="claim_name", reverse=True)
Since the code is properly documented, if you ask for its documentation in an integrated development environment (IDE), or if you go to read it for yourself, it'll appear like this:
response = lbry.account_balance()
Return the balance of an account
Params:
account_id – If provided only the balance for this account will be given (Optional)
address – If provided only the balance for this address will be given (Optional)
include_unconfirmed – Include unconfirmed (Optional)
Returns:
(decimal) amount of lbry credits in wallet(decimal) amount of lbry credits in wallet
Note that at the moment the return information is not available, as the API JSON files don't contain this information in a standardized way.
All the wrapper code does is make requests to the running lbrynet
daemon.
Therefore, if there is no proper wrapper for a particular method
(because it was generated by an outdated api.json
, for example),
a simple message can be sent to the daemon just like with using
the requests
library or curl
.
method = "claim_search"
message = {"name": "LBRYPlaylists"}
response = lbry.call(method, message)
Initialize the daemon with a username and password
and send messages just like with using the requests
library.
import pybry
lbrycrd = pybry.LbrycrdApi("username", "password")
method = "wallet_unlock"
message = {"wallet_username", "wallet_password"}
response = lbrycrd.call(method, message)