Skip to main content

Litres API

python-обёртка вокруг API ЛитРес на базе requests и xmltodict

https://img.shields.io/badge/python-3.6,%203.7,%203.8-blue.svg https://travis-ci.org/MyBook/litresapi.svg?branch=master https://codecov.io/gh/MyBook/litresapi/branch/master/graph/badge.svg Docs version

Официальная документация XML API

Примеры

Получить обновления книг

from litresapi import LitresApi

api = LitresApi(secret_key='your-secret-key', partner_id='ZZZZ')
lazy_books = api.get_fresh_book(start_date=datetime.datetime(2015, 7, 19, 12, 5))

>>> first_book = next(lazy_books)
>>> print(json.dumps(first_book, indent=4, ensure_ascii=False))
{
    "title-info": {
        "genre": "detective",
        "author": {
            "first-name": "Евгений",
            "middle-name": "Евгеньевич",
            "last-name": "Сухов",
            "id": "1212f327-2a83-102a-9ae1-2dfe723fe7c7"
        },
        ...
    }
    ...
    "@external_id": "37828892-1a76-11e5-ad6a-002590591dd6",
    "@tag": "updated-book"
    ...
}

Вместо словарей можно работать с xml напрямую (lxml):

api = LitresApi(secret_key='your-secret-key, partner_id='ZZZZ', xml=True)
lazy_books = api.get_fresh_book(start_date=datetime.datetime(2015, 7, 19, 12, 5))

>>> book = next(lazy_books)
<Element updated-book at 0x1067186c8>
>>> book.attrib['external_id']
'37828892-1a76-11e5-ad6a-002590591dd6'
>>> book.getchildren()
[<Element files at 0x10a77cd88>, <Element title-info at 0x10a77c488>, ...]

Можно предавать аргументы для трансформации ответа на базе requests hooks:

def save_xml_to_file(response, *args, **kwargs):
    with open('litres_response.xml', 'wb') as fl:
        fl.write(response.content)

>> api.get_fresh_book(start_date=datetime.datetime(2015, 7, 19, 12, 5),
                      hooks={'response': save_xml_to_file})

Ограничения

  • api.get_fresh_book нельзя вызывать чаще одного раза в секунду

  • время на сервере должно быть установлено верно, иначе timestamp запроса будет отвергнут

Скачать книгу

response = api.get_the_book(external_id='37828892-1a76-11e5-ad6a-002590591dd6')
>>> response
<Response [200]>
>>> response.headers['Content-Disposition']
'attachment; filename="Suhov_E._Rassledovaniya._Brosok_Na_Vyistrel.fb2.zip"'
>>> len(response.content)
452166

Скачать обложку

response = api.get_cover(file_id='13299029', file_ext='jpg')
>>> response
<Response [200]>
>>> response.headers['Content-Type']
'image/jpeg'
>>> len(response.content)
51405

Вместо file_id можно передать словарь книги из результатов генератора get_fresh_book

book = next(api.get_fresh_book(start_date=datetime.datetime(2015, 7, 19, 12, 5)))
>>> api.get_cover(book=book).headers['Content-Type']
'image/jpeg'

Если обложки у книги нет, функция вернёт None

book = next(api.get_fresh_book(uuid='ead79f60-4471-4952-aa81-5f126fb6da82'))
>>> api.get_cover(book=book)
None

Жанры

genres = api.get_genres()
>>> print(json.dumps(genres, indent=4, ensure_ascii=False))
[
    {
        "@id": "5003",
        "@title": "Бизнес-книги",
        "@type": "root",
        "genre": [
            {
                "@id": "5049",
                "@title": "Банковское дело",
                "@token": "bankovskoe_delo",
                "@type": "genre"
            },
            {
                "@id": "5047",
                "@title": "Кадровый менеджмент",
                "@token": "kadrovyj_menedzhment",
                "@type": "container",
                "genre": [
                    {
                        "@id": "5334",
                        "@title": "Аттестация персонала",
                        "@token": "attestaciya_personala",
                        "@type": "genre"
                    },
    ...
]

Для получения xml:

api = LitresApi(xml=True)
genres = api.get_genres()
>>> genres.xpath("//genre[@token='sport_fitnes']")[0].attrib['title']
'Спорт, фитнес'

Разработка

Запустить тесты

tox -e py38

Публикация релиза в PyPi

Для публикации релиза понадобится twine. Для удобства его можно установить глобально:

pip install twine
  1. Поднимаем версию пакета:

    __version__ = '1.1.1'
  2. Собираем пакет:

    python setup.py sdist
  3. Загружаем собранный пакет в PyPi:

    twine upload dist/litresapi-1.1.1.tar.gz

Changelog

1.2.1 (2021-06-15)

  • Add exception support for celery

1.2.0 (2020-12-21)

  • Add new content types for podcast and podcast episode

  • Drop Python <3.6 support

1.1.3 (2020-04-23)

  • Allow to download books with different partner_id

1.1.2 (2020-04-14)

  • Allow to download book covers with formats other than jpg

1.1.1 (2020-04-10)

  • Fix request params for /get_the_book/ and cover download requests

1.1.0 (2020-04-06)

  • Drop Python <3.5 support

1.0.0 (2017-02-25)

  • Enable requests hooks for get_fresh_book

  • Fix genres endpoint url

  • Introduce public parse_fresh_book method to decouple fetching from parsing

  • Enforce list for author, genre, sequence to simplify parsing

  • Wrap annotation content to CDATA tag to avoid unparsable xml

  • Better connection error handing

0.1.0 (2015-08-01)

  • First internal release

Metadata

Release files for litresapi 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for litresapi 1.2.1
File Size Uploaded
litresapi-1.2.1.tar.gz 52.8 kB Details

Release files / litresapi-1.2.1.tar.gz

Download URL litresapi-1.2.1.tar.gz
Size 52.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2886ecce6d79d91b2dbc971d7a0a839e0b65a2b2eaa3d7e229a2ebd49b2da9f0
BLAKE2b-256 checksum
How to use checksums
6655856f1be5d8dad0c2e343651df510b13ed542e07cac135a481933ea42429c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.5

Release history Release notifications | RSS feed

This release

1.2.1 This release

1 release file

1.2.0

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.2

1 release file

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page