• Stars
    star
    743
  • Rank 59,051 (Top 2 %)
  • Language
    Python
  • Created over 11 years ago
  • Updated almost 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python client library for Douban APIs (OAuth 2.0)

douban-client

douban-client 是对豆瓣 API v2 接口进行了一个简单封装,主要包括了 OAuth 2.0 认证、图片上传以及接口方面的调用。

目前已完成的接口有:

* 用户 User
* 广播 Miniblog
* 豆邮 Doumail
* 日记 Note
* 相册 Album
* 图片 Photo
* 读书 Book
* 电影 Movie
* 音乐 Music
* 线上活动 Online
* 同城活动 Event
* 论坛 Discussion
* 回复 Comment

安装

pip install douban-client

easy_install douban-client

使用说明

OAuth 2.0 认证

from douban_client import DoubanClient

API_KEY = 'your api key'
API_SECRET = 'your api secret'

# 在 OAuth 2.0 中,
# 获取权限需要指定相应的 scope,请注意!!
# scope 权限可以在申请应用的 "API 权限" 查看。

SCOPE = 'douban_basic_common,shuo_basic_r,shuo_basic_w'

client = DoubanClient(API_KEY, API_SECRET, your_redirect_uri, SCOPE)

# 以下方式 2 选 1:
# 1. 引导用户授权
print 'Go to the following link in your browser:' 
print client.authorize_url
code = raw_input('Enter the verification code:')
client.auth_with_code(code)

# 2. 如果有之前有 token,则可以
client.auth_with_token(token)

# Token Code
token_code = client.token_code

# Refresh Token
# 请注意:`refresh_token_code` 值仅可在授权完成时获取(即在 `auth_with_code`, `auth_with_password` 之后)
refresh_token_code = client.refresh_token_code
client.refresh_token(refresh_token_code) # refresh token

至此,已经完成 OAuth 2.0 授权。

Douban API v2 说明

1. 豆瓣Api V2认证统一使用OAuth2
2. 数据返回格式统一使用json,GData不再使用
3. 需要授权的Api,需要加access_token的Header,并且使用https协议,限制具体见OAuth2文档
4. 不需要授权公开api可以使用http,参数里面如果不包含apikey的话,限制单ip每分钟10次
5. Api里面的通配符,:id代表纯数字, :name代表由数字+字母+[-_.]这些特殊字符
6. 使用HTTP Status Code表示状态
7. 列表参数使用start和count
8. POST/PUT 时中文使用UTF-8编码
9. 时间格式:yyyy-MM-dd HH:mm:ss, 如"2007-06-28 11:16:11"

接口说明

默认参数(参考豆瓣官方文档):

start: 0
count: 20

所有返回数据以豆瓣官方文档为准,各接口末尾处均有相应链接入口。

用户 User

# 以下 id 指用户数字 id
当前用户 client.user.me
指定用户 client.user.get(id) 
搜索用户 client.user.search(q)       # q: 搜索的关键词

# 此处是将广播关系接口放置到用户
关注用户 client.user.follow(id)
取消关注 client.user.unfollow(id)
粉丝信息 client.user.followers(id, start, count)
关注信息 client.user.following(id, start, count) 
加入黑名单 client.user.block(id)

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

http://developers.douban.com/wiki/?title=community_1_shuo#user_info

广播 Miniblog

# 以下 id 指广播数字 id
当前用户Timeline client.miniblog.home_timeline(count)
指定用户Timeline client.miniblog.user_timeline(user_id, count)

获取一条广播 client.miniblog.get(id)
新写一条广播 client.miniblog.new(text)
新写图片广播 client.miniblog.new(text, image=open('/path/pic.png'))
删除一条广播 client.miniblog.delete(id)

推荐网址 client.miniglog.rec(title='', url='', desc='', image='http://url.jpg')

获取某广播回复列表 client.miniblog.comments(id)
回复某条广播       client.miniblog.comment.new(id, text)
获取某条广播回复   client.miniblog.comment.get(comment_id)
删除某条广播回复   client.miniblog.comment.delete(comment_id)

赞广播 client.miniblog.like(id)
取消赞 client.miniblog.unlike(id)
赞某广播用户列表 client.miniblog.likers(id)

