Example #1
0
 def notify_new_code(self, userid):
     # 长链接通知
     title = '恭喜获得了一枚新的黑蜜邀请码'
     content = '快分享给朋友一起来玩吧'
     uri = 'honey://newInviteCode'
     message_dict = {'t': 'notify'}
     message_dict['title'] = title
     message_dict['subtitle'] = content
     message_dict['avatar'] = None
     message_dict['to_id'] = userid
     message_dict['uri'] = uri
     message_json = json.dumps(message_dict, ensure_ascii=False)
     logger.debug('publish_message:%s', message_json)
     rc = RedisDb.get_connection()
     publish_result = rc.publish(Constants.REDIS_CHANNEL_FOR_PUSH, message_json)
     logger.debug('publish_result: %s', publish_result)
     push_cid_obj = ClassHelper('PushCid').find_one({'userid': userid})
     logger.debug('push_cid_obj: %s', push_cid_obj)
     if (push_cid_obj is None) or push_cid_obj['logout'] is True:
         # 没有找到对应的push_cid
         self.write(ERR_PARA.message)
         return
     push_cid = MeObject('PushCid', obj=push_cid_obj)
     result = MyGtUtil.pushMessageToSingle(push_cid['cid'], title.decode("utf-8"),
                                           content.decode("utf-8"), data=uri)
     logger.debug('push result:%s', result)
def sub_pub_start():
    rc = RedisDb.get_connection()
    ps = rc.pubsub()
    ps.subscribe([Constants.REDIS_CHANNEL_FOR_PUSH])

    for item in ps.listen():
        try:
            if item['type'] == 'message':
                logger.debug('channel:' + item['channel'] + ' receive a message:')
                message_json = item['data']
                logger.debug(message_json)
                obj = json.loads(message_json)
                # logger.debug(obj)
                # logger.debug(obj.get('from_id'))
                # logger.debug(obj.get('to_id'))
                # logger.debug(s.split('_')[0])
                # logger.debug(s.split('_')[1])
                logger.debug(Pool.user_dict.__len__())
                handler = Pool.user_dict.get(str(obj.get('to_id')))
                if handler:
                    logger.debug(' handler exists')
                    # handler.write_message(crypto.encrypt(message_json))
                    handler.write_message(message_json)
                else:
                    logger.debug(' handler is None')
        except Exception as e:
            logger.error(e)
Example #3
0
def get_media_count_from_redis():
    """
    从redis中获取media数量
    :return:
    """
    client = RedisDb.get_connection(dbid=1)
    redis_key = _build_media_redis_key()
    return client.llen(redis_key) or 0
Example #4
0
def set_last_cal_id(last_id):
    """
    保存上一次计算的最后一条数据的id
    :param last_id
    :return:
    """
    client = RedisDb.get_connection()
    redis_key = _build_last_cal_id_redis_key()
    client.setex(redis_key, last_id, LAST_CAL_ID_EXPIRE_TIME)
