Welcome to python-shanbay’s documentation!

python-shanbay

提供一系列操纵扇贝网 (www.shanbay.com) 的 API 。

Build PyPI version

Features

  • send/reply message
  • manage team
  • support shanbay api v1

Installation

To install python-shanbay, simply:

$ pip install shanbay

Basic Usage

OAuth2 认证 API(可以通过 这个脚本 获取 token)

   >>> from shanbay import API
   >>> token = {
   "access_token": "7ANNoQFY02rJkqqm8Zi67aQ9N6ES8G",
   "expires_in": "1592000", "expires_at": 1328664099.868803,
   "token_type": "Bearer", "state": "H3rTN84NG2TdunSt9bG02acEkSNWiW",
   "scope": [ "read", "write" ]
   }
   >>>
   >>> api = API('client_id_xxyyyx', token)
   >>> api.user()
   {u'avatar': u'http://qstatic.shanbay.com/avatar/media_store/3034aee41d32d464aac362cf608cb735.png?imageView/1/w/80/h/80/',
u'id': 1279912,
   u'nickname': u'\u266b mozillazg',
   u'username': u'mozillazg'}
   >>>

用户名密码认证 API(不支持 REST API)

>>> from shanbay import Shanbay, Message
>>> shanbay = Shanbay('username', 'password')
>>> shanbay.login()
True
>>> message = Message(shanbay)
>>> message.send_message(['mozillazg'], 'hello', 'hello')
True

使用案例

已知以下项目使用了本模块, 欢迎添加你的项目 ;)

API

class shanbay.ShanbayException

异常基类

class shanbay.AuthException

未登录或登录已过期

class shanbay.ConnectException

网络连接出现异常情况

class shanbay.Shanbay(username, password)
Parameters:
  • username – 用户名
  • password – 密码
>>> from shanbay import Shanbay
>>> s = Shanbay('username', 'password')
>>> s.login()
True
login(**kwargs)

登录

server_date()

获取扇贝网服务器时间(北京时间)

server_date_utc()

获取扇贝网服务器时间(UTC 时间)

REST API

class shanbay.api.API(client_id, token)
user(url='https://api.shanbay.com/account/')

获取用户信息

word(word, url='https://api.shanbay.com/bdc/search/')

查询单词

add_word(word_id, url='https://api.shanbay.com/bdc/learning/')

添加单词

examples(word_id, type=None, url='https://api.shanbay.com/bdc/example/')

获取单词的例句

add_example(word_id, original, translation, url='https://api.shanbay.com/bdc/example/')

创建例句

favorite_example(example_id, url='https://api.shanbay.com/bdc/learning_example/')

收藏例句

delete_example(example_id, url='https://api.shanbay.com/bdc/example/{example_id}/')

删除例句

notes(word_id, url='https://api.shanbay.com/bdc/note/')

获取笔记

add_note(self, word_id, note, url='https://api.shanbay.com/bdc/note/')

创建笔记

favorite_note(note_id, url='https://api.shanbay.com/bdc/learning_note/')

收藏笔记

delete_note(note_id, url='https://api.shanbay.com/bdc/note/{note_id}/')

删除笔记

add_example(*args, **kwargs)

创建例句

add_note(*args, **kwargs)

创建笔记

add_word(*args, **kwargs)

添加单词

delete_example(*args, **kwargs)

删除例句

delete_note(*args, **kwargs)

删除笔记

examples(*args, **kwargs)

获取单词的例句

favorite_example(*args, **kwargs)

收藏例句

favorite_note(*args, **kwargs)

收藏笔记

notes(*args, **kwargs)

获取笔记

user(*args, **kwargs)

获取用户信息

word(*args, **kwargs)

查询单词

Message

class shanbay.message.Message(shanbay)

站内消息

Parameters:shanbayShanbay 实例对象
>>> from shanbay import Shanbay, Message
>>> s = Shanbay('username', 'password')
>>> s.login()
>>> m = Message(s)
reply_message(message_url, body)

回复某条站内消息

Parameters:
  • message_url – 该条消息的页面 URL
  • body – 内容(不能超过 1024 个字符)
send_message(recipient_list, subject, body)

发送站内消息

Parameters:
  • recipient_list – 收件人列表
  • subject – 标题
  • body – 内容(不能超过 1024 个字符)

Team

class shanbay.team.Team(shanbay, team_url)

小组管理

Parameters:
  • shanbayShanbay 实例对象
  • team_url – 小组首页 URL
>>> from shanbay import Shanbay, Team
>>> s = Shanbay('username', 'password')
>>> s.login()
>>> t = Team(s, 'http://www.shanbay.com/team/1234/')
dismiss(member_ids)

踢人. 注意别把自己给踢了.

Parameters:member_ids – 组员 ids
Returns:bool
forum_id()

小组发帖要用的 forum_id

info()

小组信息

Returns:小组信息
Return type:dict

返回值示例

{
    'title': u'title',  # 标题
    'leader': u'leader',  # 组长
    'date_created': datetime.datetime(2013, 10, 6, 0, 0),  # 创建日期
    'rank': 1000,  # 排名
    'number': 10,  # 当前成员数
    'max_number': 20,  # 最大成员数
    'rate': 1.112,  # 打卡率
    'points': 23  # 总成长值
}
max_page()

获取小组成员管理页面的最大页数

members()

获取小组所有成员的信息列表

new_topic(title, content)

小组发贴

Returns:帖子 id 或 None
reply_topic(topic_id, content)

小组回帖

Returns:帖子 id 或 None
single_page_members(page_number=1)

获取单个页面内的小组成员信息

Parameters:page_number – 页码
Returns:包含小组成员信息的列表

返回值示例:

[{
    'id': 123,                  # member_id
    'username': 'jim',          # username
    'nickname': 'Jim',          # 昵称
    'role': u'小组长',          # 身份
    'points': 1234,             # 贡献成长值
    'days': 100,                # 组龄
    'rate': 99.9,               # 打卡率
    'checked_yesterday': True,  # 昨天是否打卡
    'checked': False,           # 今天是否打卡
}, {
    # ...
}]
update_limit(days, kind=2, condition=u'>=')

更新成员加入条件

Return type:bool

Changelog

0.3.6 (2017-02-09)

  • requests 的最低依赖版本改为 2.9.0

0.3.5 (2017-01-20)

  • 修复用户名,密码登录功能失效的问题

0.3.4 (2015-08-25)

  • 修复无法发送短信的问题

0.3.3 (2015-01-13)

  • 修复扇贝改版导致”无法获取组员用户名导致发送短信失败”的问题
  • 修复扇贝改版导致”无法成功踢人”的问题

0.3.2 (2014-08-22)

  • 小组管理页面改版

0.3.1 (2014-08-05)

  • 修复”小组成员页面改版导致无法获取组员信息”

0.3.0 (2014-07-19)

  • 封装 新版扇贝官方 API
  • 修复”扇贝更改小组成员页面的 URL 导致无法获取组员信息”

0.2.1 (2014-06-24)

  • 修复 team.info 无法处理打卡率是 0% 的情况
  • 登录成功时,shanbay.login() 的返回值改为 True

0.2.0 (2014-06-09)

  • 删除 shanbay.API, 因为扇贝网不再支持 API v0.8, 并且新的 API 尚未释出
  • 各 api 接口移除 @property 装饰器

0.1.1 (2014-05-15)

  • 新增站内消息 api
  • 新增小组管理 api
  • 调整 api 接口

具体用法可以参考 tests/ 以及 python-shanbay-team-assistant

0.1.0 (2014-03-31)

Indices and tables