转发广播 client.miniblog.reshare(id)
转发某广播的用户列表 client.miniblog.resharers(id)

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

豆邮 Doumail

# 以下 id 指豆邮数字 id
# 豆邮发送过多会需要验证,请注意
获取一封豆邮 client.doumail.get(id)
新写一封豆邮 client.doumail.new(title, content, receiver_id)

标记一封豆邮 client.doumail.read(id)
批量标记豆邮 client.doumail.reads(ids) # ids 为 list

删除一封豆邮 client.doumail.delete(id)
批量删除豆邮 client.doumail.deletes(ids) # ids: [id, id, id]

豆邮收件箱列表 client.doumail.inbox(start, count)
豆邮发件箱列表 client.doumail.outbox(start, count)
未读豆邮列表   client.doumail.unread(start, count)

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

日记 Note

# 以下 id 指日记数字 id
# format: html_full, html_short, abstract, text,默认为text
获取一篇日记 client.note.get(id, format='text')
新写一篇日记 client.note.new(title, content)
更新一篇日记 client.note.update(title, content)
删除一篇日记 client.note.delete(id)

喜欢一篇日记     client.note.like(id)
取消喜欢一篇日记 client.note.unlike(id)

获取用户日记列表       client.note.list(user_id, start, count)
获取用户喜欢的日记列表 client.note.liked_list(user_id, start, count)

获取回复列表  client.note.comments(id, start, count)
新加一条回复  client.note.comment.new(id, content)
获取一条回复  client.note.comment.get(comment_id)
删除一条回复  client.note.comment.delete(comment_id)

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

相册 Album

# 以下 id 指相册数字 id
# desc 描述文字
获取一个相册 client.album.get(id)
新建一个相册 client.album.new(title, desc)
更新一个相册 client.album.update(id, title, desc)
删除一个相册 client.album.delete(id)

获取用户相册列表 client.album.list(user_id, start, count)
用户喜欢相册列表 client.album.liked_list(user_id, start, count)
获取相册图片列表 client.album.photos(id, start, count)

喜欢一个相册 client.album.like(id)
取消喜欢相册 client.album.unlike(id)

http://developers.douban.com/wiki/?title=photo_v2#get_album

图片 Photo

# 以下 id 指图片数字 id
获取一张图片 client.photo.get(id)
上传一张图片 client.photo.new(album_id, image, desc) # image = open('/path/pic.png')
更新图片描述 client.photo.update(id, desc)     # desc 为描述文字
删除一条图片 client.photo.delete(id)

喜欢一张图片 client.photo.like(id)
取消喜欢图片 client.photo.unlike(id)

获取回复列表  client.photo.comments(id, start, count)
新加一条回复  client.photo.comment.new(id, content)
获取一条回复  client.photo.comment.get(comment_id)
删除一条回复  client.photo.comment.delete(comment_id)

http://developers.douban.com/wiki/?title=photo_v2#get_photo

读书 Book

# 以下 id 指图书条目数字 id
# q: 关键词, tag: 标签
获取一本图书信息 client.book.get(id)
通过isbn获取信息 client.book.isbn(isbn_number)
搜索图书信息     client.book.search(q, tag, start, count) 

获取图书标签 client.book.tags(id)
获取用户标签 client.book.tagged_list(user_id)

发表一条书评 client.book.review.new(id, title, content)
更新一条书评 client.book.review.update(review_id, title, content)
删除一条书评 client.book.review.delete(review_id)

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

电影 Movie

# 以下 id 指电影条目数字 id
# q: 关键词, tag: 标签
获取一部电影信息 client.movie.get(id)
获取影人信息 client.movie.celebrity(celebrity_id)
通过imdb获取电影 client.movie.imdb(imdb_number)
搜索电影信息     client.movie.search(q, tag, start, count) 

获取电影标签 client.movie.tags(id)
获取用户标签 client.movie.tagged_list(user_id)

发表一条影评 client.movie.review.new(id, title, content)
更新一条影评 client.movie.review.update(review_id, title, content)
删除一条影评 client.movie.review.delete(review_id)

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

音乐 Music

# 以下 id 指音乐条目数字 id
# q: 关键词, tag: 标签
获取音乐信息 client.music.get(id)
搜索音乐信息 client.music.search(q, tag, start, count) 