Example #5
0
def get_last_cal_id():
    """
    获取上一次计算的最后一条数据的id
    :return:
    """
    client = RedisDb.get_connection()
    redis_key = _build_last_cal_id_redis_key()
    last_id = client.get(redis_key)
    return last_id
 def push(self):
     try:
         userid = self.jsonBody['userid']
         otherid = self.jsonBody['otherid']
         action = self.jsonBody.get('action')
         logger.debug('userid: %s, otherid: %s, action: %s', userid,
                      otherid, action)
         if userid is None or action is None or otherid is None:
             self.write(ERR_PARA.message)
             return
         uri = action_uri_dict.get(action, None)
         if uri is None:
             self.write(ERR_PARA.message)
             return
         uri = 'honey://' + uri
         logger.debug('uri: %s', uri)
         if action == 'claimed':
             uri = uri + '/' + otherid
         notify_content = self.getNotifyContent(action, otherid)
         logger.debug('notify_content: %s', notify_content)
         if notify_content is None:
             self.write(ERR_PARA.message)
             return
         # 长链接通知
         message_dict = {'t': 'notify'}
         message_dict['title1'] = notify_content['title']
         # message_dict['title2'] = notify_content['title2']
         message_dict['title2'] = notify_content['content']
         message_dict['to_id'] = userid
         message_dict['uri'] = uri
         message_json = json.dumps(message_dict, ensure_ascii=False)
         logger.debug(message_json)
         rc = RedisDb.get_connection()
         publish_result = rc.publish(Constants.REDIS_CHANNEL_FOR_PUSH,
                                     message_json)
         logger.debug('public_result: %s', publish_result)
         push_cid_obj = ClassHelper('PushCid').find_one({'userid': userid})
         logger.debug('push_cid_obj: %s', push_cid_obj)
         if push_cid_obj is None:
             # 没有找到对应的push_cid
             self.write(ERR_PARA.message)
             return
         push_cid = MeObject('PushCid', obj=push_cid_obj)
         result = MyGtUtil.pushMessageToSingle(
             push_cid['cid'],
             notify_content['title'].decode("utf-8"),
             notify_content['content'].decode("utf-8"),
             data=uri)
         logger.debug('result:%s', result)
         # result = PushUtil.pushMessageToSingle()
         self.write(result)
     except Exception, e:
         logger.error(e)
         msg = traceback.format_exc()
         logger.error(msg)
         self.write(ERR_PARA.message)
Example #7
0
def save_media_to_redis(media_ids):
    """
    保存图片到redis中
    :return:
    :param media_ids
    """
    client = RedisDb.get_connection(dbid=1)
    redis_key = _build_media_redis_key()
    for media_id in media_ids:
        client.rpush(redis_key, media_id)
Example #8
0
def addFeedRecord(userId, feedType, actionId, extraInfo=None):
    """
    给用户添加feed记录
    :param userId: 发出动作的用户
    :param feedType: 行为,包括认领,关注.分享,购买
    :param actionId: 关联的事件id,目前只有认领,actionId为faceid
    :param extraInfo:
    :return:
    """
    faceHelper = ClassHelper('Face')
    face = faceHelper.get(actionId)
    if not face:
        return
    assign = face.get('assign')
    if assign and assign.get('status') == 1:
        assigner = assign.get('assigner')
    else:
        assigner = None
    fansHelper = ClassHelper('Followee')
    feedHelper = ClassHelper('InBox')
    fans = fansHelper.find({'followee': userId, 'effect': {'$gt': 0}})
    feedUserIds = []
    for fan in fans:
        feedInfo = {
            'user': fan['user'],
            'type': feedType,
            'actionId': actionId,
            'read': False
        }
        if extraInfo:
            feedInfo['extraInfo'] = extraInfo
        feedHelper.create(feedInfo)
        feedUserIds.append(fan['user'])
    rc = RedisDb.get_connection()
    for user in feedUserIds:
        count = RedisDb.incrby('user_unread_feed_count_%s' % user, 1)
        message_json = {
            'to_id': user,
            'count': count,
            't': 'feed',
            'uri': 'honey://newFeed/$' + userId
        }
        publish_result = rc.publish(
            'push_channel', json.dumps(message_json, ensure_ascii=False))
        log.debug('public_result: %s', publish_result)
        # 发推送
        if not assigner == user:
            message = {'userid': user, 'action': 'newFeed', 'otherid': userId}
            requests.post(BaseConfig.pushUrl,
                          data=json.dumps(message),
                          headers={'X-MeCloud-Debug': '1'})
Example #9
0
def start_face_cal():
    """
    开始计算人脸向量
    :return:
    """
    media_helper = ClassHelper('Media')
    client = RedisDb.get_connection(dbid=1)
    redis_key = _build_media_redis_key()
    while client.llen(redis_key):
        media_id = client.lpop(redis_key)
        media = media_helper.get(media_id)
        if not media:
            log.err('media not found: %s' % media_id)
            continue
        faces = get_faces_from_media_id(media_id)
        face_ids = []
        for face_info in faces:
            face_info['media'] = media_id
            face = create_face_db(face_info)
            face_ids.append(face['_id'])
        # 没有获取到的话就不更新了
        if face_ids:
            media_helper.update(media_id, {'$set': {'faces': face_ids}})
            log.info('media face calculate finish: %s' % media_id)
