Exemple #1
0
	def __init__(self, app_account, card, appid):
		super(client, self).__init__()
		for item in config.sections():
			if item == appid:
				self.appid = item
				self.appsecret = config[item]['app_secret']
				self._cert = (config[item]['client_cer'], config[item]['client_key'])
			else:
				pass
		self.app_account = app_account
		self.card = card
		self._token = get_token(appid, card, False)
		self._tradetoken = get_token(appid, card, True)
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 fetch_token(chat_id, code=None):
    google.state = chat_id
    token = None
    try:
        token = db.get_token(chat_id)

        if code:

            token = google.fetch_token(token_url,
                                       client_secret=client_secret,
                                       code=code)
            db.save_token(chat_id, token)
        elif 'access_token' not in token:
            refresh_token = token.get('refresh_token', None)
            if refresh_token:
                token = google.refresh_token(token_url,
                                             refresh_token,
                                             client_id=client_id,
                                             client_secret=client_secret)
                db.save_token(chat_id, token)
    except (TimeoutError, ConnectionError) as service_error:
        logger.error("Connection problem. %s" % (service_error))
        raise service_error

    if 'access_token' in token:
        return token
    else:
        return None
Exemple #4
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 #5
0
    def authenticate(self, password):
        if not bcrypt.checkpw(password.encode('utf-8'), self.password_hash):
            return json_response({"message": "Invalid login or password"}, 403)

        token = get_token(self._id)
        if token is None:
            token = create_new_token(self._id)

        return json_response({"token": token}, 200)
Exemple #6
0
def get_user_by_token(collection, token):
    user_id = get_token(token)
    if user_id is None:
        return None

    user = find_one(collection, user_id)
    if user is None:
        return None
    return User(**user)
Exemple #7
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 #8
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
def signup():
    '''
    Creates a user with an email, hashed password, push_token,
    and randomized salt.
    '''
    data_dict = request.get_json()
    email = data_dict.get("email", "")
    password = data_dict.get("password", "")
    push_token = data_dict.get("push_token", "")
    response = {"outcome": "failure"}
    if email and password and push_token:
        user = db.signup(email, password, push_token)
        response["token"] = db.get_token(user)
        response["outcome"] = "successful"
    return jsonify(response)
Exemple #10
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 #11
0
    def POST(self):
        i = web.input()
        web.header('Content-Type', 'application/json')
        if not is_valid(web):
            raise Exception("Invalid request")

        sp.pqueue.put({ "response_url": i.response_url,
                        "user_id": i.user_id,
                        "user_name": i.user_name,
                        "channel_id": i.channel_id,
                        "team_id": i.team_id,
                        "formula": i.text,
                        "token": db.get_token(i.team_id).bot_token})

        return json.dumps(
            eph_msg("Processing *%s*, position %d in queue!" %
                    (i.text, sp.pqueue.qsize())))
def login():
    '''
    Returns either
    {outcome: "failure"}
    ...or...
    {outcome: "successful", token:"token-string"}
    based on if the email/password were correct
    '''
    data_dict = request.get_json()
    email = data_dict.get("email", "")
    password = data_dict.get("password", "")
    response = {"outcome": "failure"}
    if email and password:
        user = db.get_user(email)
        if user and db.verify_password(user, password):
            response["token"] = db.get_token(user, dual=False)
            response["outcome"] = "successful"
    return jsonify(response)
Exemple #13
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 #14
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 #15
0
def requests_status(id):
    if not id or not db.has_token(id):
        return jsonify(error='invalid id'), 400

    token = db.get_token(id)
    return jsonify(token), 200