豆瓣圖書 API

Oauth 2.0

http://developers.douban.com/wiki/?title=oauth2

權限不同,token 就會不同

Configuration

with python-social-auth

SOCIAL_AUTH_DOUBAN_OAUTH2_KEY = 'YOUR_KEY'
SOCIAL_AUTH_DOUBAN_OAUTH2_SECRET = 'YOUR_SECRET'
SOCIAL_AUTH_DOUBAN_OAUTH2_SCOPE = [
    'douban_basic_common,book_basic_r,book_basic_w'
]

Usage

http://developers.douban.com/wiki/?title=api_v2
http://developers.douban.com/wiki/?title=book_v2

headers = {
    'Authorization': 'Bearer YOUR_TOKEN',
}

for isbn in (item['isbn_13'], item['isbn_10']):
    book_isbn_url = 'https://api.douban.com/v2/book/isbn/%s' % (isbn)
    r = requests.get(book_isbn_url, headers=headers)
    if r.ok:
        book = json.loads(r.content)
        book_id = book['id']

        book_collect_url = 'https://api.douban.com/v2/book/%s/collection' % (book_id)
        data = {
            'status': item['status'],
        }
        if 'comment' in item:
            data['comment'] = item['comment']
        r2 = requests.post(book_collect_url, data=data, headers=headers)
        if not r2.ok:
            if r2.status_code == 409:
                # 已收藏,略過
                pass
            else:
                log.msg('post fail: %s' % (book_name), level=log.ERROR)
                log.msg('post fail: %s' % (r2.status_code), level=log.ERROR)

        break
    else:
        log.msg('isbn not found: %s' % (book_name), level=log.ERROR)
        log.msg('isbn not found: %s' % (isbn), level=log.ERROR)