sph_keyword.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import re
  2. import time
  3. import requests
  4. import json
  5. from common import Common, Feishu, AliyunLogger
  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.190.241:8888/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)
  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. Common.logger("sph-key-word").info(f"快手搜索词数据获取失败,{response['msg']}\n")
  37. return list
  38. data_list = response['data']['data']
  39. for data in data_list:
  40. items = data['items'][0]
  41. video_id = data["'boxID'"]
  42. duration = items["duration"]
  43. duration = cls.time_str_to_seconds(duration)
  44. digg_count = items.get('likeNum', "0")
  45. old_title =items.get('title', "")
  46. old_title = re.sub(r'<em.*?>.*?</em>', '', old_title)
  47. cover_url = items["image"]
  48. video_url = items["videoUrl"]
  49. log_data = f"user:{keyword},,video_id:{video_id},,video_url:{video_url},,original_title:{old_title},,digg_count:{digg_count},,duration:{duration}"
  50. AliyunLogger.logging(channel_id, name, keyword, video_id, "扫描到一条视频", "2001", log_data)
  51. Common.logger("sph-key-word").info(
  52. f"扫描:{task_mark},搜索词:{keyword},视频id{video_id},点赞{digg_count}")
  53. status = sqlCollect.is_used(task_mark, video_id, mark, channel_id)
  54. if status:
  55. AliyunLogger.logging(channel_id, name, keyword, video_id, "该视频已改造过", "2001", log_data)
  56. continue
  57. if int(digg_count) < 2000:
  58. AliyunLogger.logging(channel_id, name, keyword, video_id, f"不符合规则:点赞小于2000", "2003",
  59. log_data)
  60. Common.logger("sph-key-word").info(
  61. f"不符合规则:{task_mark},用户主页id:{keyword},视频id{video_id} ,点赞{digg_count} ,时长:{int(duration)} ")
  62. continue
  63. if int(duration) < 30 or int(duration) > 900:
  64. AliyunLogger.logging(channel_id, name, keyword, video_id, f"不符合规则:时长不符合规则大于900秒/小于30秒", "2003", log_data)
  65. Common.logger("sph-key-word").info(
  66. f"不符合规则:{task_mark},用户主页id:{keyword},视频id{video_id} 点赞{digg_count} ,时长:{int(duration)} ")
  67. continue
  68. AliyunLogger.logging(channel_id, name, keyword, video_id, "符合规则等待改造", "2004", log_data)
  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. return list
  73. except Exception as exc:
  74. Common.logger("sph-key-word").info(f"视频号搜索词{keyword}获取失败{exc}\n")
  75. return list
  76. if __name__ == '__main__':
  77. SphKeyword.get_key_word('iphone手机', '', '', '', '')