Exemplo n.º 1
0
 def post(self):
     ret = json.dumps({'status': 0, 'content': 'OK'})
     db = Mydb()
     try:
         info = self.request.protocol + "://" + self.request.host + ", method=" + self.request.method + ", access url=" + self.request.uri
         logger.info(info)
         body = self.request.body_arguments
         Args = [
             self_argument('username', True, helpinfo='Miss username'),
             self_argument('password', True, helpinfo='Miss password'),
         ]
         s, vals = DataIsValid(Args, body)
         if s:
             raise Exception(vals)
         username = vals['username']
         password = vals['password']
         if not (username or password):
             raise Exception('no password or username')
         sql = "SELECT * FROM user WHERE username='******' and password='******'" % (username, password)
         s, f = db.get(sql)
         if f:
             raise Exception("The user is exist!")
         else:
             sql = "INSERT INTO user VALUES (%s, %s)"
             s, f = db.modify(sql, [username, password])
             if s:
                 raise Exception(f)
             ret = json.dumps({'status': 0, 'content': 'OK'})
     except Exception, e:
         logger.error(str(e))
         ret = json.dumps({'status': '-110', 'content': str(e)})
Exemplo n.º 2
0
 def get(self):
     try:
         info = self.request.protocol + "://" + self.request.host + ", method=" + self.request.method + ", access url=" + self.request.uri
         logger.info(info)
         returndata = ""
         c_cursor = ""
         pre = False
         db = Mydb()
         news = []
         s, max_cursor = db.get('SELECT max(news_cursor) as max_cursor FROM news_detail')
         if s:
             raise Exception("cant get max cursor")
         else:
             max_cursor = max_cursor[0]['max_cursor']
         try:
             c_cursor = int(self.get_argument('cursor'))
             num_news = int(self.get_argument('num'))
         except:
             logger.error('get cursor or num error')
             raise Exception('get cursor or num error')
         if int(max_cursor) <= int(c_cursor):
             returndata = news_to_json(True, max_cursor, [])
         else:
             D_value = int(max_cursor) - int(c_cursor)
             sql_str = 'SELECT news_cursor as n_cursor, news_source as source, news_url as url, news_keywords as keywords, ' \
                       'news_title as title, news_date as date, news_abstract as abstract, news_image as image, news_id as' \
                       ' id FROM news_detail WHERE news_cursor > %d ORDER BY news_cursor LIMIT ' % (c_cursor)
             if D_value < num_news:
                 sql_str += str(D_value)
                 s, news = db.get(sql_str)
                 if s:
                     raise Exception("cant get news data")
                 cursor = news[len(news) - 1]['n_cursor']
                 returndata = news_to_json(True, cursor, news)
             else:
                 sql_str += str(num_news)
                 s, news = db.get(sql_str)
                 if s:
                     raise Exception("cant get max news data")
                 cursor = news[len(news) - 1]['n_cursor']
                 if int(cursor) == int(max_cursor):
                     pre = True
                 returndata = news_to_json(pre, cursor, news)
 #        news = db.query_news()
 #        news = sorted(news, key=lambda obj:obj.get('cursor'), reverse=True)
 #        cursor = news[0]['cursor']
 #        returndata = news_to_json(True, cursor, news)
     except Exception, e:
         logger.error(str(e))
         returndata = json.dumps({'status' : '-100', 'content' : str(e)})
Exemplo n.º 3
0
class tag_url(restful.Resource):
    def __init__(self):
        self.ret = {
            "status": "0",
            "message": ""
        }
        self.db = Mydb()

    def get(self, project, domain, role, tag):
        tag_project = project
        tag_domain = domain
        tag_role = role
        tag_tag = tag
        sql_str = 'SELECT download_url, config_url FROM tag WHERE ' \
                  'project="%s" and domain="%s" and role="%s" and tag="%s"' %(
                  tag_project, tag_domain, tag_role, tag_tag)
        url = self.db.get(sql_str)["message"]
        if not url:
            self.ret["status"] = "-9"
            self.ret["message"] = "can't find the tag"
            return self.ret
        else:
            self.ret["message"] = {
                "download_url" : url[0][0],
                "config_url"   : url[0][1]
            }
        if self.ret["status"] != "0":
            logger.error(self.ret["message"])
            return self.ret
        return self.ret
