dou_yin_keywords_search.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import json
  2. from typing import List, Any, Optional
  3. from simpleeval import simple_eval
  4. from client.CrawlerClient import CrawlerClient
  5. from model.automation_provide_job import DouYinSearchConfig, ChannelSearchAndDetailDTO, SearchFilterConfigItem
  6. from util.automation_provide_util import AutoProvideUtil
  7. crawler_client = CrawlerClient()
  8. preFilterThreshold = 3
  9. result_txt_file = '/Users/zhao/Desktop/tzld/文档/分析文档/关键词分析_20260204_1.txt'
  10. def write_result_file(content, mode='a+'):
  11. with open(result_txt_file, mode) as f:
  12. f.write(content)
  13. f.write("\n")
  14. def log_info_print_title():
  15. write_result_file(
  16. "视频ID,品类,关键词,爬取计划,结果,原因,搜索使用的账号ID,排序方式,站外视频ID,站外账号ID,过滤结果,分享量,点赞量,分享量/点赞量,视频时长(秒),观众年龄50+占比,观众年龄50+TGI,过滤规则表达式", 'w')
  17. def log_info_print(log_json: dict[str, Any], account_id: Optional[str] = None):
  18. if 'ext' in log_json and isinstance(log_json['ext'], dict):
  19. log_json['ext'] = json.dumps(log_json['ext'], ensure_ascii=False)
  20. if 'modelValueConfig' in log_json and isinstance(log_json['modelValueConfig'], dict):
  21. log_json['modelValueConfig'] = json.dumps(log_json['modelValueConfig'], ensure_ascii=False)
  22. video_id = log_json["videoId"]
  23. keywords = log_json['keywords']
  24. crawler_plan_id = log_json.get("crawlerPlanId", "")
  25. result = log_json.get("result", False)
  26. reason = log_json.get("reason", "")
  27. merge_cate2 = log_json['mergeSecondLevelCate']
  28. sort_type = json.loads(log_json.get("modelValueConfig", "{}")).get("sortType")
  29. ext_json = json.loads(log_json.get("ext", "{}"))
  30. account_id = account_id if account_id else 0
  31. if not ext_json:
  32. write_result_file(f"{video_id},{merge_cate2},{keywords},'{crawler_plan_id},'{result},{reason},{account_id},{sort_type}")
  33. return
  34. for channel_content_id in ext_json:
  35. if channel_content_id in ['mergeCate2Map']:
  36. continue
  37. channel_ext_info = ext_json[channel_content_id]
  38. filter_result = channel_ext_info.get("result", False)
  39. rule_str = channel_ext_info.get("rule", "")
  40. rule_context = channel_ext_info.get('ruleContext', {})
  41. share_cnt = rule_context.get('shareCnt', 0)
  42. video_duration_s = rule_context.get('videoDuration_s', 0)
  43. like_cnt = rule_context.get('likeCnt', 0)
  44. audience_age_50_rate = rule_context.get('audienceAge50Rate', 0)
  45. audience_age_50_tgi = rule_context.get('audienceAge50TGI', 0)
  46. share_div_link = rule_context.get('shareDivLink', 0)
  47. channel_account_id = ""
  48. if "contentDetail" in channel_ext_info:
  49. channel_account_id = channel_ext_info["contentDetail"].get("channelAccountId")
  50. elif "fanPortrait" in channel_ext_info:
  51. channel_account_id = channel_ext_info["fanPortrait"].get("channelAccountId")
  52. write_result_file(f"{video_id},{merge_cate2},{keywords},'{crawler_plan_id},'{result},{reason},{account_id},{sort_type},'{channel_content_id},{channel_account_id},{filter_result},"
  53. f"{share_cnt},{like_cnt},{share_div_link},{video_duration_s},{audience_age_50_rate},{audience_age_50_tgi},{rule_str}")
  54. def keywords_search(keywords: str, sort_type: str, account: Optional[str] = None) -> List[ChannelSearchAndDetailDTO]:
  55. search_config = DouYinSearchConfig(
  56. search_content=keywords,
  57. sort_type=sort_type,
  58. account_id=account,
  59. )
  60. return crawler_client.dou_yin_keywords_search(search_config, True, True)
  61. def eval_expr(expr: str, context: dict) -> bool:
  62. expr = expr.replace("&&", " and ").replace("||", " or ")
  63. return bool(simple_eval(expr, names=context))
  64. def keywords_search_and_filter(keywords: str, sort_type: str, account_id: str, log_json: dict[str, Any], filters: List[SearchFilterConfigItem]) -> dict[str, Any]:
  65. need_copy_keys = ["videoId", "accountFilters", "contentFilters", "mergeSecondLevelCate", "keywords"]
  66. result_json = {}
  67. for key in need_copy_keys:
  68. result_json[key] = log_json.get(key)
  69. log_ext_info = {}
  70. result_json['ext'] = log_ext_info
  71. result_json['result'] = True
  72. result_json['modelValueConfig'] = {"sortType": sort_type}
  73. rule_str = AutoProvideUtil.parse_filter_config_to_rule_str(filters)
  74. channel_search_and_detail_dtos = keywords_search(keywords, sort_type, account_id)
  75. if not channel_search_and_detail_dtos:
  76. result_json["result"] = False
  77. result_json['reason'] = '关键词搜索结果为空'
  78. return result_json
  79. cnt = 0
  80. for channel_search_and_detail_dto in channel_search_and_detail_dtos:
  81. channel_content_id = channel_search_and_detail_dto.channel_content_id
  82. channel_account_id = channel_search_and_detail_dto.channel_account_id
  83. content_detail = channel_search_and_detail_dto.content_detail
  84. fans_portrait = channel_search_and_detail_dto.fans_portrait
  85. ext_json = {}
  86. log_ext_info[channel_content_id] = ext_json
  87. if content_detail:
  88. content_detail['channelAccountId'] = channel_account_id
  89. content_detail['channelContentId'] = channel_content_id
  90. ext_json['contentDetail'] = content_detail
  91. if fans_portrait:
  92. fans_portrait['channelAccountId'] = channel_account_id
  93. fans_portrait['channelContentId'] = channel_content_id
  94. ext_json['fanPortrait'] = fans_portrait
  95. rule_context = AutoProvideUtil.extract_content_rule_feature(content_detail=content_detail, fans_portrait=fans_portrait)
  96. ext_json['ruleContext'] = rule_context
  97. ext_json['rule'] = rule_str
  98. if (not content_detail) or (not fans_portrait):
  99. ext_json["result"] = False
  100. continue
  101. if not rule_context:
  102. cnt += 1
  103. continue
  104. result = eval_expr(expr=rule_str, context=rule_context)
  105. ext_json['result'] = result
  106. if result:
  107. cnt += 1
  108. if cnt <= preFilterThreshold:
  109. result_json["result"] = False
  110. result_json['reason'] = '该关键词首页满足条件的视频数不足'
  111. return result_json
  112. def keywords_not_login_comprehensive_sort(keywords: str, log_json: dict[str, Any], filters: List[SearchFilterConfigItem]):
  113. """
  114. 未登录,综合排序
  115. """
  116. account_id = "0"
  117. log_json = keywords_search_and_filter(keywords=keywords, sort_type="综合排序", account_id=account_id, log_json=log_json, filters=filters)
  118. log_info_print(log_json, account_id=account_id)
  119. def keywords_login_comprehensive_sort(keywords: str, log_json: dict[str, Any], filters: List[SearchFilterConfigItem]):
  120. """
  121. 登录,综合排序
  122. """
  123. account_id = "771431186"
  124. log_json = keywords_search_and_filter(keywords=keywords, sort_type="综合排序", account_id=account_id, log_json=log_json, filters=filters)
  125. log_info_print(log_json, account_id=account_id)
  126. def keywords_login_like_sort(keywords: str, log_json: dict[str, Any], filters: List[SearchFilterConfigItem]):
  127. """
  128. 登录状态,最多点赞
  129. """
  130. account_id = "771431186"
  131. log_json = keywords_search_and_filter(keywords=keywords, sort_type="最多点赞", account_id=account_id, log_json=log_json, filters=filters)
  132. log_info_print(log_json, account_id=account_id)
  133. def handle_log_json(log_json: dict[str, Any]):
  134. # 登录,综合排序
  135. # log_info_print(log_json)
  136. keywords = log_json['keywords']
  137. account_filters = json.loads(log_json.get("accountFilters", "[]"))
  138. content_filters = json.loads(log_json.get("contentFilters", '[]'))
  139. search_filter_config_tems = []
  140. for filter_item in account_filters + content_filters:
  141. search_filter_config_tems.append(SearchFilterConfigItem(**filter_item))
  142. # keywords_not_login_comprehensive_sort(keywords, log_json, search_filter_config_tems)
  143. # keywords_login_comprehensive_sort(keywords, log_json, search_filter_config_tems)
  144. keywords_login_like_sort(keywords, log_json, search_filter_config_tems)
  145. def main():
  146. file_path = '/Users/zhao/Desktop/keywords.json'
  147. log_list = []
  148. with open(file_path, "r", encoding="utf-8") as f:
  149. line = f.readline()
  150. while line:
  151. log_list.append(json.loads(line))
  152. line = f.readline()
  153. log_info_print_title()
  154. for log in log_list:
  155. if "“揭秘开国 领导人" == log['keywords']:
  156. handle_log_json(log)
  157. if __name__ == '__main__':
  158. main()