Beispiel #1
0
def tw_save_to_tweet_timing_list(task_detail):

    item_detail = dict()

    #item_detail['uid'] = task_detail['uid']
    item_detail['xnr_user_no'] = task_detail['xnr_user_no']
    item_detail['task_source'] = task_detail['tweet_type']
    #item_detail['operate_type'] = task_detail['operate_type']
    item_detail['create_time'] = task_detail['create_time']
    item_detail['post_time'] = task_detail['post_time']
    item_detail['text'] = task_detail['text']
    item_detail['task_status'] = task_detail['task_status']  # 0-尚未发送,1-已发送
    item_detail['remark'] = task_detail['remark']
    #item_detail['task_status'] = 0

    task_id = task_detail['xnr_user_no'] + '_' + str(
        item_detail['create_time'])
    # task_id: uid_提交时间_发帖时间

    try:
        es.index(index=tw_xnr_timing_list_index_name,
                 doc_type=tw_xnr_timing_list_index_type,
                 id=task_id,
                 body=item_detail)
        mark = True
    except:
        mark = False

    return mark
Beispiel #2
0
def tw_save_to_fans_follow_ES(xnr_user_no,uid,follow_type,trace_type):

    try:
        results = es_fb_tw.get(index=fb_xnr_fans_followers_index_name,doc_type=fb_xnr_fans_followers_index_type,\
                id=xnr_user_no)

        results = results["_source"]
    except:
        results = []

    if follow_type == 'follow':
        if trace_type == 'trace_follow':
            try:
                if results:
                    # 添加追随关注
                    try:
                        trace_follow_uids = results['trace_follow_list']
                        trace_follow_uids_set = set(trace_follow_uids)
                        trace_follow_uids_set.add(uid)
                        trace_follow_uids = list(trace_follow_uids_set)
                    except:
                        trace_follow_uids = [uid]

                    # # 添加普通关注
                    # try:
                    #     followers_uids = results['followers_list']
                    #     followers_uids_set = set(followers_uids)
                    #     followers_uids_set.add(uid)
                    #     followers_uids = list(followers_uids_set)
                    # except:
                    #     followers_uids = [uid]
                    
                    # results['followers_list'] = followers_uids
                
                    results['trace_follow_list'] = trace_follow_uids
                    es_fb_tw.update(index=tw_xnr_fans_followers_index_name,doc_type=tw_xnr_fans_followers_index_type,\
                                id=xnr_user_no,body={'doc':results})
                else:
                    results['trace_follow_list'] = [uid]
                    results['followers_list'] = [uid]
                    es_fb_tw.index(index=tw_xnr_fans_followers_index_name,doc_type=tw_xnr_fans_followers_index_type,\
                        body=results,id=xnr_user_no)

            except:
                return False


    elif follow_type == 'unfollow':

        try:
            followers_uids = results['trace_follow_pre_list']
            followers_uids = list(set(followers_uids).difference(set([uid])))
            results['trace_follow_pre_list'] = followers_uids

            es_fb_tw.update(index=tw_xnr_fans_followers_index_name,doc_type=tw_xnr_fans_followers_index_type,\
                        id=xnr_user_no,body={'doc':results})
        except:
            return False

    return True
Beispiel #3
0
def compute_keywords_mark():
    xnr_user_list=lookup_xnr_user_list()
    #xnr_user_list=['TXNR0001']
    print 'xnr_user_list:', xnr_user_list
    now_time=int(time.time()) - DAY
    date_time=ts2datetime(now_time)

    mark_list=[]
    for xnr_user_no in xnr_user_list:
        keywords_task_detail=dict()
        keyword_value_string=json.dumps(xnr_keywords_compute(xnr_user_no))
        keywords_task_detail['keyword_value_string']=keyword_value_string
        keywords_task_detail['xnr_user_no']=xnr_user_no
        #keywords_task_detail['date_time']=date_time
        #keywords_task_detail['timestamp']=datetime2ts(date_time)
        if S_TYPE == 'test':
            keywords_task_id = xnr_user_no + '_' + test_date
            keywords_task_detail['timestamp']=datetime2ts(test_date)
            keywords_task_detail['date_time']=test_date
            print 'keywords_task_detail:', test_date
        else:
            keywords_task_id=xnr_user_no+'_'+date_time
            keywords_task_detail['timestamp']=datetime2ts(date_time)
            keywords_task_detail['date_time']=date_time
            print 'keywords_task_detail:', date_time
        try:
            es_xnr_2.index(index=twitter_keyword_count_index_name,doc_type=twitter_keyword_count_index_type,body=keywords_task_detail,id=keywords_task_id)
            mark=True
        except:
            mark=False
        mark_list.append(mark)
    print 'mark_list:', mark_list
    return mark_list