Exemplo n.º 4
0
class tag_list(restful.Resource):
    def __init__(self):
        self.ret = {
            "status" : 0,
            "message": ""
        }
        self.db = Mydb()

    def get(self, project, domain, branch, role):
        try:
            tag_limit = request.args.get('limit')
            if not tag_limit:
                raise Exception("Miss limit")
            if not tag_limit.isdigit():
                raise Exception("limit is not a digit")
            sql_str = 'SELECT tag,comment, commit_time,commit_user FROM tag ' \
                      'WHERE project="%s" AND domain="%s" AND branch="%s" AND role="%s" ORDER BY commit_time DESC LIMIT %d' % (
                       project, domain, branch, role, int(tag_limit))
            print sql_str
            s, f = self.db.get(sql_str)
            if s:
                logger.error(f)
                raise Exception(str(f))
            self.ret['message'] = f
            for i in range(len(self.ret['message'])):
                self.ret['message'][i]['commit_time'] = self.ret['message'][i]['commit_time'].strftime('%Y-%m-%d %H:%M:%S')
            print self.ret
        except Exception, e:
            logger.error(str(e))
            self.ret["status"] = -400
            self.ret["message"] = str(e)
        finally:
Exemplo n.º 5
0
class tag_url(restful.Resource):
    def __init__(self):
        self.ret = {
            "status": 0,
            "message": ""
        }
        self.db = Mydb()

    def get(self, project, domain,  branch, role, tag):
        try:
            sql_str = 'SELECT download_url, config_url FROM tag WHERE ' \
                      'project="%s" AND domain="%s" AND branch="%s" AND role="%s" AND tag="%s"' %(
                      project, domain, branch, role, tag)
            print sql_str
            s, f = self.db.get(sql_str)
            if s:
                logger.error(f)
                raise Exception(str(f))
            self.ret['message'] = f
            print self.ret
        except Exception, e:
            logger.error(str(e))
            self.ret["status"] = -400
            self.ret["message"] = str(e)
        finally:
Exemplo n.º 6
0
 def get(self, id):
     db = Mydb()
     ret = json.dumps({'status': 0, 'content': 'OK'})
     err = False
     try:
         info = self.request.protocol + "://" + self.request.host + ", method=" + self.request.method + ", access url=" + self.request.uri
         logger.info(info)
         info = "get %s topic" % (str(id))
         logger.info(info)
         sql_str = 'SELECT title, content FROM topic WHERE topic_id=%s' % (str(id))
         s, topic_dict = db.get(sql_str)
         if s or not topic_dict:
             err = True
             raise Exception(str(topic_dict))
         topic_dict = topic_dict[0]
     except Exception, e:
         logger.error(str(e))
         ret = json.dumps({'status': '-120', 'content': str(e)})
Exemplo n.º 7
0
 def get(self):
     try:
         info = self.request.protocol + "://" + self.request.host + ", method=" + self.request.method + ", access url=" + self.request.uri
         logger.info(info)
         ret = ""
         c_cursor = ""
         pre = False
         db = Mydb()
         topics = []
         s, min_cursor = db.get('SELECT min(topic_id) as min_cursor FROM topic')
         if s:
             raise Exception("cant get max cursor")
         else:
             min_cursor = min_cursor[0]['min_cursor']
         try:
             c_cursor = int(self.get_argument('cursor'))
             num_news = int(self.get_argument('num'))
         except:
             logger.error('get cursor or num error')
             raise Exception('get cursor or num error')
         if c_cursor < 1:
             sql_str = 'SELECT topic_id as n_cursor, username as author, title as title, abstract as abstract,' \
                       'commit_time as commit_time FROM topic ORDER BY topic_id DESC LIMIT %s' % (str(num_news))
             s, comments = db.get(sql_str)
             if s:
                 raise Exception("cant get comments data")
             cursor = comments[len(comments) - 1]['n_cursor']
             if int(cursor) == int(min_cursor):
                 pre = True
             ret = topic_to_json(pre, cursor, comments)
         else:
             sql_str = 'SELECT COUNT(*) as nums FROM topic WHERE topic_id<=%d' % (c_cursor)
             s, num = db.get(sql_str)
             if s:
                 raise Exception(num)
             num = num[0]['nums']
             if num < 1:
                 raise Exception("no topics")
             else:
                 sql_str = 'SELECT topic_id as n_cursor, username as author, title as title, abstract as abstract,' \
                           'commit_time as commit_time FROM topic WHERE topic_id<=%d ORDER BY topic_id DESC LIMIT ' % (c_cursor)
                 if int(num) < int(num_news):
                     sql_str += str(num)
                     s, topics = db.get(sql_str)
                     if s:
                         raise Exception("cant get topics data")
                     cursor = topics[len(topics) - 1]['n_cursor']
                     ret = topic_to_json(True, cursor, topics)
                 else:
                     sql_str += str(num_news)
                     s, topics = db.get(sql_str)
                     if s:
                         raise Exception("cant get max topics data")
                     cursor = topics[len(topics) - 1]['n_cursor']
                     if int(cursor) == int(min_cursor):
                         pre = True
                     ret = topic_to_json(pre, cursor, topics)
     except Exception, e:
         logger.error(str(e))
         ret = json.dumps({'status': '-120', 'content': str(e)})