Example #10
0
 def push(self):
     try:
         userid = self.jsonBody['userid']
         otherid = self.jsonBody['otherid']
         action = self.jsonBody.get('action')
         extra = self.jsonBody.get('extra', '')
         logger.debug('third push userid: %s, otherid: %s, action: %s',
                      userid, otherid, action)
         if userid is None or action is None or otherid is None:
             self.write(ERR_PARA.message)
             return
         uri = action_uri_dict.get(action, None)
         if uri is None:
             self.write(ERR_PARA.message)
             return
         uri = 'honey://' + uri
         logger.debug('uri: %s', uri)
         if action == 'claimed' or action == 'newFeed':
             uri = uri + '/' + otherid
         elif action == 'similarFace' or action == 'intrestedFace':
             uri = uri + '?unreadCount=' + extra
         notify_content = self.getNotifyContent(action, userid, otherid,
                                                extra)
         logger.debug('notify_content: %s', notify_content)
         if notify_content is None:
             self.write(ERR_PARA.message)
             return
         # 长链接通知
         message_dict = {'t': 'notify'}
         message_dict['title'] = notify_content['title']
         # message_dict['title2'] = notify_content['title2']
         message_dict['subtitle'] = notify_content['content']
         message_dict['avatar'] = notify_content['avatar']
         message_dict['to_id'] = userid
         message_dict['uri'] = uri
         message_json = json.dumps(message_dict, ensure_ascii=False)
         logger.debug('publish_message:%s', message_json)
         rc = RedisDb.get_connection()
         publish_result = rc.publish(Constants.REDIS_CHANNEL_FOR_PUSH,
                                     message_json)
         logger.debug('publish_result: %s', publish_result)
         push_cid_obj = ClassHelper('PushCid').find_one({'userid': userid})
         logger.debug('push_cid_obj: %s', push_cid_obj)
         if (push_cid_obj is None) or push_cid_obj['logout'] is True:
             # 没有找到对应的push_cid
             self.write(ERR_PARA.message)
             return
         claim_count = 0
         message_count = 0
         stat1 = ClassHelper('StatCount').find_one(
             {'name': 'toClaim_' + userid})
         # logger.debug('stat1:')
         if stat1:
             claim_count = stat1['count']
             if claim_count < 0:
                 claim_count = 0
         stat2 = ClassHelper('StatCount').find_one(
             {'name': 'unreadMsg_' + userid})
         if stat2:
             message_count = stat2['count']
             if message_count < 0:
                 message_count = 0
         badge = claim_count + message_count
         push_cid = MeObject('PushCid', obj=push_cid_obj)
         result = MyGtUtil.pushMessageToSingle(
             push_cid['cid'], notify_content['title'].decode("utf-8"),
             notify_content['content'].decode("utf-8"), uri, badge)
         logger.debug('result:%s', result)
         # result = PushUtil.pushMessageToSingle()
         self.write(result)
     except Exception, e:
         logger.error(e)
         msg = traceback.format_exc()
         logger.error(msg)
         self.write(ERR_PARA.message)
