dy_search.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. import requests
  6. from applications.functions.common import sensitive_flag
  7. from applications.functions.log import logging
  8. def douyin_search(keyword, sensitive_words, trace_id):
  9. """
  10. Search with dou cha cha
  11. rank the relevance and recall the best three videos
  12. :param trace_id:
  13. :param sensitive_words: sensitive words in pq
  14. :param keyword: the words needs to be searched
  15. :return:
  16. """
  17. url = "http://8.217.190.241:8888/crawler/dou_yin/top_hub_content"
  18. payload = json.dumps({
  19. "keyword": keyword,
  20. "category": "全部",
  21. "period": "近90天",
  22. "content_modal": "视频",
  23. "cursor": ""
  24. })
  25. headers = {
  26. 'Content-Type': 'application/json'
  27. }
  28. response = requests.request("POST", url, headers=headers, data=payload)
  29. try:
  30. dt_list = response.json()['data']['data']
  31. L = []
  32. for obj in dt_list:
  33. try:
  34. title = obj['video_desc']
  35. video_id = obj['video_id']
  36. if sensitive_flag(sensitive_words, title):
  37. res = douyin_detail(video_id)
  38. L.append(res)
  39. else:
  40. continue
  41. except Exception as e:
  42. continue
  43. logging(
  44. code="8001",
  45. info="抖音搜索",
  46. data={
  47. "keys": keyword,
  48. "search_count": len(dt_list),
  49. "useful_count": len(L)
  50. },
  51. trace_id=trace_id
  52. )
  53. return L
  54. except Exception as e:
  55. logging(
  56. code="4003",
  57. info="抖音搜索失败-搜索词:{} 原因:-{}".format(keyword, e),
  58. trace_id=trace_id
  59. )
  60. return []
  61. def douyin_detail(video_id):
  62. """
  63. get video url address
  64. :param video_id:
  65. :return:
  66. """
  67. url = "http://8.217.190.241:8888/crawler/dou_yin/detail"
  68. payload = json.dumps({
  69. "content_id": video_id
  70. })
  71. headers = {
  72. 'Content-Type': 'application/json'
  73. }
  74. response = requests.request("POST", url, headers=headers, data=payload).json()
  75. video_info = response['data']['data']
  76. return video_info