Exemplo n.º 8
0
 def post(self):
     ret = json.dumps({'status': 0, 'content': 'OK'})
     db = Mydb()
     try:
         info = self.request.protocol + "://" + self.request.host + ", method=" + self.request.method + ", access url=" + self.request.uri
         logger.info(info)
         body = self.request.body_arguments
         Args = [
             self_argument('username', required=True, helpinfo="miss user name"),
             self_argument('title', required=True, helpinfo="miss topic title"),
             self_argument('content', required=True, helpinfo="miss topic content"),
         ]
         s, vals = DataIsValid(Args, body)
         if s:
             raise Exception(vals)
         username = vals['username']
         if not(username):
             raise Exception("No username, Maybe not login")
         sql_str = "SELECT * FROM user WHERE username='******'" % (username)
         s, f = db.get(sql_str)
         if s or not(f):
             raise Exception("No this user")
         title = vals['title']
         if len(title) > 100:
             raise Exception("title has too many words")
         content = vals['content']
         if len(content) < 100:
             abstract = content
         else:
             abstract = content[:100]
         date = get_ct()
         sql_str = "INSERT INTO topic VALUES (NULL, %s, %s, %s, %s, %s)"
         data = [username, title, content, abstract, date]
         s, f = db.modify(sql_str, data)
         if s:
            raise Exception(f)
     except Exception, e:
         logger.error(str(e))
         ret = json.dumps({'status': '-120', 'content': str(e)})
Exemplo n.º 9
0
 def post(self):
     ret = json.dumps({'status': 0, 'content': 'OK'})
     db = Mydb()
     try:
         info = self.request.protocol + "://" + self.request.host + ", method=" + self.request.method + ", access url=" + self.request.uri
         logger.info(info)
         body = self.request.body_arguments
         Args = [
             self_argument('username', required=True, helpinfo="miss user name"),
             self_argument('topic_id', required=True, helpinfo="miss topic id"),
             self_argument('content', required=True, helpinfo="miss comment content"),
         ]
         s, vals = DataIsValid(Args, body)
         if s:
             raise Exception(vals)
         username = vals['username']
         if not(username):
             raise Exception("No username, Maybe not login")
         sql_str = "SELECT * FROM user WHERE username='******'" % (username)
         s, f = db.get(sql_str)
         if s or not(f):
             raise Exception("No this user")
         date = get_ct()
         content = vals['content']
         topic_id = vals['topic_id']
         sql_str = "SELECT * FROM topic WHERE topic_id=%s" % (topic_id)
         s, f= db.get(sql_str)
         if s or not(f):
             raise Exception("No this topic")
         sql_str = "INSERT INTO comment VALUES (NULL, %s, %s, %s, %s)"
         data = [username, topic_id, content, date]
         s, f = db.modify(sql_str, data)
         if s:
             raise Exception(f)
     except Exception, e:
         logger.error(str(e))
         ret = json.dumps({'status': '-120', 'content': str(e)})
Exemplo n.º 10
0
class tag_list(restful.Resource):
    def __init__(self):
        self.ret = {
            "status" : "0",
            "message": ""
        }
        self.db = Mydb()

    def get(self, project, domain, role):
        tag_project = project
        tag_domain = domain
        tag_role = role
        tag_limit = request.args.get('limit')
        if (tag_limit is None) and (tag_limit.isdigit()):
            self.ret["status"] = "-8"
            self.ret["message"] = "Miss limit or limit is not a digit"
            return self.ret
        sql_str = 'SELECT tag,comment, commit_time,commit_user FROM tag ' \
                  'WHERE project="%s" and domain="%s" and role="%s" ORDER BY commit_time DESC LIMIT %d' % (
                   tag_project, tag_domain, tag_role, int(tag_limit))
        print sql_str
        mes = self.db.get(sql_str)["message"]
        self.ret["message"] = []
        #将数据库取出的信息转化成字典
        for item in mes:
            item_dict = {
                'tag'           : item[0],
                'comment'       : item[1],
                'commit_time'   : item[2].strftime('%Y-%m-%d %H:%M:%S'),
                'commit_user'   : item[3]
            }
            self.ret["message"].append(item_dict)
        if self.ret["status"] != "0":
            logger.error(self.ret["message"])
            return self.ret
        logger.info('get tags info: %s', self.ret["message"])
        return self.ret
