Esempio n. 1
0
def post_article():
    """
    发表新文章后,首先调用该函数进行文章分析和数据库存入
    :return:
    """
    try:
        jdata = json.loads(request.data)
        media_id = jdata['media_id']
        items = jdata['items']
        update_time = jdata['update_time']
        # admin_id = jdata['admin_id']
        for item in items:
            article_title = item['article_title']
            article_content = item['article_content']
            article_thumb_id = item['article_thumb_id']
            article_id = hashlib.md5(media_id + article_thumb_id).hexdigest()
            article_url = item['article_url']
            article_post_user = item['article_post_user']
            article_post_date = update_time
            a_topiclist = tagging_utils.passage_second_level_classify(article_content)
            atags = {}
            for topic in a_topiclist:
                atags[topic['topic_tag']] = topic['topic_prob']
            article = Article(a_id=article_id, a_title=article_title, post_user=article_post_user,
                              post_date=article_post_date, a_tags=atags, a_url=article_url, a_content=article_content)
            DAO_utils.mongo_insert_article(article)
        resp = make_response(json.dumps({'code': 0, 'msg': 'success'}), 200)
    except KeyError, ke:
        print ke
        resp = make_response(
            json.dumps({'code': 103,
                        'msg': 'request key error, details=%s' % str(ke)}), 500)
Esempio n. 2
0
def update_a_u_map(reaction_list, a_u_map, insert_rate=0.001):
    """
    根据一段时间的交互记录,更新文章tag到用户tag之间的映射权重值
    :param reaction_list:
    :param a_u_map:
    :param insert_rate:
    :return:
    """
    # TODO 更顺畅的权值更新公式
    for reaction in reaction_list:
        # process article tags
        reaction_a_id = reaction.reaction_a_id
        article = DAO_utils.get_article_by_id(reaction_a_id)
        article_tags = article.atags
        # get user tags
        reaction_user_id = reaction.reaction_user_id
        user = DAO_utils.get_user_by_id(reaction_user_id)
        utag_vec = user.user_tag_score_vec

        reaction_type = reaction.reaction_type
        reaction_date = reaction.reaction_date
        for atag_key in article_tags:
            # 采用稀释的方式改变映射权值
            atag_value = article_tags[atag_key]  # 文章本身的tag权值,用于衡量该篇文章的属性,0~1之间的值。
            # 首先稀释原有的权值
            a_u_inst = a_u_map[atag_key]
            for ukey in a_u_inst:
                a_u_inst[ukey] *= (1 - insert_rate)
            # 然后insert用户的tags
            for utag_key in utag_vec:
                if utag_key in a_u_inst:
                    a_u_inst[utag_key] += utag_vec[utag_key] * insert_rate * atag_value
                else:
                    a_u_inst[utag_key] = utag_vec[utag_key] * insert_rate * atag_value
        return a_u_map
Esempio n. 3
0
def record_reactions():
    """
    用于记录交互行为
    :return:
    """
    try:
        user_id = request.args['openid']
        media_id = request.args['media_id']
        thumb_id = request.args['thumb_id']
        article_id = hashlib.md5(media_id + thumb_id).hexdigest()
        article = DAO_utils.mongo_get_article(article_id)
        redirect_url = article.a_url
        # if code:
        #     token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token'
        #     raw_auth_result = requests.get(token_url, {'appid': APPID, 'secret': SECRET, 'code': code})
        #     auth_result = json.loads(raw_auth_result)
        #     user_id = auth_result.get('openid')
        reaction_id = hashlib.md5(user_id + article_id + str(time.time())).hexdigest()
        reaction = Reaction(reaction_id=reaction_id, reaction_type='read', reaction_a_id=article_id,
                            reaction_user_id=user_id,
                            reaction_date=datetime.datetime.utcnow())
        # todo db连接是否需要长期保持?
        DAO_utils.mongo_insert_reactions(reaction)
        # else:
        #     print 'user did not agree to auth'
        return redirect(redirect_url)
    except Exception, e:
        print e
        return make_response(json.dumps({'code': 1, 'msg': str(e)}), 500)
