def human2machine(msg): if not isinstance(msg, unicode): msg = msg.decode('utf-8') #: process with some hard coded translations first for k, v in h_d.items(): if k in msg.split('@3bugs')[-1]: return v[0] action = None action_type = None obj = None repeated_duration = 0 import jieba.posseg as pseg seg = classify(pseg.cut(msg), l_d.keys()) for v in seg['verb']: action = ch_d.get(v, None)[0] or action action_type = action_type_d.get(action, None)[0] for n in seg['noun']: obj = ch_d.get(n, None)[0] or obj repeated_duration = find_repeated(seg) or 0 if action and (action_type is not None) and \ (action == 'capture' or obj): return action, action_type, obj, repeated_duration else: logger.info('Found unknown command %s' % msg) logger.debug('%s %s %s %s' % (str(action), str(action_type), str(obj), str(repeated_duration))) logger.debug(seg) return None
def qq_weibo_get_code(): code = request.args['code'] openid = request.args['openid'] openkey = request.args['openkey'] qqbot = db.session.query(Bot).filter(Bot.type == 1).one() bot = qqbot.build_bot() resp = bot.request_access_token(code) resp.openid = openid resp.openkey = openkey logger.debug(resp) u = db.session.query(User).filter(User.openid == openid) if u.count(): u = u.one() u.assign(resp) else: u = User() u.assign(resp) db.session.add(u) logger.info('Created new user <%s %s>' % (u.name, u.openid)) db.session.commit() if not u.token: u.generate_token() return 'your arm server token: %s' % (u.token)
def refresh_token(self, refresh_token): body = _http_get('%s%s?' % (self.auth_url, 'access_token'), \ client_id = self.client_id, \ client_secret = self.client_secret, \ refresh_token = refresh_token, \ grant_type = 'refresh_token') #: FIXME debug for refresh token from server.base import logger logger.debug('refresh token body:\n%s' % body) r = _obj_hook(dict([p.split('=') for p in body.split('&')])) r.expires_in = int(r.expires_in) + int(time.time()) return r
def enqueue(action, tweet_id, username): summary = human2machine(action) if summary: action, action_type, obj, repeated = summary user = db.session.query(User).filter(User.name == username).one() job = Job() job.action = action job.obj = obj job.tweet_id = tweet_id job.type = action_type job.status = 0 job.repeated = repeated job.added_time = datetime.utcnow() db.session.add(job) job.user = user logger.debug(user.name) db.session.commit() return job else: return None