Example #11
0
 def send(self):
     try:
         logger.debug(self.jsonBody)
         obj = self.jsonBody
         logger.debug('to_id:' + obj.get('to_id'))
         logger.debug('c: %s', obj.get('c'))
         logger.debug('c_type: %s', obj.get('c_type'))
         logger.debug('m_id: %s', obj.get('m_id'))
         media_id = obj.get('m_id')
         from_id = self.user['_id']
         logger.debug('from_id: %s', from_id)
         user_query = MeQuery("UserInfo")
         user_info = user_query.find_one({'user': from_id})
         message_dict = {
             'from_id': from_id,
             'c': obj.get('c'),
             'to_id': obj.get('to_id'),
             'c_type': obj.get('c_type'),
             'msg_type': 2,
             'from_avatar': user_info['avatar'],
             'from_name': user_info['nickName'],
             'status': 0,
             'm_id': media_id
         }
         message = MeObject('Message', obj=message_dict)
         message['session'] = SessionUtil.create(from_id, obj.get('to_id'))
         message.save()
         # 格式化时间为long型
         message_dict['create_at'] = long(
             message['createAt'].strftime('%s')) * 1000
         message_dict['t'] = 'comment'
         message_dict['id'] = message['_id']
         message_json = json.dumps(message_dict, ensure_ascii=False)
         logger.debug(message_json)
         print type(message['createAt'])
         rc = RedisDb.get_connection()
         rc.publish(Constants.REDIS_CHANNEL_FOR_PUSH, message_json)
         # 发push
         push_cid_obj = ClassHelper('PushCid').find_one(
             {'userid': obj.get('to_id')})
         logger.debug('push_cid_obj: %s', push_cid_obj)
         if push_cid_obj and (push_cid_obj['logout'] is False):
             # push_cid = MeObject('PushCid', obj=push_cid_obj)
             title = user_info['nickName']
             content = obj.get('c')
             content = PushEmoji.getPushContent(content)
             data = 'honey://comment/' + from_id + '?m_id=' + media_id
             print title.encode('utf-8')
             print content.encode('utf-8')
             claim_count = 0
             message_count = 0
             stat1 = ClassHelper('StatCount').find_one(
                 {'name': 'toClaim_' + obj.get('to_id')})
             if stat1:
                 claim_count = stat1['count']
                 if claim_count < 0:
                     claim_count = 0
             stat2 = ClassHelper('StatCount').find_one(
                 {'name': 'unreadMsg_' + obj.get('to_id')})
             if stat2:
                 message_count = stat2['count']
                 if message_count < 0:
                     message_count = 0
             badge = claim_count + message_count
             t = threading.Thread(target=MyGtUtil.pushMessageToSingle,
                                  args=(
                                      push_cid_obj['cid'],
                                      title.encode('utf-8'),
                                      content.encode('utf-8'),
                                      data,
                                      badge,
                                  ))
             t.setDaemon(True)
             t.start()
         # logger.debug(ERR_SUCCESS.message)
         r = {}
         r['id'] = message['_id']
         r['code'] = 0
         r['errCode'] = 0
         r['create_at'] = message_dict['create_at']
         self.write(r)
         # 计数 unreadMsg +1
         logger.debug('update to_id:unreadMsg_%s unreadMsg count ',
                      obj.get('to_id'))
         ClassHelper('StatCount').updateOne(
             {'name': 'unreadMsg_' + obj.get('to_id')},
             {"$inc": {
                 'count': 1
             }},
             upsert=True)
     except Exception, e:
         logger.error(e)
         msg = traceback.format_exc()
         logger.error(msg)
         self.write(ERR_PARA.message)