获取音乐标签 client.music.tags(id)
获取用户标签 client.music.tagged_list(user_id)

发表一条乐评 client.music.review.new(id, title, content)
更新一条乐评 client.music.review.update(review_id, title, content)
删除一条乐评 client.music.review.delete(review_id)

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

线上活动 Online

# 以下 id 指线上活动数字 id
# begin_time, end_time 格式为 '%Y-%m-%d %H:%M:%S'
# cate 可选值: day, week, latest
获取一条线上活动 client.online.get(id)
发表一条线上活动 client.online.new(title, desc, begin_time, end_time)
更新一条线上活动 client.online.update(title, desc, begin_time, end_time)
删除一条线上活动 client.online.delete(id)

参加一条线上活动 client.online.join(id)
取消参加线上活动 client.online.quit(id)

喜欢一条线上活动 client.online.like(id)
取消喜欢线上活动 client.online.unlike(id)

获取线上活动图片列表 client.online.photos(id, start, count)
上传图片到线上活动   client.online.upload(id, image) # image = open('xxx.jpg')

获取线上活动讨论列表 client.online.discussions(id, start, count)
在线上活动新发讨论   client.online.discussion.new(id, title, content)

获取参加线上活动成员列表 client.online.participants(id, start, count)

获取线上活动列表 client.online.list(cate, start, end) 
获取参加过的活动 client.online.joined(user_id, start, count)
获取创建过的活动 client.online.created(user_id, start, count)

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

同城活动 Event

# 以下 id 指同城活动 id 
# q: 关键词, loc: 城市
# day_type: future, week, weekend, today, tomorrow
# type: all,music, film, drama, commonweal, salon, \
#       exhibition, party, sports, travel, others
获取同城活动 client.event.get(id)
搜索同城活动 client.event.search(q, loc, start, count)

参加同城活动 client.event.join(id)
取消参加活动 client.event.quit(id)

对同城活动感兴趣 client.event.wish(id)
取消同城活动兴趣 client.event.unwish(id)

某同城活动参加者   client.event.participants(id, start, count)
某同城活动感兴趣者 client.event.wishers(id, start, count)

获取用户创建过的同城活动 client.event.owned(user_id, start, count)
获取用户参加过的同城活动 client.event.participated(user_id, start, count)
获取用户感兴趣的同城活动 client.event.wished(user_id, start, count)

获取同城活动列表 client.event.list(loc, day_type, type, start, count)

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

论坛 Discussion

# 以下 id 指论坛帖子 id 
# target 指相应产品线(如 online, review 等)
# target_id 指相应产品 id
获取帖子 client.discussion.get(id)
发表帖子 client.discussion.new(target, target_id, title, content)
更新帖子 client.discussion.update(id, title, content)
删除帖子 client.discussion.delete(id)

获取帖子列表 client.discussion.list(target, target_id)

获取回复列表 client.discussion.comments(id, start, count)
新加一条回复 client.discussion.comment.new(id, content)
获取某条回复 client.discussion.comment.get(comment_id)
删除某条回复 client.discussion.comment.delete(comment_id)

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

已实现的接口中单元测试覆盖 90%+,如果文档中有没有说明的可以参考下: https://github.com/liluo/douban-client/tree/master/tests

Changelog

v0.0.6 [2013-12-18]

  • 兼容 Python 3.x
  • 接口变更:
+ note: upload_photo
- user: block, friendships, follow_in_common
- movie: celebrity_works
- miniblog: mentions

v0.0.5 [2013-04-26]

  • 修复文档中关于授权部分的错误
  • 修复 refresh_token 错误
  • DoubanClient 实例对象增加 token_code, refresh_token_code 属性

v0.0.4 [2013-04-07]

  • 增加 refresh token 方法

v0.0.3 [2012-10-23]

  • 解决 py-oauth2 与 python-oauth2 命名冲突
  • 同步更新同城活动 api

v0.0.2 [2012-09-07]

  • 与豆瓣官网同步,调整 people -> user
  • 获取创建线上活动接口变更 online.owned -> online.created
  • 去除已被删除的豆瓣猜接口

v0.0.1 [2012-09-06]

  • 根据豆瓣 API v2 文档,发布第一个版本

