sph_keyword.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import re
  2. import time
  3. import requests
  4. import json
  5. from common import Feishu, AliyunLogger, Material
  6. from common.sql_help import sqlCollect
  7. class SphKeyword:
  8. @classmethod
  9. def time_str_to_seconds(cls, time_str):
  10. # 分钟和秒用 ":" 分隔
  11. minutes, seconds = map(int, time_str.split(":"))
  12. # 转换为秒
  13. total_seconds = minutes * 60 + seconds
  14. return total_seconds
  15. @classmethod
  16. def get_key_word(cls, keyword, task_mark, mark, channel_id, name):
  17. url = "http://8.217.192.46:8889/crawler/wei_xin/shi_pin_hao/keyword"
  18. list = []
  19. payload = json.dumps({
  20. "keyword": keyword,
  21. "sort": "不限",
  22. "cursor": ""
  23. })
  24. headers = {
  25. 'Content-Type': 'application/json'
  26. }
  27. try:
  28. time.sleep(1)
  29. response = requests.request("POST", url, headers=headers, data=payload, timeout=30)
  30. response = response.json()
  31. code = response['code']
  32. if code != 0:
  33. Feishu.finish_bot(f"shi_pin_hao/keyword {response['msg']}",
  34. "https://open.feishu.cn/open-apis/bot/v2/hook/575ca6a1-84b4-4a2f-983b-1d178e7b16eb",
  35. "【视频号搜索接口使用提示】")
  36. return list
  37. data_list = response['data']['data']
  38. for data in data_list:
  39. items = data['items'][0]
  40. video_id = data["boxID"]
  41. duration = items["duration"]
  42. if duration == '' or duration == None:
  43. duration = "00:01"
  44. duration = cls.time_str_to_seconds(duration)
  45. digg_count = items.get('likeNum', "0")
  46. if digg_count == '10万+':
  47. digg_count = '100000'
  48. old_title =items.get('title', "")
  49. old_title = re.sub(r'<em.*?>.*?</em>', '', old_title)
  50. cover_url = items["image"]
  51. video_url = items["videoUrl"]
  52. log_data = f"user:{keyword},,video_id:{video_id},,video_url:{video_url},,original_title:{old_title},,digg_count:{digg_count},,duration:{duration}"
  53. AliyunLogger.logging(channel_id, name, keyword, video_id, "扫描到一条视频", "2001", log_data)
  54. day_count = Material.get_count_restrict(channel_id)
  55. if day_count:
  56. status = sqlCollect.is_used_days(task_mark, video_id, mark, channel_id, day_count)
  57. else:
  58. status = sqlCollect.is_used(task_mark, video_id, mark, channel_id)
  59. if status:
  60. AliyunLogger.logging(channel_id, name, keyword, video_id, "该视频已改造过", "2002", log_data)
  61. continue
  62. if int(digg_count) < 2000:
  63. AliyunLogger.logging(channel_id, name, keyword, video_id, f"不符合规则:点赞小于2000", "2003",
  64. log_data)
  65. continue
  66. if int(duration) < 30 or int(duration) > 900:
  67. AliyunLogger.logging(channel_id, name, keyword, video_id, f"不符合规则:时长不符合规则大于900秒/小于30秒", "2003", log_data)
  68. continue
  69. all_data = {"video_id": video_id, "cover": cover_url, "video_url": video_url, "rule": '',
  70. "old_title": old_title}
  71. list.append(all_data)
  72. AliyunLogger.logging(channel_id, name, keyword, video_id, "符合规则等待改造", "2004", log_data)
  73. return list
  74. except Exception as exc:
  75. return list
  76. if __name__ == '__main__':
  77. SphKeyword.get_key_word('最有钱的地方', '', '', '', '')