Exemplo n.º 11
0
class branch_list(restful.Resource):
    def __init__(self):
        self.ret = {
            "status" : 0,
            "message": ""
        }
        self.db = Mydb()

    def get(self, project, domain, role):
        try:
            sql_str = 'SELECT DISTINCT branch FROM tag ' \
                      'WHERE project="%s" AND domain="%s" AND role="%s"' % (
                          project, domain, role)
            s, f = self.db.get(sql_str)
            print s, f
            if s:
                logger.error(f)
                raise Exception(str(f))
            print f
            self.ret['message'] = f
        except Exception, e:
            self.ret["status"] = -400
            self.ret["message"] = str(e)
        finally:
Exemplo n.º 12
0
class tag_submit(restful.Resource):
    def __init__(self):
        self.ret = {
            "status": "0",
            "message": ""
        }
        self.db = Mydb()

    def post(self):
        #判断post的值是否正确
        arguments = [
            Arg('tag', type=str, required=True, help='Miss tag'),
            Arg('domain', type=str, required=True, help='Miss domain'),
            Arg('project', type=str, required=True, help='Miss project'),
            Arg('role', type=str, required=True, help='Miss role'),
            Arg('comment', type=str, required=True, help='Miss comment'),
            Arg('commit_time', type=str, required=True, help='Miss commit_time'),
            Arg('commit_user', type=str, required=True, help='Miss commit_user'),
            Arg('download_url', type=str, required=True, help='Miss download_url'),
            Arg('config_url', type=str, required=True, help='Miss config_url'),
            Arg('is_ready', type=str, required=True, help='Miss is_ready'),
        ]
        args = get_parser(arguments).parse_args()
        tag_tag = args['tag']
        tag_domain = args['domain']
        tag_project = args['project']
        tag_role = args['role']
        tag_comment = args['comment']
        tag_commit_time = args['commit_time']
        tag_commit_user = args['commit_user']
        tag_download_url = args['download_url']
        tag_config_url = args['config_url']
        if not(url_is_correct(tag_config_url) and url_is_correct(tag_download_url)):
            self.ret["status"] = "-7"
            self.ret["message"] = "invalid config or download url"
            return self.ret
        tag_is_ready = args['is_ready']
        #判断此tag值是否已经存在
        sql_str = 'SELECT tag FROM tag WHERE tag="%s" and project="%s" and role="%s"' % (
                    tag_tag, tag_project, tag_role)
        exist_tag = self.db.get(sql_str)["message"]
        if exist_tag:
            self.ret["status"] = "-9"
            self.ret["message"] = "tag is exist"
            return self.ret
        ISOTIMEFORMAT = '%Y-%m-%d %H:%M:%S'
        try:
            #将提交时间的字符转转化成datetime格式
            tag_commit_time = datetime.datetime.strptime(tag_commit_time, ISOTIMEFORMAT)
        except Exception, e:
            self.ret["status"] = "-1"
            self.ret["message"] = e.args
            return self.ret
        print tag_commit_time
        sql_str = 'INSERT INTO tag (tag, domain, project, role, comment, commit_time, commit_user, config_url, download_url, is_ready) ' \
                  'VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);'
        params = [tag_tag, tag_domain, tag_project, tag_role, tag_comment, tag_commit_time, tag_commit_user, tag_config_url, tag_download_url, tag_is_ready]
        print params
        self.ret = self.db.modify(sql_str, params)
        if self.ret["status"] != "0":
            logger.error(self.ret["message"])
            return self.ret
        logger.info('insert a tag info: %s', self.ret["message"])
        return self.ret