Beispiel #4
0
def compute_full_keywords():
    now_time = int(time.time())
    date_time = ts2datetime(now_time)

    flow_text_index_name = facebook_flow_text_index_name_pre + date_time

    query_body = {
        'aggs': {
            'keywords': {
                'terms': {
                    'field': 'keywords_string',
                    'size': 1000
                }
            }
        }
    }

    flow_text_exist=es_xnr_2.search(index=flow_text_index_name,doc_type=facebook_flow_text_index_type,\
           body=query_body)['aggregations']['keywords']['buckets']
    word_dict = dict()

    word_dict_new = dict()

    keywords_string = ''
    for item in flow_text_exist:
        word = item['key']
        count = item['doc_count']
        word_dict[word] = count

        keywords_string += '&'
        keywords_string += item['key']

    k_dict = extract_keywords(keywords_string)

    for item_item in k_dict:
        keyword = item_item.word
        # print 'keyword::',keyword,type(keyword)
        if word_dict.has_key(keyword):
            word_dict_new[keyword] = word_dict[keyword]
        else:
            word_dict_new[keyword] = 1
        # print 'count:',word_dict_new[keyword]

    keywords_task_detail = dict()
    keywords_task_detail['date_time'] = date_time
    keywords_task_detail['timestamp'] = datetime2ts(date_time)
    keywords_task_detail['keyword_value_string'] = json.dumps(word_dict_new)
    keywords_task_id = date_time

    try:
        es_xnr_2.index(index=facebook_full_keyword_index_name,
                       doc_type=facebook_full_keyword_index_type,
                       body=keywords_task_detail,
                       id=keywords_task_id)
        mark = True
    except:
        mark = False
    #print word_dict_new
    return mark
Beispiel #5
0
def savedata2es(date, index_pre, index_type, data):
    config = {
        'facebook_feedback_like_':
        ['uid', 'root_uid', 'timestamp', 'text', 'root_text', 'root_mid'],
        'facebook_feedback_comment_': [
            'uid', 'root_uid', 'mid', 'timestamp', 'text', 'root_text',
            'root_mid', 'comment_type'
        ],
        'facebook_feedback_retweet_': [
            'uid', 'root_uid', 'mid', 'timestamp', 'text', 'root_text',
            'root_mid'
        ],
        'facebook_feedback_private_':
        ['uid', 'root_uid', 'timestamp', 'text', 'root_text', 'private_type'],
        'facebook_feedback_friends': ['uid', 'root_uid'],
        'facebook_feedback_at_':
        ['uid', 'root_uid', 'mid', 'timestamp', 'text'],
    }
    if index_pre in [
            'facebook_feedback_at_', 'facebook_feedback_comment_',
            'facebook_feedback_retweet_', 'facebook_feedback_private_',
            'facebook_feedback_like_'
    ]:
        index_name = index_pre + date
        search_index_name = index_pre + '*'
    else:
        index_name = index_pre
        search_index_name = index_name
    for d in data:
        query_body = {
            "query": {
                "filtered": {
                    "filter": {
                        "bool": {
                            "must": []
                        }
                    }
                }
            }
        }
        try:
            for field in config[index_pre]:
                query_body['query']['filtered']['filter']['bool'][
                    'must'].append({'term': {
                        field: d.get(field, '')
                    }})
            query_result = es.search(search_index_name, index_type,
                                     query_body)['hits']['hits']
            if query_result:
                print es.update(index=index_name,
                                doc_type=index_type,
                                body={'doc': d},
                                id=query_result[0]['_id'])
            else:
                print es.index(index_name, index_type, d)
        except Exception, e:
            EXCEPTION += '\n savedata2es Exception: ' + str(e)
