示例#1
0
    def handle_commands(self, commands):
        def _is_user(name):
            return User.query.filter_by(name=name).count() > 0

        def _auto_reply(content):
            s = content.split('@3bugs')[-1]
            for k, v in a.items():
                if k in s or s in k:
                    return v
            return None

        for command in commands:
            if not waitings.in_queue(command['id']) and \
                    _is_user(command['name']) and \
                    not unknown.in_queue(command['id']):

                auto_reply = _auto_reply(command['content'])
                if auto_reply:
                    self.repost(auto_reply(), command['id'])
                    unknown.enqueue(command['id'], command['content'])
                    continue

                new_job = waitings.enqueue(command['content'],
                        command['id'], command['name'])
                if new_job:
                    logger.info('found new job <%d %s>' % (
                            new_job.id, new_job.action))
                else:
                    unknown.enqueue(command['id'], command['content'])
                    self.repost(s['unknowncommand'](), command['id'])
示例#2
0
def report_job(job_id):
    user = auth()
    if not user:
        abort(403)

    # FIXME insecurity data transfering
    data = json.loads(request.args['data'])

    # FIXME didn't check if it is a valid report here
    report = data['report']
    #: is query all or capture, save the image
    if (report['action'] == 'query' and report['obj'] == 'all') or \
            (report['action'] == 'capture') and request.files.keys():
        image = request.files.get(request.files.keys()[0])
        imagesbin.save(str(job_id), image)

    job = workings.get(job_id)
    if job:
        #: enqueue job to reports, and set the status to finished
        reports.enqueue(job_id, report=report)
        logger.info('got report <%d %s> from arm <%s %s>' % (
                    job_id, report, user.name, user.token))
        return jsonify(report='OK'), 200
    else:
        return jsonify(report='FAILED'), 404
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
示例#4
0
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)
示例#5
0
def fetch_job():
    url = 'http://localhost:5000/arm/job'
    payload = json.dumps(token)
    resp = requests.get(url, data=payload, headers=json_header)
    logger.info('got response <%d>' % resp.status_code)
    if resp.status_code == 200:
        logger.info('new job <%d %s> fetched' % (
                            resp.json['id'], resp.json['action']))
    return resp.json
示例#6
0
def handle_commands(commands):
    def _is_user(name):
        return db.session.query(User).filter(User.name == name).count() > 0

    for command in commands:
        if not waitings.in_queue(command.id) and _is_user(command.name) and not unknown.in_queue(command.id):
            new_job = waitings.enqueue(command.text, command.id, command.name)
            if new_job:
                logger.info("found new job <%d %s>" % (new_job.id, new_job.action))
            else:
                unknown.enqueue(command.id, command.text)
示例#7
0
def report_job(job_id, report):
    url = 'http://localhost:5000/arm/job/%d' % job_id
    payload = token
    token['report'] = {
            'obj': 'tv',
            'action': 'turnon',
            'type': 0,
            'status': report
    }
    payload = json.dumps(payload)
    resp = requests.put(url, data=payload, headers=json_header)
    logger.info('got response <%d>' % resp.status_code)
    return resp
示例#8
0
def request_job():
    user = auth()
    if not user:
        abort(403)
    job = waitings.get(user.id)
    if job:
        #: enqueue job to workings, and set the status to working
        workings.enqueue(job.id)
        logger.info('arm <%s %s> got new job <%d %s %s>' % (
                    user.name, user.token, job.id, job.action, job.obj))
        return jsonify(action=job.action, obj=job.obj, id=str(job.id),
                       repeated=job.repeated)
    else:
        return jsonify(action='None'), 404
示例#9
0
def send():
    report = reports.get()
    if report:
        #: is query all
        if report.type == 1 and report.obj == 'all':
            resp = upload_image(report.report, str(report.id),
                                report.user.name)
        else:
            resp = repost(report.report, report.tweet_id)
        reports.archive(report.id)
        logger.info('archived job <%d %s>' % (report.id, report.action))
        return resp
    else:
        return None
示例#10
0
 def send(self):
     report = reports.get()
     if report:
         if report.type == 1 and report.obj == 'all' or \
                 report.action == 'capture':
             resp = self.upload_image(report.report, str(report.id),
                                      report.user.name)
         else:
             resp = self.repost(report.report, report.tweet_id)
         reports.archive(report.id)
         logger.info('archived job <%d %s>' % (report.id, report.action))
         return resp
     else:
         return None
示例#11
0
def qqbot_callback():
    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(qqweibot_callback_uri)
    resp = bot.request_access_token(code)
    resp.openid = openid
    resp.openkey = openkey

    qqbot.assign(resp)
    db.session.commit()
    logger.info('Updated qq bot info <%s %s>' % (qqbot.name, qqbot.openid))

    return 'OK'
示例#12
0
def send():
    report = reports.get()
    if report:
        if report.type == 1 and report.obj == "all":
            logger.info("post image")
        else:
            logger.info("sent report <%s>" % (report.report))
        reports.archive(report.id)
        logger.info("archived job <%d %s>" % (report.id, report.action))
    else:
        return None
示例#13
0
def post_order(order):
    order = '@%s %s' % (bot.name, order)
    userbot.post.t__add(content=order)
    logger.info('new order <%s> post' % order)