Esempio n. 1
0
def message(data):
    if authorize_msg(request):
        if isinstance(data, Text):
            """
            <xml><ToUserName><![CDATA[gh_6cafe8fadb1b]]></ToUserName>
            <FromUserName><![CDATA[orrRbwKLJYHeqjKFL1EN9Hp-bppo]]></FromUserName>
            <CreateTime>1486964295</CreateTime>
            <MsgType><![CDATA[text]]></MsgType>
            <Content><![CDATA[我的]]></Content>
            <MsgId>6386463017764215603</MsgId>
            </xml>"""
            Msg.add(**dict(msg_id=data.msg_id,
                           msg_type=data.msg_type,
                           from_user=data.from_user,
                           to_user=data.to_user,
                           content=data.content,
                           creation_time=datetime.fromtimestamp(
                               float(data.create_time))))
        if isinstance(data, Image):
            print data, 'Image'
        if isinstance(data, Voice):
            print data, 'voice'
        if isinstance(data, Video):
            print data, 'video'
        if isinstance(data, Location):
            print data, 'location'
        return ''
    else:
        return abort(403)
Esempio n. 2
0
def test_api_get_msg(client):
    db.session.add(Msg(name='app'))
    db.session.commit()
    response = client.get(url_for('api_v1.get_msg', id=1))
    assert response.status_code == 200
    info = json.loads(response.data.decode('utf-8'))
    assert 'app' == str(info['data']['name'])
    response = client.get(url_for('api_v1.get_msg', id=6))
    assert response.status_code == 404
def test_api_get_msg(client):
    db.session.add(Msg(name="app"))
    db.session.commit()
    response = client.get(url_for("api_v1.get_msg", id=1))
    assert response.status_code == 200
    info = json.loads(response.data.decode("utf-8"))
    assert "app" == str(info["data"]["name"])
    response = client.get(url_for("api_v1.get_msg", id=6))
    assert response.status_code == 404
Esempio n. 4
0
def update():
    import time
    #session['userid'], session['token'], session['expires_in']
    oauth = get_oauth()
    if not oauth:
        print 'oauth expires'
        return redirect('/oauth')
    else:
        if time.time() - session.get('last_time', 0) < 30:
            print 'time less than 10 seconds'
            return ''
        #return ''
        print oauth.account__rate_limit_status()
        mentions = oauth.statuses__mentions(since_id=0)
        session['last_time'] = time.time()
        #mentions = oauth.statuses__mentions(since_id=session.get('since_id', 0))

        statuses = mentions.statuses

        msg = ''
        for s in statuses:
            wid = s['id']
            if Msg.get_by_weibo_id(wid):
                return ''
            try:
                coor = s['geo']['coordinates']
                if coor:
                    import random
                    coordinate = '%s,%s' % (coor[1]+random.random()*2, coor[0]+random.random()*2)
                    addr = oauth.get.location__geo__geo_to_address(coordinate=coordinate)['geos'][0]
                    avatar = s['user']['profile_image_url']
                    session['since_id'] = s.id if s.id >= session['since_id'] else session['since_id']

                    msg = Msg(coor[0], coor[1], s['bmiddle_pic'],
                            s['text'], s['user']['id'], avatar,
                            s['id'], addr['city_name'], addr['province_name'],
                            addr['address'], s['created_at'])
                    msg.save()
            except Exception as e:
                        print e

    return jsonify(success=True)
def create_msg():
    json = request.get_json(force=True)

    # Validations
    if json.get("name") is None or len(json.get("name")) > 120:
        return bad_request()

    msg = Msg.new(json["name"])
    if msg.save():
        return response(msg.serialize())

    return bad_request()
Esempio n. 6
0
def get_msgs():
    msgs = Msg.get_all()
    data = []
    for m in msgs:
        openid = m.from_user
        u = User.get_by_openid(openid)
        if not u:
            info = api.get_user_info(openid)
            info['openid'] = openid
            u = User.add_or_get(**info)
        data.append({
            'from_user': openid,
            'openid': m.msg_id,
            'head_img_url': u.head_img_url,
            'nickname': u.nickname,
            'sex': u.sex,
            'remark': u.remark,
            'update_time': str(m.update_time),
            'creation_time': str(m.creation_time),
            'content': m.content
        })
    return jsonify(r=True, data=data)
Esempio n. 7
0
def asyn(lat, lng, backup=None):
    #session['map_ids'] = []
    #if not backup:
        #if session.get('map_ids'):
            #session.pop('map_ids')

    r = get_offset_cube(lat, lng, 100000)
    msgs = Msg.get_all_points(r)
    msgs = [{'content': m.content, 'avatar': m.avatar, 'uid': m.weibo_uid, 'id': m.id, 'lat': m.lat, 'lng': m.lng, 'photo': m.photo, 'weibo_id':
        m.weibo_id} for m in msgs]

    #ids = set([m['id'] for m in msgs])
    #map_ids = set(session.get('map_ids', []))
    #diff = map_ids.union(ids) - map_ids

    #msgs = filter(lambda x:x['id'] in diff, msgs)

    #map_ids = map_ids.union(diff)
    #session['map_ids'] = list(map_ids)
    #print session['map_ids']

    return jsonify(success=True, msgs=msgs)