Beispiel #6
0
def write_envent_warming(today_datetime,event_warming_content,task_id):
    twitter_event_warning_index_name=twitter_event_warning_index_name_pre+ts2datetime(today_datetime)
    # print 'facebook_event_warning_index_name:',facebook_event_warning_index_name
    #try:
    es_xnr_2.index(index=twitter_event_warning_index_name,doc_type=twitter_event_warning_index_type,body=event_warming_content,id=task_id)
    mark=True
    #except:
    #    mark=False
    return mark
Beispiel #7
0
def create_speech_warning(xnr_user_no,today_datetime):
    #查询关注列表
    lookup_type='followers_list'
    followers_list=lookup_xnr_fans_followers(xnr_user_no,lookup_type)
    
    query_body={
        'query':{
            'filtered':{
                'filter':{
                    'bool':{'must':{'range':{'sensitive':{'gte':1}}}}
                }
            }
        },
        'size':MAX_SEARCH_SIZE,
        'sort':{'sensitive':{'order':'desc'}}
    }
    twitter_flow_text_index_name=get_timets_set_indexset_list(twitter_flow_text_index_name_pre,today_datetime,today_datetime)
    #print twitter_flow_text_index_name
    results=es_xnr_2.search(index=twitter_flow_text_index_name,doc_type=twitter_flow_text_index_type,body=query_body)['hits']['hits']
    #print results
    result=[]
    for item in results:
        if item['_source']['uid'] in followers_list:
            item['_source']['content_type']='follow'
        else:
            item['_source']['content_type']='unfollow'

        item['_source']['validity']=0
        item['_source']['xnr_user_no']=xnr_user_no

        #查询三个指标字段
        tid_result=lookup_tid_attend_index(item['_source']['tid'],today_datetime)
        if tid_result:
            item['_source']['comment']=tid_result['comment']
            item['_source']['share']=tid_result['share']
            item['_source']['favorite']=tid_result['favorite']
        else:
            item['_source']['comment']=0
            item['_source']['share']=0
            item['_source']['favorite']=0 

        #查询用户昵称
        item['_source']['nick_name']=get_user_nickname(item['_source']['uid'])

        task_id=xnr_user_no+'_'+item['_source']['tid']

        #写入数据库
        today_date=ts2datetime(today_datetime)
        twitter_speech_warning_index_name=twitter_speech_warning_index_name_pre+today_date
        # try:
        es_xnr_2.index(index=twitter_speech_warning_index_name,doc_type=twitter_speech_warning_index_type,body=item['_source'],id=task_id)
        mark=True
        # except:
        #     mark=False

        result.append(mark)
    return result
Beispiel #8
0
def create_date_warning(today_datetime):
    query_body={
        'query':{
            'match_all':{}
        },
        'size':MAX_VALUE,
        'sort':{'date_time':{'order':'asc'}}
    }
    try:
        result=es_xnr.search(index=weibo_date_remind_index_name,doc_type=weibo_date_remind_index_type,body=query_body)['hits']['hits']
        date_result=[]
        for item in result:
            #计算距离日期
            date_time=item['_source']['date_time']
            year=ts2yeartime(today_datetime)
            warming_date=year+'-'+date_time
            today_date=ts2datetime(today_datetime)
            countdown_num=(datetime2ts(warming_date)-datetime2ts(today_date))/DAY
        
            if abs(countdown_num) < WARMING_DAY:
                #根据给定的关键词查询预警微博
                # print 'date_time:',date_time
                keywords=item['_source']['keywords']
                date_warming=lookup_facebook_date_warming(keywords,today_datetime)
                item['_source']['facebook_date_warming_content']=json.dumps(date_warming)
                item['_source']['validity']=0
                item['_source']['timestamp']=today_datetime   

                # task_id=str(item['_source']['create_time'])+'_'+str(today_datetime)
                task_id=date_time+'_'+str(today_datetime) 
                #print 'task_id',task_id
                #print 'date_warming',date_warming
                print 'type:',type(item['_source'])
                #写入数据库
                
                facebook_timing_warning_index_name=facebook_timing_warning_index_name_pre+warming_date
                
                if date_warming:
                    print 'facebook_timing_warning_index_name:',facebook_timing_warning_index_name,task_id
                    try:
                        es_xnr_2.index(index=facebook_timing_warning_index_name,doc_type=facebook_timing_warning_index_type,body=item['_source'],id=task_id)
                        mark=True
                    except:
                        mark=False
                else:
                    pass

                date_result.append(mark)
        else:
            pass

    except:
        date_result=[]
    return date_result