联系

  • 使用 douban-client 过程中遇到 bug, 可以到 Issues 反馈
  • 比较紧急的问题可以在 Douban 或者 Twitter @liluoliluo
  • 欢迎提 pull request

More Repositories

1

DOUAudioStreamer

A Core Audio based streaming audio player for iOS and macOS
Objective-C
2,771
star
2

dpark

Python clone of Spark, a MapReduce alike framework in Python
Python
2,692
star
3

code

[DEPRECATED]Douban CODE
CSS
1,808
star
4

beansdb

Archived, see GoBeansDB instead.
C
871
star
5

rexxar-android

Mobile Hybrid Framework Rexxar Android Container
Java
667
star
6

rexxar-ios

Mobile Hybrid Framework Rexxar iOS Container
Objective-C
578
star
7

FRDIntent

A framework for handle the call between view controllers in iOS
Swift
493
star
8

gobeansdb

Distributed object storage server from Douban Inc.
Go
450
star
9

libmc

Fast and light-weight memcached client for C++ / #python / #golang #libmc
C++
442
star
10

greenify

Make blocking C library work with gevent
C
426
star
11

ynm3k

UI Automation + YUItest driven acceptance tests that can be hooked into Jenkins
JavaScript
410
star
12

paracel

Distributed training framework with parameter server
C++
337
star
13

douban-objc-client

Objective-C client library for Douban APIs (OAuth 2.0)
Objective-C
254
star
14

beanseye

Proxy and monitor for beansdb in Go
Go
234
star
15

rexxar-web

Mobile Hybrid Framework Rexxar Web SDK
JavaScript
207
star
16

Kenshin

Kenshin: A time-series database alternative to Graphite Whisper with 40x improvement in IOPS
Python
205
star
17

tfmesos

Tensorflow in Docker on Mesos #tfmesos #tensorflow #mesos
Python
191
star
18

pymesos

A pure python implementation of Mesos scheduler and executor
Python
163
star
19

brownant

Brownant is a web data extracting framework.
Python
158
star
20

linguist

Language Savant, Python clone of github/linguist.
Python
153
star
21

graph-index

index of Graphite & Diamond
Python
129
star
22

CaoE

Kill all children processes when the parent dies
Python
97
star
23

douban-quixote

Douban's Quixote
Python
82
star
24

douban-utils

Douban's Utils
Python
59
star
25

python-libmemcached

DEPRECATED, use https://github.com/douban/libmc instead. python-libmemcached is a python extention for libmemcached
Python
57
star
26

PyCharlockHolmes

Character encoding detecting library for Python using ICU and libmagic.
Common Lisp
50
star
27

DOUSNSSharing

SNS OAuth 2 binding and sharing
Objective-C
47
star
28

ellen

Ellen is a wrapper of pygit2 and git command.
Python
41
star
29

Polymorph

Transform value of dictionary to property of Objective-C class, by using a `dynamic` like directive.
Objective-C
40
star
30

douban-sqlstore

Douban's MySQL lib.
Python
31
star
31

gpack

GIT Smart HTTP Server Rack Implementation, Python clone of https://github.com/schacon/grack
Python
30
star
32

douban-orz

The Missing Data Manager In Douban
Python
29
star
33

douban-mc

Douban's Memcached lib for python.
Python
27
star
34

helpdesk

Yet another helpdesk based on multiple providers
Python
22
star
35

sina

A GIT Smart HTTP Server WSGI Implementation.
Python
21
star
36

sa-tools-core

Handy tools for sysadmin.
Python
18
star
37

graphite-kenshin

A plugin for using graphite-web with the kenshin-based storage backend.
Python
16
star
38

charts

Helm charts from douban
Smarty
15
star
39

gobeansproxy

A proxy for GoBeansDB
Go
13
star
40

beansdbadmin

GoBeansDB Admin UI
Python
9
star
41

redarrow-rs

A command dispatcher to run executables remotely and safely.
Rust
6
star
42

MTURLProtocol

Multiple NSURLProtocol subclasses alternative solution.
Objective-C
4
star
43

python-libmagic

A wrapper for libmagic with static build.
Python
3
star
44

qiniu-exporter

Go
2
star
45

aliyun-exporter

Go
2
star
46

pyquicklz

C
1
star
47

upyun-exporter

Go
1
star
48

sa-tools-go

go version for sa-tools
Go
1
star