Exemplo n.º 13
0
class tag_submit(restful.Resource):
    def __init__(self):
        self.ret = {
            "status": 0,
            "message": ""
        }
        self.db = Mydb()

    def post(self):
        #判断post的值是否正确
        try:
            arguments = [
                Arg('tag', type=str, required=True, help='Miss tag'),
                Arg('domain', type=str, required=False, help='Miss domain', default=""),
                Arg('project', type=str, required=True, help='Miss project'),
                Arg('role', type=str, required=True, help='Miss role'),
                Arg('comment', type=str, required=True, help='Miss comment'),
                Arg('commit_time', type=str, required=True, help='Miss commit_time'),
                Arg('commit_user', type=str, required=True, help='Miss commit_user'),
                Arg('download_url', type=str, required=True, help='Miss download_url'),
                Arg('config_url', type=str, required=True, help='Miss config_url'),
                Arg('is_ready', type=str, required=False, help='Miss is_ready', default=""),
                Arg('branch', type=str, required=True, help='Miss is_ready'),
            ]
            args = get_parser(arguments).parse_args()
            tag_tag = args['tag']
            tag_domain = args['domain']
            tag_project = args['project']
            tag_role = args['role']
            tag_comment = args['comment']
            tag_commit_time = args['commit_time']
            tag_commit_user = args['commit_user']
            tag_download_url = args['download_url']
            tag_config_url = args['config_url']
            tag_is_ready = args['is_ready']
            tag_branch = args['branch']
            #判断此tag值是否已经存在
            sql_str = 'SELECT tag FROM tag WHERE tag="%s" and project="%s" and role="%s" and branch="%s"' % (
                        tag_tag, tag_project, tag_role, tag_branch)
            exist_tag, f = self.db.get(sql_str)
            if exist_tag:
                raise Exception("this tag is exist")
            ISOTIMEFORMAT = '%Y-%m-%d %H:%M:%S'
            try:
                #将提交时间的字符转转化成datetime格式
                tag_commit_time = datetime.datetime.strptime(tag_commit_time, ISOTIMEFORMAT)
            except Exception, e:
                raise Exception(str(e))
            print tag_commit_time
            sql_str = 'INSERT INTO tag (tag, domain, project, role, comment, commit_time, commit_user, config_url, download_url, is_ready, branch) ' \
                      'VALUES ("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s");' % (
                tag_tag, tag_domain, tag_project, tag_role, tag_comment, tag_commit_time, tag_commit_user, tag_config_url, tag_download_url, tag_is_ready, tag_branch)
            s, f = self.db.modify(sql_str)
            if s:
                logger.error(f)
                raise Exception(str(f))
            self.ret['message'] = "OK"
            logger.info('insert a tag info: %s', self.ret["message"])
        except Exception, e:
            logger.error(str(e))
            self.ret["status"] = -400
            self.ret["message"] = str(e)