Beispiel #9
0
def tw_save_to_xnr_flow_text(tweet_type, xnr_user_no, text, message_type):

    current_time = int(time.time())
    current_date = ts2datetime(current_time)
    xnr_flow_text_index_name = tw_xnr_flow_text_index_name_pre + current_date

    item_detail = {}
    item_detail['uid'] = tw_xnr_user_no2uid(xnr_user_no)
    item_detail['xnr_user_no'] = xnr_user_no
    item_detail['text'] = text
    item_detail['task_source'] = tweet_type
    item_detail['message_type'] = message_type
    #item_detail['topic_field'] = ''
    item_detail['tid'] = ''
    task_id = xnr_user_no + '_' + str(current_time)

    #classify_results = topic_classfiy(classify_mid_list, classify_text_dict)

    try:

        #result = tw_xnr_flow_text_mappings(xnr_flow_text_index_name)

        index_result = es_xnr_2.index(index=xnr_flow_text_index_name,doc_type=tw_xnr_flow_text_index_type,\
                id=task_id,body=item_detail)

        mark = True

    except:
        mark = False

    return mark
def save_results_to_es(xnr_user_no, current_date, sort_item, result):

    item_body = {}
    item_body['xnr_user_no'] = xnr_user_no
    item_body['sort_item'] = sort_item
    item_body['result'] = json.dumps(result)
    item_body['timestamp'] = datetime2ts(current_date)

    _id = xnr_user_no + '_' + sort_item

    index_name = active_social_index_name_pre + current_date

    es.index(index=index_name,
             doc_type=active_social_index_type,
             body=item_body,
             id=_id)
Beispiel #11
0
def update_facebook_xnr_relations(root_uid,
                                  uid,
                                  data,
                                  update_portrait_info=False):
    # pingtaiguanzhu 决定了是否要在平台上真正关注该用户,涉及到更改该关系时,一定要指定该字段(1或0)。
    '''
    :param root_uid: xnr_uid
    :param uid: user_uid
    :param data: relation data. eg: data={'gensuiguanzhu': 1, 'pingtaiguanzhu': 1}
    :param update_portrait_info: update or not
    :return: update success or not
    '''
    xnr_user_no = fb_uid2xnr_user_no(root_uid)
    if xnr_user_no:
        data['platform'] = 'facebook'
        data['xnr_no'] = xnr_user_no
        data['xnr_uid'] = root_uid
        data['uid'] = uid
        '''
        pingtaiguanzhu = data.get('pingtaiguanzhu', -1)
        pingtaiguanzhu_state  = load_pingtaiguanzhu_state(root_uid, uid)
        if pingtaiguanzhu != pingtaiguanzhu_state:
            if pingtaiguanzhu == 1:
                'gaunzhu'
                data['pingtaiguanzhu'] = 1
            elif pingtaiguanzhu == 0:
                'quxiao'
                data['pingtaiguanzhu'] = 0
        '''

        try:
            _id = '%s_%s' % (root_uid, uid)
            user_exist = es_xnr_2.exists(
                index=facebook_xnr_relations_index_name,
                doc_type=facebook_xnr_relations_index_type,
                id=_id)
            if user_exist:
                if update_portrait_info:
                    protrait_info = update_facebook_user_portrait_info(uid)
                    data.update(protrait_info)
                es_result = es_xnr_2.update(
                    index=facebook_xnr_relations_index_name,
                    doc_type=facebook_xnr_relations_index_type,
                    id=_id,
                    body={'doc': data})
            else:
                protrait_info = update_facebook_user_portrait_info(uid)
                data.update(protrait_info)
                es_result = es_xnr_2.index(
                    index=facebook_xnr_relations_index_name,
                    doc_type=facebook_xnr_relations_index_type,
                    id=_id,
                    body=data)
            print es_result
            return True
        except Exception, e:
            print 'update_facebook_xnr_relations Error: ', str(e)