Esempio n. 4
0
def start_user_tagging():
    """
    根据所有未标注过的交互记录,对用户进行tagging
    :return:
    """
    try:
        reaction_list = DAO_utils.mongo_get_reactions()
        reaction_type_weight = DAO_utils.mongo_get_reaction_type_weight()
        a_u_map = DAO_utils.mongo_get_a_u_tagmap()
        count = tagging_utils.user_tagging_by_reactionlist(reaction_list, reaction_type_weight, a_u_map)
        return json.dumps({'code': 0, 'msg': 'handled %s reactions' % count})
    except Exception, e:
        return json.dumps({'code': 1, 'msg': 'unknown error, details = %s' % str(e)})
Esempio n. 5
0
def new_user():
    """
    每当有用户新关注后,回调该函数,将用户信息存入
    :return:
    """
    try:
        jdata = json.loads(request.data)
        user_id = jdata['openid']
        wechat_user = WechatUser(user_id)
        DAO_utils.mongo_insert_user(wechat_user)
        resp = make_response(json.dumps({'code': 0, 'msg': 'success'}), 200)
    except KeyError, ke:
        print ke
        resp = make_response(
            json.dumps({'code': 103,
                        'msg': 'request key error, details=%s' % str(ke)}), 500)
Esempio n. 6
0
def get_all_taglist():
    try:
        taglist = DAO_utils.mongo_get_all_taglist()
        return json.dumps({'code': 0, 'tagList': taglist}, ensure_ascii=False)

    except Exception, e:
        print e
        return json.dumps({'code': 1, 'msg': 'unknown error, details = %s' % str(e)})
Esempio n. 7
0
def get_openidlist_by_tag():
    try:
        req_data = json.loads(request.data)
        taglist = req_data['tagList']
        openid_list = DAO_utils.mongo_get_openid_by_tags(taglist)
        return json.dumps({'code': 0, 'openid_list': openid_list})
    except Exception, e:
        print e
        return json.dumps({'code': 1, 'msg': 'unknown error, details = %s' % str(e)})
Esempio n. 8
0
def tagging_by_url():
    try:
        req_data = json.loads(request.data)
        user_id = req_data['user_id']
        admin_id = req_data['admin_id']
        article_url = req_data['article_url']
        reaction_type_weight = DAO_utils.mongo_get_reaction_type_weight()

        result = tagging_utils.user_tagging_by_url(article_url=article_url, admin_id=admin_id, user_id=user_id,
                                                   reaction_type_weight=reaction_type_weight)
        resp = make_response(json.dumps(result), 200)
    except Exception, e:
        print e
        resp = make_response(json.dumps({'code': 1, 'msg': 'unknown error, details = %s' % str(e)}), 400)
Esempio n. 9
0
def get_article(article_id):
    """
    根据文章id获取文章信息
    :return:
    """
    try:
        # params = request.args
        # article_id = params['article_id']
        inst_article = DAO_utils.mongo_get_article(article_id)
        json_article = inst_article.get_json_object()
        resp = make_response(json.dumps({'code': 0, 'article': json_article}), 200)
    except KeyError, ke:
        print ke
        resp = make_response(
            json.dumps({'code': 103,
                        'msg': 'request key error, details=%s' % str(ke)}), 500)
Esempio n. 10
0
def config_reaction_type_weight(reaction_type_weight):
    if type(reaction_type_weight) is dict:
        DAO_utils.mongo_set_conf('reaction_type_weight', reaction_type_weight, is_overwrite=True)
    else:
        raise TypeError('reaction_type_weight should be a dict!')
Esempio n. 11
0
def config_a_u_map(a_u_map):
    if type(a_u_map) is dict:
        DAO_utils.mongo_set_conf('a_u_tagmap', a_u_map)
    else:
        raise TypeError('a_u_map should be a dict!')