def bulk_del_blackword(shop_id, word_list, camp_id = None, adg_id_list = None, opter = 1, opter_name = ''): del_id_list = [] if (not camp_id) and (not adg_id_list): return del_id_list word_list_escaped = [] for word in word_list: for sign in Const.COMMON_RE_SIGN: word = word.replace(sign, '\%s' % sign) word_list_escaped.append(word) start_time = date_2datetime(datetime.date.today() - datetime.timedelta(15)) filter_dict = {'shop_id':shop_id, '$or':[{'word':re.compile(word)} for word in word_list_escaped], 'rpt_list':{'$not':{'$elemMatch':{'date':{'$gte':start_time}, '$or':[{'directpaycount':{'$gt':0}}, {'indirectpaycount':{'$gt':0}}] }}} } if adg_id_list: filter_dict.update({'adgroup_id':{'$in':adg_id_list}}) elif camp_id: filter_dict.update({'campaign_id':camp_id}) else: return del_id_list kw_cursor = kw_coll.find(filter_dict, {'campaign_id':1, 'adgroup_id':1, '_id':1, 'word':1}) kw_arg_dict = {} for keyword in kw_cursor: campaign_id, adgroup_id, keyword_id, word = keyword['campaign_id'], keyword['adgroup_id'], keyword['_id'], keyword['word'] if not kw_arg_dict.has_key(campaign_id): kw_arg_dict[campaign_id] = [] kw_arg_list = kw_arg_dict[campaign_id] kw_arg_list.append([adgroup_id, keyword_id, word, 0, 0, '包含屏蔽词']) for campaign_id, kw_arg_list in kw_arg_dict.items(): sub_del_id_list = delete_keywords(shop_id = shop_id, campaign_id = campaign_id, kw_arg_list = kw_arg_list, data_type = 404, opter = opter, opter_name = opter_name) del_id_list.extend(sub_del_id_list) return del_id_list
def get_garbage_words(shop_id): '''获取整店所有无展现词 (无展现词定义:连续15天展现为0且未激活)''' start_time = date_2datetime(datetime.datetime.now() - datetime.timedelta(days = 15)) result = [] mnt_campid_list = MntMnger.get_longtail_camp_ids(shop_id = shop_id) kw_cur = kw_coll.find({'shop_id': shop_id, 'is_garbage':True, 'campaign_id':{'$ne':mnt_campid_list}}, {'campaign_id':1, 'adgroup_id':1, 'word':1, 'rpt_list': {'$slice':-15}}) for kw in kw_cur: impr = 0 if kw.has_key('rpt_list'): for rpt in kw['rpt_list']: if rpt['date'] >= start_time: impr += rpt['impressions'] if impr == 0: result.append({'kw_id':kw['_id'], 'word':kw['word'], 'camp_id':kw['campaign_id'], 'adg_id':kw['adgroup_id']}) return result
def filter_dupl_words(shop_id, condition, word_list): '''获取一批重复词按条件过滤后的所有关键词信息''' del_day, del_level, statistics_type, del_offline = condition['del_day'], condition['del_level'], condition['del_statistics_type'], condition['del_offline'] mnt_campid_list = MntMnger.get_longtail_camp_ids(shop_id = shop_id) if not word_list: return [] kw_list = list(kw_coll.find({'shop_id': shop_id, 'campaign_id':{'$ne':mnt_campid_list}, 'word': {'$in':word_list}}, {'word':1, 'qscore':1, 'campaign_id':1, 'adgroup_id':1, 'rpt_list': {'$slice':-del_day}}).sort('word')) if del_offline: camp_id_list, adg_id_list = [], [] for kw in kw_list: camp_id_list.append(kw['campaign_id']) adg_id_list.append(kw['adgroup_id']) camp_cur = camp_coll.find({'_id':{'$in':camp_id_list}}, {'online_status':1}) camp_status_dict = {camp['_id']:camp['online_status'] for camp in camp_cur} adg_cur = adg_coll.find({'shop_id':shop_id, '_id':{'$in':adg_id_list}}, {'online_status':1}) adg_status_dict = {adg['_id']:adg['online_status'] for adg in adg_cur} kw_list.append({'word':None}) temp_word, temp_list, result, count = '', [], [], 0 start_time = date_2datetime(datetime.datetime.now() - datetime.timedelta(days = del_day)) for kw in kw_list: # 当一个重复词遍历完后,排序,过滤后加入到result, 并初始化temp_word, temp_list if temp_word != kw['word']: temp_list = sorted(temp_list, key = itemgetter('ctr', 'impr', 'qscore'), reverse = True) # 按点击率、展现量、质量得分降序排序 count = 0 for obj in temp_list: count += 1 if count > del_level and (statistics_type == '0' or obj[statistics_type] == 0) and (del_offline == 0 or obj['camp_status'] == obj['adg_status'] == 'online'): result.append({'kw_id':obj['kw_id'], 'word':temp_word, 'camp_id':obj['camp_id'], 'adg_id':obj['adg_id']}) temp_word, temp_list = kw['word'], [] # 将当前词的相关信息存入 temp_list if kw['word']: impr, click = 0, 0 if del_offline and kw.has_key('rpt_list'): for rpt in kw['rpt_list']: if rpt['date'] >= start_time: impr += rpt['impressions'] click += rpt['click'] temp_list.append({'kw_id': kw['_id'], 'impr': impr, 'click': click, 'camp_id': kw['campaign_id'], 'adg_id': kw['adgroup_id'], 'qscore':kw['qscore'], 'ctr': impr and click / impr, 'adg_status':del_offline and adg_status_dict[kw['adgroup_id']] or '', 'camp_status': del_offline and camp_status_dict[kw['campaign_id']] or '', }) return result
def kw_list(self): if not hasattr(self, '_kw_list'): kw_list = list( kw_coll.find({ 'shop_id': self.shop_id, 'campaign_id': self.campaign_id, 'adgroup_id': self.adgroup_id, 'mnt_opt_type': { '$ne': 2 }, '_id': { '$in': self.kw_rtrpt_dict.keys() } })) self._kw_list = trans_batch_dict_2document(src_dict_list=kw_list, class_object=Keyword) return self._kw_list
def get_dupl_word_info(shop_id, last_day, word): '''获取一个重复词的所有关键词信息''' mnt_campid_list = MntMnger.get_longtail_camp_ids(shop_id = shop_id) kw_list = list(kw_coll.find({'shop_id': shop_id, 'word': word, 'campaign_id':{'$nin':mnt_campid_list}}, {'rpt_list':{'$slice':-last_day}})) camp_id_list, adg_id_list = [], [] for kw in kw_list: camp_id_list.append(kw['campaign_id']) adg_id_list.append(kw['adgroup_id']) camp_cur = camp_coll.find({'_id':{'$in':camp_id_list}}, {'title':1}) camp_dict = {camp['_id']:camp['title'] for camp in camp_cur} adg_cur = adg_coll.find({'shop_id':shop_id, '_id':{'$in':adg_id_list}}, {'item_id':1}) adg_dict = {adg['_id']:adg['item_id'] for adg in adg_cur} item_cur = item_coll.find({'shop_id':shop_id, '_id':{'$in':adg_dict.values()}}, {'title':1, 'pic_url':1, 'price':1}) item_dict = {item['_id']:(item['title'], item['price'], item['pic_url']) for item in item_cur } result = [] start_time = date_2datetime(datetime.datetime.now() - datetime.timedelta(days = last_day)) no_item_tuple = ('该宝贝可能不存在或者下架,请尝试同步数据', 0, '/site_media/jl/img/no_photo') # 当item不存在时,使用默认 for kw in kw_list: cost, impr, click, pay = 0, 0, 0, 0 if kw.has_key('rpt_list'): for rpt in kw['rpt_list']: if rpt['date'] >= start_time: cost += rpt['cost'] impr += rpt['impressions'] click += rpt['click'] pay += rpt['directpay'] + rpt['indirectpay'] temp_key = kw['adgroup_id'] if temp_key in adg_dict: item_id = adg_dict[kw['adgroup_id']] temp_item_info = item_dict.get(item_id, no_item_tuple) result.append({'kw_id':kw['_id'], 'max_price': kw['max_price'], 'qscore': kw['qscore'], 'impr': impr, 'click': click, 'pay': pay, 'cpc': click and cost / click, 'ctr': impr and click / impr, 'camp_title': camp_dict[kw['campaign_id']], 'item_title': temp_item_info[0], 'item_price':temp_item_info[1], 'pic_url': temp_item_info[2], 'item_id':item_id }) result = sorted(result, key = itemgetter('ctr', 'impr', 'qscore', 'max_price'), reverse = True) # 按点击率、展现量、质量得分、出价降序排序 return result