Beispiel #12
0
def push_keywords_task(task_detail):

    #print 'task_detail::',task_detail

    try:
        item_dict = {}
        item_dict['task_id'] = task_detail['task_id']
        item_dict['xnr_user_no'] = task_detail['xnr_user_no']
        keywords_string = '&'.join(task_detail['keywords_string'].encode('utf-8').split(','))
        item_dict['keywords_string'] = keywords_string
        item_dict['compute_status'] = task_detail['compute_status']
        item_dict['submit_time'] = task_detail['submit_time']
        item_dict['submit_user'] = task_detail['submit_user']
        _id = item_dict['xnr_user_no']+'_'+task_detail['task_id']
        es.index(index=fb_hot_keyword_task_index_name,doc_type=fb_hot_keyword_task_index_type,\
                id=_id,body=item_dict)
        mark = True
    except:
        mark = False

    return mark    
Beispiel #13
0
def create_personal_warning(xnr_user_no, today_datetime):
    #查询好友列表
    friends_list = lookup_xnr_friends(xnr_user_no)

    #查询虚拟人uid
    xnr_uid = lookup_xnr_uid(xnr_user_no)

    #计算敏感度排名靠前的用户
    query_body = {
        # 'query':{
        #     'filtered':{
        #         'filter':{
        #             'terms':{'uid':friends_list}
        #         }
        #     }
        # },
        'aggs': {
            'friends_sensitive_num': {
                'terms': {
                    'field': 'uid'
                },
                'aggs': {
                    'sensitive_num': {
                        'sum': {
                            'field': 'sensitive'
                        }
                    }
                }
            }
        },
        'size': MAX_SEARCH_SIZE
    }

    facebook_flow_text_index_name = get_timets_set_indexset_list(
        facebook_flow_text_index_name_pre, today_datetime, today_datetime)

    try:
        first_sum_result=es_xnr_2.search(index=facebook_flow_text_index_name,doc_type=facebook_flow_text_index_type,\
        body=query_body)['aggregations']['friends_sensitive_num']['buckets']
    except:
        first_sum_result = []

    #print 'first_sum_result',first_sum_result
    top_userlist = []
    for i in xrange(0, len(first_sum_result)):
        user_sensitive = first_sum_result[i]['sensitive_num']['value']
        if user_sensitive > 0:
            user_dict = dict()
            user_dict['uid'] = first_sum_result[i]['key']
            friends_mark = judge_user_type(user_dict['uid'], friends_list)
            user_dict['sensitive'] = user_sensitive * friends_mark
            top_userlist.append(user_dict)
        else:
            pass
    #####################
    #如果是好友,则用户敏感度计算值增加1.5倍
    #####################
    #查询敏感用户的敏感内容
    results = []
    for user in top_userlist:
        #print user
        user_detail = dict()
        user_detail['uid'] = user['uid']
        user_detail['user_sensitive'] = user['sensitive']
        user_lookup_id = user['uid']
        print user_lookup_id
        # try:
        #     #user_result=es_xnr.get(index=facebook_feedback_friends_index_name,doc_type=facebook_feedback_friends_index_type,id=user_lookup_id)['_source']
        #     user_result=es_xnr.get(index=facebook_user_index_name,doc_type=facebook_user_index_type,id=user['uid'])['_source']
        #     user_detail['user_name']=user_result['nick_name']
        # except:
        #     user_detail['user_name']=''
        user_detail['user_name'] = get_user_nickname(user['uid'])

        query_body = {
            'query': {
                'filtered': {
                    'filter': {
                        'bool': {
                            'must': [{
                                'term': {
                                    'uid': user['uid']
                                }
                            }, {
                                'range': {
                                    'sensitive': {
                                        'gte': 1
                                    }
                                }
                            }]
                        }
                    }
                }
            },
            'size': MAX_WARMING_SIZE,
            'sort': {
                'sensitive': {
                    'order': 'desc'
                }
            }
        }

        try:
            second_result = es_xnr_2.search(
                index=facebook_flow_text_index_name,
                doc_type=facebook_flow_text_index_type,
                body=query_body)['hits']['hits']
        except:
            second_result = []

        s_result = []
        for item in second_result:
            #查询三个指标字段
            fid_result = lookup_fid_attend_index(item['_source']['fid'],
                                                 today_datetime)
            if fid_result:
                item['_source']['comment'] = fid_result['comment']
                item['_source']['share'] = fid_result['share']
                item['_source']['favorite'] = fid_result['favorite']
            else:
                item['_source']['comment'] = 0
                item['_source']['share'] = 0
                item['_source']['favorite'] = 0
            #查询用户昵称
            item['_source']['nick_name'] = get_user_nickname(
                item['_source']['uid'])

            s_result.append(item['_source'])

        s_result.sort(key=lambda k: (k.get('sensitive', 0)), reverse=True)
        user_detail['content'] = json.dumps(s_result)

        user_detail['xnr_user_no'] = xnr_user_no
        user_detail['validity'] = 0
        user_detail['timestamp'] = today_datetime

        #写入数据库
        today_date = ts2datetime(today_datetime)
        facebook_user_warning_index_name = facebook_user_warning_index_name_pre + today_date

        task_id = xnr_user_no + '_' + user_detail['uid']
        if s_result:
            try:
                es_xnr_2.index(index=facebook_user_warning_index_name,
                               doc_type=facebook_user_warning_index_type,
                               body=user_detail,
                               id=task_id)
                mark = True
            except:
                mark = False
        else:
            pass

        results.append(mark)

    return results