Exemplo n.º 14
0
 def post(self):
     ret = {'status': 0, 'message': 'OK'}
     db = Mydb()
     try:
         upfilepath = ''
         ivecfile = ''
         upload_path = config_get('wavdir', 'sre')
         if not os.path.exists(upload_path):
             os.makedirs(upload_path)
         logger.info('upload path is ' + upload_path)
         ivec_path = config_get('ivectordir', 'sre')
         validframes = int(config_get('validframes', 'sre'))
         if not os.path.exists(ivec_path):
             os.makedirs(ivec_path)
         logger.info('ivec path is ' + ivec_path)
         info = self.request.protocol + '://' + self.request.host + ', method=' + self.request.method + ', access url=' + self.request.uri
         logger.info(info)
         body = self.request.body_arguments
         Args = [
             self_argument('UserId', required=True,
                           helpinfo='miss user id'),
             self_argument('IsSelf',
                           required=True,
                           helpinfo='miss isself symbol')
         ]
         s, vals = DataIsValid(Args, body)
         if s:
             raise Exception(vals)
         userid = vals['UserId']
         isself = vals['IsSelf']
         isself_symbol = False
         if isself == 'yes':
             isself_symbol = True
         elif isself != 'no':
             logger.warning('isself param maybe wrong:' + isself)
         logger.info('userid is ' + userid)
         if not (userid):
             raise Exception('No user id')
         res, userinfo = db.get('SELECT * FROM user WHERE userid="%s"' %
                                (userid))
         if res != 0:
             raise Exception('cannot find user: %s information.' % (userid))
         if len(userinfo) > 0:
             latest_info = sorted(userinfo,
                                  key=lambda x: int(x['timeid']),
                                  reverse=True)[0]
         else:
             raise Exception('%s\'s userinfo is empty' % (userid))
         logger.info('query user %s info:%s' % (userid, str(latest_info)))
         file_metas = self.request.files['UploadFile']
         wav_uuid = uuid.uuid1()
         logger.info('generate uuid:' + str(wav_uuid))
         wavpath = upload_path + '/' + userid
         if not os.path.exists(wavpath):
             os.makedirs(wavpath)
         for meta in file_metas:
             filename = meta['filename']
             upfilepath = wavpath.rstrip('/') + '/' + str(
                 wav_uuid) + '_' + str(isself_symbol) + '_' + filename
             with open(upfilepath, 'wb') as up:
                 up.write(meta['body'])
                 up.close()
                 break
         ivecfile_path = ivec_path.rstrip('/') + '/' + userid + '/'
         if not os.path.exists(ivecfile_path):
             os.makedirs(ivecfile_path)
         ivecfile = ivecfile_path.rstrip('/') + '/' + str(
             wav_uuid) + '.ivec'
         ubm = get_ubm()
         sre_compute = CpuCompute()
         #use str function is to avoid string encode error
         if not sre_compute.Compute(ubm, str(upfilepath), str(ivecfile),
                                    int(validframes)):
             raise Exception('compute ivector failed')
         sre_score = ScorePLDA()
         mean = get_mean()
         if not mean:
             raise Exception('ubm is not inited')
         #get all user enrolling ivecotr
         res, users = db.get(
             'select a.* from user a where timeid=(select max(timeid) from user where userid=a.userid)'
         )
         if res != 0:
             raise Exception(users)
         #use str function is to avoid string encode error
         scores = {}
         for enroll_user in users:
             score = float(
                 sre_score.score_plda(ubm.ubm, str(enroll_user['ivecpath']),
                                      str(ivecfile), str(mean)))
             if not enroll_user['userid'] in scores.keys():
                 scores[enroll_user['userid']] = score
             else:
                 raise Exception("duplicate user score")
         speaker_top3 = sorted(scores.items(),
                               key=lambda x: float(x[1]),
                               reverse=True)[0:3]
         speaker = speaker_top3[0][0]
         speaker_top3_str = str(speaker_top3)
         logger.info("speaker score top3:" + speaker_top3_str)
         ret['message'] = 'speaker is %s' % (speaker)
     except Exception, e:
         logger.error(str(e))
         ret = {'status': -1, 'message': str(e)}
         if os.path.isfile(upfilepath):
             os.remove(upfilepath)
         if os.path.isfile(ivecfile):
             os.remove(ivecfile)
Exemplo n.º 15
0
 def get(self, id):
     try:
         info = self.request.protocol + "://" + self.request.host + ", method=" + self.request.method + ", access url=" + self.request.uri
         logger.info(info)
         ret = ""
         c_cursor = ""
         pre = False
         db = Mydb()
         sql_str = 'SELECT COUNT(*) as nums FROM comment WHERE topic_id=%s' % (str(id))
         s, num = db.get(sql_str)
         if s:
             raise Exception(num)
         num = num[0]['nums']
         if num < 1:
             raise Exception("no comments")
         comments = []
         s, min_cursor = db.get('SELECT min(id) as min_cursor FROM comment WHERE topic_id=%s' % (str(id)))
         if s:
             raise Exception("cant get max cursor")
         else:
             min_cursor = min_cursor[0]['min_cursor']
         try:
             c_cursor = int(self.get_argument('cursor'))
             num_news = int(self.get_argument('num'))
         except:
             logger.error('get cursor or num error')
             raise Exception('get cursor or num error')
         if c_cursor < 1:
             sql_str = 'SELECT id as n_cursor, topic_id as topic_id, username as author, content as content,' \
                       'commit_time as commit_time FROM comment WHERE topic_id=%s ORDER BY id desc LIMIT %s' % (str(id), str(num_news))
             s, comments = db.get(sql_str)
             if s:
                 raise Exception("cant get comments data")
             cursor = comments[len(comments) - 1]['n_cursor']
             if int(cursor) == int(min_cursor):
                 pre = True
             ret = comment_to_json(pre, cursor, comments, num)
         else:
             if int(min_cursor) > int(c_cursor):
                 ret = comment_to_json(True, min_cursor, [], num)
             else:
                 sql_str = 'SELECT id as n_cursor, topic_id as topic_id, username as author, content as content,' \
                           'commit_time as commit_time FROM comment WHERE id<=%s and topic_id=%s ORDER BY id desc LIMIT ' % (str(c_cursor), str(id))
                 if int(num) < int(num_news):
                     sql_str += str(num)
                     s, comments = db.get(sql_str)
                     if s:
                         raise Exception("cant get comments data")
                     cursor = comments[len(comments) - 1]['n_cursor']
                     ret = comment_to_json(True, cursor, comments, num)
                 else:
                     sql_str += str(num_news)
                     s, comments = db.get(sql_str)
                     if s:
                         raise Exception("cant get max comments data")
                     cursor = comments[len(comments) - 1]['n_cursor']
                     if int(cursor) == int(min_cursor):
                         pre = True
                     ret = comment_to_json(pre, cursor, comments, num)
     except Exception, e:
         logger.error(str(e))
         ret = json.dumps({'status': '-120', 'content': str(e)})
