Exemple #1
0
def wish_read(message, session):
    token = get_token(message, session)
    if (None == token):
        return "输入'豆瓣'完成授权后回到微信"
    client_wechat.auth_with_token(token)
    if get_state(session) == 'booklist' or get_state(session) == 'wishread':
        bookid = session.get(message.content, 0)
        if 0 == bookid:
            return "输入有误,请重新输入"
        else:
            try:
                client_wechat.book.collection(bookid)
            except:
                return "你收藏过这本书啦!"
            else:
                return "设置想读成功!"
        set_state(session, 'wishread')
    elif get_state(session) == 'dnlist':
        dnstr = message.content
        tedstr = ted_kv.get(to_binary(TED_POPULAR))
        retstr = ''
        if None != tedstr:
            speaker_list = json.loads(tedstr)
        dnid = int(dnstr)
        if dnid >= len(speaker_list):
            return '输入有误,请重新输入'
        # change id to keyward
        message.content = speaker_list[dnid]
Exemple #2
0
def wish_read(message, session):
    token = get_token(message, session)
    if (None == token):
        return "输入'豆瓣'完成授权后回到微信"
    client_wechat.auth_with_token(token)
    if get_state(session) == 'booklist' or get_state(session) == 'wishread':
        bookid =  session.get(message.content, 0)
        if 0 == bookid:
            return "输入有误,请重新输入"
        else:
            try:
                client_wechat.book.collection(bookid)
            except:
                return "你收藏过这本书啦!"
            else:
                return "设置想读成功!"
        set_state(session, 'wishread')
    elif get_state(session) ==  'dnlist':
        dnstr =  message.content
        tedstr = ted_kv.get(to_binary(TED_POPULAR))
        retstr = ''
        if None != tedstr:
            speaker_list = json.loads(tedstr)
        dnid = int(dnstr)
        if dnid >= len(speaker_list):
           return '输入有误,请重新输入'
        # change id to keyward
        message.content = speaker_list[dnid]
Exemple #3
0
def douban(message, session):
    guid = message.source
    token = get_token(message, session)
    if None != token:
        try:
            client_wechat.auth_with_token(token)
            client_wechat.book.search('哈哈', '', 0, 1)
            return u"豆瓣授权成功! " + u"公众号id:" + guid + u'豆瓣token:'+token
        except:
            print u"豆瓣token失效了"
    # auth again
    auth_des = u'请点我完成豆瓣授权'
    auth_url =  client_wechat.authorize_url + '&state='+ guid
    auth_html = '<a href="%s">%s</a>' % (auth_url,auth_des)
    return auth_html
Exemple #4
0
def douban(message, session):
    guid = message.source
    token = get_token(message, session)
    if None != token:
        try:
            client_wechat.auth_with_token(token)
            client_wechat.book.search('哈哈', '', 0, 1)
            return u"豆瓣授权成功! " + u"公众号id:" + guid + u'豆瓣token:' + token
        except:
            print u"豆瓣token失效了"
    # auth again
    auth_des = u'请点我完成豆瓣授权'
    auth_url = client_wechat.authorize_url + '&state=' + guid
    auth_html = '<a href="%s">%s</a>' % (auth_url, auth_des)
    return auth_html
Exemple #5
0
def book(message, session):
    token = get_token(message, session)
    if (None == token):
        return "输入'豆瓣'完成授权后回到微信"

    # get json data from douban 
    try:
        client_wechat.auth_with_token(token)
        querystr = ''
        if message.type == 'text':
            querystr = message.content
        elif message.type == 'voice':
            querystr = message.recognition
        else:
            return u'暂时没法处理这种输入,我们赶紧查查'
            
        res = client_wechat.book.search(querystr, '', 0, 5)
        res_str = json.dumps(res)
        print res_str
    except:
        return u"豆瓣授权过期了,请输入'豆瓣'(db)重新授权~"
    count = res['count']
    if count == 0:
        if message.type == 'voice':
            return u"输入的是: '%s' 么?改用文字输入试试吧" %message.recognition
        else:
            return u"没找到啊,修改下关键字试试~"

    ret_str = u'书籍列表:\n'
    if message.type == 'voice':
        ret_str += u"识别的关键词:%s\n" %message.recognition
        
    for i in range(count):
        bookid = res['books'][i]['id']
        #save bookid
        session[str(i)] = bookid
        bookurl = "http://book.douban.com/subject/"+ bookid
        bookauthor = ''
        for index in range(len(res['books'][i]['author'])):
            bookauthor += res['books'][i]['author'][index] + ' '
        ret_str +=  '[' + str(i) + ']' + '.' +res['books'][i]['title']  \
                    + '\n' + bookauthor +  ','   \
                    + res['books'][i]['publisher'] + ',' \
                    + res['books'][i]['pubdate'] \
                    + '\n ' + bookurl + '\n ' + '\n ' 
    ret_str +=  u'输入书的序号0,1,2...可直接标记为想读'
    set_state(session, 'booklist')
    return ret_str 
Exemple #6
0
def book(message, session):
    token = get_token(message, session)
    if (None == token):
        return "输入'豆瓣'完成授权后回到微信"
    client_wechat.auth_with_token(token)
    # get json data from douban 
    try:
        querystr = ''
        if message.type == 'text':
            querystr = message.content
        elif message.type == 'voice':
            querystr = message.recognition
        else:
            return u'暂时没法处理这种输入,我们赶紧查查'
            
        res = client_wechat.book.search(querystr, '', 0, 5)
        res_str = json.dumps(res)
        print res_str
    except Exception, e:
        traceback.print_exc()
        return u"豆瓣授权过期了,请输入'豆瓣'(db)重新授权~"
Exemple #7
0
def book(message, session):
    token = get_token(message, session)
    if (None == token):
        return "输入'豆瓣'完成授权后回到微信"
    client_wechat.auth_with_token(token)
    # get json data from douban
    try:
        querystr = ''
        if message.type == 'text':
            querystr = message.content
        elif message.type == 'voice':
            querystr = message.recognition
        else:
            return u'暂时没法处理这种输入,我们赶紧查查'

        res = client_wechat.book.search(querystr, '', 0, 5)
        res_str = json.dumps(res)
        print res_str
    except Exception, e:
        traceback.print_exc()
        return u"豆瓣授权过期了,请输入'豆瓣'(db)重新授权~"