Beispiel #14
0
def read_tracing_followers_tweet():

    if S_TYPE == 'test':
        query_body = {
            'query':{
                'term':{'xnr_user_no':'FXNR0003'}
            },
            'size':MAX_SEARCH_SIZE
        }
        
    else:
        query_body = {
            'query':{
                'match_all':{}
            },
            'size':MAX_SEARCH_SIZE
        }


    results = es_xnr.search(index=fb_xnr_fans_followers_index_name,doc_type=fb_xnr_fans_followers_index_type,\
                body=query_body)['hits']['hits']
    if results:
        for result in results:
            result = result['_source']
            
            xnr_user_no = result['xnr_user_no']
            trace_follow_list = result['trace_follow_list']
            print 'trace_follow_list:::',trace_follow_list

            if S_TYPE == 'test':
                current_time = datetime2ts(S_DATE_FB)
                #trace_follow_list = TRACE_FOLLOW_LIST
            else:
                current_time = int(time.time())

            current_date = ts2datetime(current_time)

            flow_text_index_name = facebook_flow_text_index_name_pre + current_date

            query_body_flow = {
                'query':{
                    'filtered':{
                        'filter':{
                            'terms':{'uid':trace_follow_list}
                        }
                    }
                },
                'size':MAX_SEARCH_SIZE
            }

            results_flow = es_xnr.search(index=flow_text_index_name,doc_type=flow_text_index_type,\
                            body=query_body_flow)['hits']['hits']

            if results_flow:
                for result_flow in results_flow:
                    
                    result_flow = result_flow['_source']
                    fid = result_flow['fid']

                    #先判断 之前是否已经存过该fid
                    
                    task_id = xnr_user_no + '_' + fid
                    try:
                        # 如果已添加则跳过
                        es_xnr.get(index=fb_xnr_retweet_timing_list_index_name,doc_type=\
                            fb_xnr_retweet_timing_list_index_type,id=task_id)['_source']
                        continue

                    except:
                        # 如果未添加过则加入列表
                        task_detail = {}
                        task_detail['xnr_user_no'] = xnr_user_no
                        task_detail['fid'] = fid
                        task_detail['text'] = result_flow['text']
                        task_detail['uid'] = result_flow['uid']
                        task_detail['nick_name'],task_detail['photo_url'] = fb_uid2nick_name_photo(result_flow['uid'])
                        task_detail['timestamp'] = result_flow['timestamp']
                        task_detail['timestamp_set'] = result_flow['timestamp'] + random.randint(RETWEET_START_TS,RETWEET_END_TS)
                        task_detail['compute_status'] = 0

                        es_xnr.index(index=fb_xnr_retweet_timing_list_index_name,doc_type=\
                            fb_xnr_retweet_timing_list_index_type,body=task_detail,id=task_id)
