dy_search.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. duration = int(obj['duration'])
  37. if sensitive_flag(sensitive_words, title) and duration < 30000:
  38. res = douyin_detail(video_id)
  39. L.append(res)
  40. else:
  41. continue
  42. except Exception as e:
  43. continue
  44. logging(
  45. code="8001",
  46. info="抖音搜索",
  47. data={
  48. "keys": keyword,
  49. "search_count": len(dt_list),
  50. "useful_count": len(L)
  51. },
  52. trace_id=trace_id
  53. )
  54. return L
  55. except Exception as e:
  56. logging(
  57. code="4003",
  58. info="抖音搜索失败-搜索词:{} 原因:-{}".format(keyword, e),
  59. trace_id=trace_id
  60. )
  61. return []
  62. def douyin_detail(video_id):
  63. """
  64. get video url address
  65. :param video_id:
  66. :return:
  67. """
  68. url = "http://8.217.190.241:8888/crawler/dou_yin/detail"
  69. payload = json.dumps({
  70. "content_id": video_id
  71. })
  72. headers = {
  73. 'Content-Type': 'application/json'
  74. }
  75. response = requests.request("POST", url, headers=headers, data=payload).json()
  76. video_info = response['data']['data']
  77. return video_info