Example #12
0
 def login(self):
     mid = self.jsonBody['mid']
     device = self.jsonBody['device']
     logger.debug('mid:%s', mid)
     userMigrateHelper = ClassHelper('UserMigrate')
     user_migrate = userMigrateHelper.find_one({'_sid': mid, 'isused': 0})
     logger.debug('user_migrate:%s', user_migrate)
     if not user_migrate:
         self.write(ERR_QRCODE_TO_LOGIN.message)
         return
     else:
         logger.debug('user_migrate.createAt:%s', user_migrate['createAt'])
         user = ClassHelper('User').get(user_migrate['user'])
         # user = MeUser(obj=user_obj)
         # user = MeQuery('User').get(user_migrate['user'])
         if not user:
             self.write(ERR_USER_NOTFOUND.message)
             return
         else:
             # 长链接通知
             notify_dict = {'to_id': user['_sid'], 't': 'logout'}
             message_json = json.dumps(notify_dict, ensure_ascii=False)
             logger.debug(message_json)
             # message_json. = long(message['createAt'].strftime('%s')) * 1000
             rc = RedisDb.get_connection()
             r = rc.publish(Constants.REDIS_CHANNEL_FOR_PUSH, message_json)
             logger.debug('redis publish r:%s', r)
             # del user['password']
             # del user['username']
             user['device'] = device
             user_info = ClassHelper('UserInfo').find_one(
                 {'user': user['_sid']})
             del user['createAt']
             del user['updateAt']
             del user['password']
             del user_info['createAt']
             del user_info['updateAt']
             result = {}
             result['user'] = user
             result['user_info'] = user_info
             result['errCode'] = 0
             result['info'] = 'success'
             logger.debug('result: %s', json.dumps(result))
             self.write(json.dumps(result))
             self.set_secure_cookie("u", user['_id'])
             new_cookie = str(self._new_cookie.get('u')).split(
                 '=')[1].split(';')[0].replace('"', '')
             print new_cookie
             print user['_id']
             if new_cookie is not None:
                 self.set_user_cookie_record(new_cookie, user['_id'])
             try:
                 userMigrateHelper.update(mid, {'$set': {'isused': 1}})
                 ClassHelper('User').update(
                     user['_sid'],
                     {'$set': {
                         'username': device,
                         'device': device
                     }})
                 self.save_login_log(user['_sid'])
             except Exception, e:
                 logger.error(e)
                 msg = traceback.format_exc()
                 logger.error(msg)
Example #13
0
 def send(self):
     try:
         logger.debug(self.jsonBody)
         obj = self.jsonBody
         logger.debug('to_id:' + obj.get('to_id'))
         logger.debug('c: %s', obj.get('c'))
         logger.debug('c_type: %s', obj.get('c_type'))
         logger.debug('m_id: %s', obj.get('m_id'))
         media_id = obj.get('m_id')
         from_id = self.user['_id']
         logger.debug('from_id: %s', from_id)
         user_query = MeQuery("UserInfo")
         user_info = user_query.find_one({'user': from_id})
         message_dict = {
             'from_id': from_id,
             'c': obj.get('c'),
             'to_id': obj.get('to_id'),
             'c_type': obj.get('c_type'),
             'msg_type': 2,
             'from_avatar': user_info['avatar'],
             'from_name': user_info['nickName'],
             'status': 0,
             'm_id': media_id
         }
         message = MeObject('Message', obj=message_dict)
         message.save()
         # 格式化时间为long型
         message_dict['create_at'] = long(
             message['createAt'].strftime('%s')) * 1000
         message_dict['t'] = 'comment'
         message_dict['id'] = message['_id']
         message_json = json.dumps(message_dict, ensure_ascii=False)
         logger.debug(message_json)
         print type(message['createAt'])
         rc = RedisDb.get_connection()
         rc.publish(Constants.REDIS_CHANNEL_FOR_PUSH, message_json)
         # 发push
         push_cid_obj = ClassHelper('PushCid').find_one(
             {'userid': obj.get('to_id')})
         logger.debug('push_cid_obj: %s', push_cid_obj)
         if push_cid_obj:
             # push_cid = MeObject('PushCid', obj=push_cid_obj)
             title = user_info['nickName']
             content = '新评论'
             data = 'honey://comment/' + from_id + '?m_id=' + media_id
             print title.encode('utf-8')
             print content.encode('utf-8')
             t = threading.Thread(target=MyGtUtil.pushMessageToSingle,
                                  args=(
                                      push_cid_obj['cid'],
                                      title.encode('utf-8'),
                                      content.encode('utf-8'),
                                      data,
                                  ))
             t.setDaemon(True)
             t.start()
         # logger.debug(ERR_SUCCESS.message)
         r = {}
         r['id'] = message['_id']
         r['code'] = 0
         r['create_at'] = message_dict['create_at']
         self.write(r)
     except Exception, e:
         logger.error(e)
         msg = traceback.format_exc()
         logger.error(msg)
         self.write(ERR_PARA.message)