Exemplo n.º 16
0
    def post(self):
        ret = {'status': 0, 'message': 'OK'}
        db = Mydb()
        try:
            upfilepath = ''
            ivecfile = ''
            upload_path = config_get('wavdir', 'sre')
            dependent_threshold = float(
                config_get('dependent-threshold', 'sre'))
            independent_threshold = float(
                config_get('independent-threshold', 'sre'))
            if not os.path.exists(upload_path):
                os.makedirs(upload_path)
            logger.info('upload path is ' + upload_path)
            ivec_path = config_get('ivectordir', 'sre')
            validframes = int(config_get('validframes', 'sre'))
            if not os.path.exists(ivec_path):
                os.makedirs(ivec_path)
            logger.info('ivec path is ' + ivec_path)
            info = self.request.protocol + '://' + self.request.host + ', method=' + self.request.method + ', access url=' + self.request.uri
            logger.info(info)
            body = self.request.body_arguments
            Args = [
                self_argument('UserId', required=True,
                              helpinfo='miss user id'),
                self_argument('IsSelf',
                              required=True,
                              helpinfo='miss isself symbol'),
                self_argument('TextRelevance',
                              required=True,
                              helpinfo='miss text relevance symbol')
            ]
            s, vals = DataIsValid(Args, body)
            if s:
                raise Exception(vals)
            userid = vals['UserId']
            isself = vals['IsSelf']
            text_relevance = vals['TextRelevance']
            isself_symbol = False
            if isself == 'yes':
                isself_symbol = True
            elif isself != 'no':
                logger.warning('isself param maybe wrong:' + isself)
            text_relevance_symbol = False
            if text_relevance == 'yes':
                text_relevance_symbol = True
            elif text_relevance != 'no':
                logger.warning('text_relevance param maybe wrong:' +
                               text_relevance)
            logger.info('userid is ' + userid)
            if not (userid):
                raise Exception('No user id')
            res, userinfo = db.get('SELECT * FROM user WHERE userid="%s"' %
                                   (userid))
            if res != 0:
                raise Exception('cannot find user: %s information.' % (userid))
            if len(userinfo) > 0:
                latest_info = sorted(userinfo,
                                     key=lambda x: int(x['timeid']),
                                     reverse=True)[0]
            else:
                raise Exception('%s\'s userinfo is empty' % (userid))
            logger.info('query user %s info:%s' % (userid, str(latest_info)))
            file_metas = self.request.files['UploadFile']
            wav_uuid = uuid.uuid1()
            logger.info('generate uuid:' + str(wav_uuid))
            wavpath = upload_path + '/' + userid
            if not os.path.exists(wavpath):
                os.makedirs(wavpath)
            for meta in file_metas:
                filename = meta['filename']
                upfilepath = wavpath.rstrip('/') + '/' + str(
                    wav_uuid) + '_' + str(isself_symbol) + '_' + filename
                with open(upfilepath, 'wb') as up:
                    up.write(meta['body'])
                    up.close()
                    break
            ivecfile_path = ivec_path.rstrip('/') + '/' + userid + '/'
            if not os.path.exists(ivecfile_path):
                os.makedirs(ivecfile_path)
            ivecfile = ivecfile_path.rstrip('/') + '/' + str(
                wav_uuid) + '.ivec'
            ubm = get_ubm()
            sre_compute = CpuCompute()
            #use str function is to avoid string encode error
            if not sre_compute.Compute(ubm, str(upfilepath), str(ivecfile),
                                       int(validframes)):
                raise Exception('compute ivector failed')
            sre_score = ScorePLDA()
            mean = get_mean()
            if not mean:
                raise Exception('ubm is not inited')
            #use str function is to avoid string encode error
            score = float(
                sre_score.score_plda(ubm.ubm, str(latest_info['ivecpath']),
                                     str(ivecfile), str(mean)))
            res, _ = db.modify(
                'INSERT INTO valid (userid, wavpath, ivecpath, isself, score) values ("%s", "%s", "%s", %s, %f);'
                % (userid, upfilepath, ivecfile, isself_symbol, score))
            if res != 0:
                raise Exception('insert record into user error')
            threshold = 0
            if text_relevance_symbol:
                threshold = dependent_threshold
                ret['message'] = 'Text-dependent valid function, '
            else:
                threshold = independent_threshold
                ret['message'] = 'Text-independent valid function, '
            if score > threshold:
                ret['message'] += 'you are in person, threshold is ' + str(
                    threshold)
            else:
                ret['message'] += 'you are not in person, threshold is ' + str(
                    threshold)
            ret['message'] += ', score is %f' % (score)

        except Exception, e:
            logger.error(str(e))
            ret = {'status': -1, 'message': str(e)}
            if os.path.isfile(upfilepath):
                os.remove(upfilepath)
            if os.path.isfile(ivecfile):
                os.remove(ivecfile)