Beispiel #15
0
    ts = datetime2ts(date)
    fb_xnr_fans_followers_mappings()
    friend = FriendExist(xnr_info['account'], xnr_info['password'])
    lis = friend.get_friend_exist()
    # {'uid', 'nick_name', 'photo_url'}
    try:
        new_friends_list = [item['uid'] for item in lis]
    except Exception, e:
        EXCEPTION += '\n friends_exist Exception: ' + str(e)
    try:
        friends_list = es.get(index=fb_xnr_fans_followers_index_name,
                              doc_type=fb_xnr_fans_followers_index_type,
                              id=xnr_user_no)['_source']['fans_list']
    except:
        print es.index(fb_xnr_fans_followers_index_name,
                       fb_xnr_fans_followers_index_type,
                       body={'fans_list': []},
                       id=xnr_user_no)
        friends_list = []

    if not new_friends_list == friends_list:
        print es.update(index=fb_xnr_fans_followers_index_name,
                        doc_type=fb_xnr_fans_followers_index_type,
                        body={'doc': {
                            'fans_list': new_friends_list
                        }},
                        id=xnr_user_no)


def main():
    xnr_info_list = load_xnr_info()
    date = ts2datetime(time.time())
Beispiel #16
0
def get_trace_follow_operate(xnr_user_no, uid_string, nick_name_string):

    mark = False
    fail_nick_name_list = []
    if uid_string:
        uid_list = uid_string.encode('utf-8').split(',')

    elif nick_name_string:
        nick_name_list = nick_name_string.encode('utf-8').split(',')
        uid_list = []

        for nick_name in nick_name_list:
            query_body = {
                'query': {
                    'filtered': {
                        'filter': {
                            'term': {
                                'nick_name': nick_name
                            }
                        }
                    }
                },
                '_source': ['uid']
            }
            try:
                uid_results = es.search(index=twitter_user_index_name,doc_type=twitter_user_index_type,\
                            body=query_body)['hits']['hits']

                uid_result = uid_result[0]['_source']
                uid = uid_result['uid']
                uid_list.append(uid)

            except:
                fail_nick_name_list.append(nick_name)

    try:
        result = es.get(index=tw_xnr_fans_followers_index_name,doc_type=tw_xnr_fans_followers_index_type,\
                        id=xnr_user_no)['_source']

        try:
            trace_follow_list = result['trace_follow_list']
        except:
            trace_follow_list = []

        try:
            followers_list = result['followers_list']
        except:
            followers_list = []

        trace_follow_list = list(set(trace_follow_list) | set(uid_list))

        followers_list = list(set(followers_list) | set(uid_list))

        es.update(index=tw_xnr_fans_followers_index_name,doc_type=tw_xnr_fans_followers_index_type,\
                    id=xnr_user_no,body={'doc':{'trace_follow_list':trace_follow_list,'followers_list':followers_list}})

        mark = True

    except:

        item_exists = {}

        item_exists['xnr_user_no'] = xnr_user_no
        item_exists['trace_follow_list'] = uid_list
        item_exists['followers_list'] = uid_list

        es.index(index=tw_xnr_fans_followers_index_name,doc_type=tw_xnr_fans_followers_index_type,\
                    id=xnr_user_no,body=item_exists)

        mark = True

    return [mark, fail_nick_name_list]
