sph_keyword.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. if digg_count == '10万+':
  46. digg_count = '100000'
  47. old_title =items.get('title', "")
  48. old_title = re.sub(r'<em.*?>.*?</em>', '', old_title)
  49. cover_url = items["image"]
  50. video_url = items["videoUrl"]
  51. log_data = f"user:{keyword},,video_id:{video_id},,video_url:{video_url},,original_title:{old_title},,digg_count:{digg_count},,duration:{duration}"
  52. AliyunLogger.logging(channel_id, name, keyword, video_id, "扫描到一条视频", "2001", log_data)
  53. Common.logger("sph-key-word").info(
  54. f"扫描:{task_mark},搜索词:{keyword},视频id{video_id},点赞{digg_count}")
  55. status = sqlCollect.is_used(task_mark, video_id, mark, channel_id)
  56. if status:
  57. AliyunLogger.logging(channel_id, name, keyword, video_id, "该视频已改造过", "2001", log_data)
  58. continue
  59. if int(digg_count) < 2000:
  60. AliyunLogger.logging(channel_id, name, keyword, video_id, f"不符合规则:点赞小于2000", "2003",
  61. log_data)
  62. Common.logger("sph-key-word").info(
  63. f"不符合规则:{task_mark},用户主页id:{keyword},视频id{video_id} ,点赞{digg_count} ,时长:{int(duration)} ")
  64. continue
  65. if int(duration) < 30 or int(duration) > 900:
  66. AliyunLogger.logging(channel_id, name, keyword, video_id, f"不符合规则:时长不符合规则大于900秒/小于30秒", "2003", log_data)
  67. Common.logger("sph-key-word").info(
  68. f"不符合规则:{task_mark},用户主页id:{keyword},视频id{video_id} 点赞{digg_count} ,时长:{int(duration)} ")
  69. continue
  70. AliyunLogger.logging(channel_id, name, keyword, video_id, "符合规则等待改造", "2004", log_data)
  71. all_data = {"video_id": video_id, "cover": cover_url, "video_url": video_url, "rule": '',
  72. "old_title": old_title}
  73. list.append(all_data)
  74. return list
  75. except Exception as exc:
  76. Common.logger("sph-key-word").info(f"视频号搜索词{keyword}获取失败{exc}\n")
  77. return list
  78. if __name__ == '__main__':
  79. SphKeyword.get_key_word('iphone手机', '', '', '', '')