Exemplo n.º 17
0
 def __init__(self):
     self.ret = {
         "status" : "0",
         "message": ""
     }
     self.db = Mydb()
Exemplo n.º 18
0
    def post(self):
        ret = {'status': 0, 'message': 'OK'}
        db = Mydb()
        try:
            upfilepath = ''
            ivecfile = ''
            upload_path = config_get('wavdir', 'sre')
            if not os.path.exists(upload_path):
                os.makedirs(upload_path)
            logger.info('upload path is ' + upload_path)
            ivec_path = config_get('ivectordir', 'sre')
            validframes = int(config_get('validframes', 'sre'))
            if not os.path.exists(ivec_path):
                os.makedirs(ivec_path)
            logger.info('ivec path is ' + ivec_path)
            info = self.request.protocol + '://' + self.request.host + ', method=' + self.request.method + ', access url=' + self.request.uri
            logger.info(info)
            body = self.request.body_arguments
            Args = [
                self_argument('UserId', required=True, helpinfo='miss user id')
            ]
            s, vals = DataIsValid(Args, body)
            if s:
                raise Exception(vals)
            userid = vals['UserId']
            logger.info('userid is ' + userid)
            if not (userid):
                raise Exception('No user id')
            file_metas = self.request.files['UploadFile']
            wav_uuid = uuid.uuid1()
            logger.info('generate uuid:' + str(wav_uuid))
            wavpath = upload_path + '/' + userid
            if not os.path.exists(wavpath):
                os.makedirs(wavpath)
            for meta in file_metas:
                filename = meta['filename']
                upfilepath = wavpath.rstrip('/') + '/' + str(
                    wav_uuid) + '_' + filename
                with open(upfilepath, 'wb') as up:
                    up.write(meta['body'])
                    up.close()
                    break
            ivecfile_path = ivec_path.rstrip('/') + '/' + userid + '/'
            if not os.path.exists(ivecfile_path):
                os.makedirs(ivecfile_path)
            ivecfile = ivecfile_path.rstrip('/') + '/' + str(
                wav_uuid) + '.ivec'
            ubm = get_ubm()
            sre_compute = CpuCompute()
            #use str function is to avoid string encode error
            if not sre_compute.Compute(ubm, str(upfilepath), str(ivecfile),
                                       int(validframes)):
                raise Exception('compute ivector failed')
            res, userinfo = db.get('SELECT * FROM user WHERE userid="%s";' %
                                   (userid))
            if len(userinfo) > 0:
                logger.warning(
                    'the user %s was enrolled, verification just use latest ivector'
                    % (userid))
            sql_command = 'INSERT INTO user (userid, wavpath, ivecpath) values ("%s", "%s", "%s");' % (
                userid, upfilepath, ivecfile)

            res, _ = db.modify(sql_command)
            if res != 0:
                raise Exception('insert record into user error')
        except Exception, e:
            logger.error(str(e))
            ret = {'status': -1, 'message': str(e)}
            if os.path.isfile(upfilepath):
                os.remove(upfilepath)
            if os.path.isfile(ivecfile):
                os.remove(ivecfile)