Skip to main content

Annict API for Python

Project description

Annict API wrapper for python

CircleCI

python-annict は Python3.6 以上をサポートしています。

インストール

使い方

認証

認証コード取得用のURLを生成

>>> from annict.auth import OAuthHandler
>>> handler = OAuthHandler(client_id='Your client ID', client_secret='Your client secret')
>>> url = handler.get_authorization_url(scope='read write')
>>> print(url)

URLをブラウザで開いて認証コードを表示します。それを handler.authenticate() の引数に渡してアクセストークンを取得します。

>>> handler.authenticate(code='Authentication code')
>>> print(handler.get_access_token())

なお、Annict上で個人用アクセストークンを発行しそれを使用する場合はこの認証フローは不要です。

参照: Annict API: 個人用アクセストークンが発行できるようになりました

API

>>> from annict.api import API
>>> annict = API('Your access token')
>>> results = annict.works(filter_title="Re:ゼロから始める異世界生活")
>>> print(results[0].title)
Re:ゼロから始める異世界生活

キャッシュ

独自のキャッシュシステムは実装していませんが、requests_cache というrequests用キャッシュプラグインとの併用を強くおすすめします。

使用例

>>> import requests_cache
>>> # 有効期限300秒でメモリにキャッシュするよう設定
>>> requests_cache.install_cache(cache_name='annict', backend='memory', expire_after=300)
>>> # 最初のリクエストはAPIから
>>> api.me()
>>> # 300秒以内の同一リクエストはキャッシュから
>>> api.me()

さらに詳しい使い方は Requests-cache 公式ドキュメント を参照してください。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

annict-0.5.0.tar.gz (29.1 kB view hashes)

Uploaded Source

Built Distributions

annict-0.5.0-py3.6.egg (19.2 kB view hashes)

Uploaded Source

annict-0.5.0-py3-none-any.whl (11.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page