Beispiel #17
0
def update_twitter_xnr_relations(root_uid,
                                 uid,
                                 data,
                                 update_portrait_info=False):
    # pingtaiguanzhu 决定了是否要在平台上真正关注该用户,涉及到更改该关系时,一定要指定该字段(1或0)。
    '''
    :param root_uid: xnr_uid
    :param uid: user_uid
    :param data: relation data. eg: data={'gensuiguanzhu': 1, 'pingtaiguanzhu': 1}
    :param update_portrait_info: update or not
    :return: update success or not
    '''
    xnr_user_no = tw_uid2xnr_user_no(root_uid)
    if xnr_user_no:
        data['platform'] = 'twitter'
        data['xnr_no'] = xnr_user_no
        data['xnr_uid'] = root_uid
        data['uid'] = uid
        '''
        pingtaiguanzhu = data.get('pingtaiguanzhu', -1)
        pingtaiguanzhu_state  = load_pingtaiguanzhu_state(root_uid, uid)
        if pingtaiguanzhu != pingtaiguanzhu_state:
            if pingtaiguanzhu == 1:
                'gaunzhu'
                data['pingtaiguanzhu'] = 1
            elif pingtaiguanzhu == 0:
                'quxiao'
                data['pingtaiguanzhu'] = 0
        '''
        # kn push jinrong cloud
        pingtaiguanzhu = data.get('pingtaiguanzhu', -1)
        pingtaiguanzhu_state = load_twitter_pingtaiguanzhu_state(root_uid, uid)
        tw_account, tw_password = load_twitter_user_passwd(root_uid)
        params_dict = {}
        params_dict['account'] = tw_account
        params_dict['password'] = tw_password
        params_dict['to_id'] = uid
        params_dict['xnr_user_no'] = xnr_user_no
        params_dict['root_uid'] = root_uid
        # 根据screen_name关注

        if pingtaiguanzhu != pingtaiguanzhu_state:
            if pingtaiguanzhu == 1:
                # 'gaunzhu'
                params_dict['operate_type'] = 'follow'
                data['pingtaiguanzhu'] = 1
                ali_re.lpush(twitter_relation_params, json.dumps(params_dict))
                print params_dict
                print "push aliyun successful"
            elif pingtaiguanzhu == 0:
                # 'quxiao'
                params_dict['operate_type'] = 'unfollow'
                data['pingtaiguanzhu'] = 0
                ali_re.lpush(twitter_relation_params, json.dumps(params_dict))
                print "push aliyun successful"
        else:
            #params_dict['operate_type'] = 'follow'
            #data['pingtaiguanzhu'] = 1
            #ali_re.lpush(twitter_relation_params, json.dumps(params_dict))
            #print "push aliyun successful"
            pass

        try:
            _id = '%s_%s' % (root_uid, uid)
            user_exist = es_xnr_2.exists(
                index=twitter_xnr_relations_index_name,
                doc_type=twitter_xnr_relations_index_type,
                id=_id)
            if user_exist:
                if update_portrait_info:
                    protrait_info = update_twitter_user_portrait_info(uid)
                    data.update(protrait_info)
                es_result = es_xnr_2.update(
                    index=twitter_xnr_relations_index_name,
                    doc_type=twitter_xnr_relations_index_type,
                    id=_id,
                    body={'doc': data})
            else:
                protrait_info = update_twitter_user_portrait_info(uid)
                data.update(protrait_info)
                es_result = es_xnr_2.index(
                    index=twitter_xnr_relations_index_name,
                    doc_type=twitter_xnr_relations_index_type,
                    id=_id,
                    body=data)
            return True
        except Exception, e:
            print 'update_twitter_xnr_relations Error: ', str(e)