video_item.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import time
  2. from common.public import clean_title
  3. from .aliyun_log import AliyunLogger
  4. class VideoItem(object):
  5. """
  6. function: 当扫描进一条视频的时候,对该视频的基本信息进行处理,保证发送给 pipeline和 etl 的 video_dict 是正确的
  7. __init__: 初始化空json 对象,用来存储视频信息
  8. add_video_info: 把视频信息存储到 item 对象中
  9. check_item: 检查 item 对象中的各个元素以及处理
  10. """
  11. def __init__(self):
  12. self.item = {}
  13. def add_video_info(self, key, value):
  14. self.item[key] = value
  15. def check_item(self):
  16. """
  17. 判断item 里面的字段,是否符合要求
  18. 字段分为 3 类:
  19. 1. 必须存在数据的字段: ["video_id", "user_id", "user_name", "out_user_id", "out_video_id", "session", "video_url", "cover_url", "platform", "strategy"]
  20. 2. 不存在默认为 0 的字段 :["duration", "play_cnt", "like_cnt", "comment_cnt", "share_cnt", "width", "height"]
  21. 3. 需要后出理的字段: video_title, publish_time
  22. """
  23. if self.item.get("video_title"):
  24. self.item["video_title"] = clean_title(self.item["video_title"])
  25. else:
  26. return False
  27. if self.item.get("publish_time_stamp"):
  28. publish_time_str = time.strftime(
  29. "%Y-%m-%d %H:%M:%S", time.localtime(self.item["publish_time_stamp"])
  30. )
  31. self.add_video_info("publish_time_str", publish_time_str)
  32. else:
  33. publish_time_stamp = int(time.time())
  34. publish_time_str = time.strftime(
  35. "%Y-%m-%d %H:%M:%S", time.localtime(publish_time_stamp)
  36. )
  37. self.add_video_info("publish_time_stamp", publish_time_stamp)
  38. self.add_video_info("publish_time_str", publish_time_str)
  39. self.add_video_info("publish_time", publish_time_str)
  40. if not self.item.get("update_time_stamp"):
  41. self.add_video_info("update_time_stamp", int(time.time()))
  42. # 如果不存在,默认值为 0
  43. config_keys = [
  44. "duration",
  45. "play_cnt",
  46. "like_cnt",
  47. "comment_cnt",
  48. "share_cnt",
  49. "width",
  50. "height",
  51. ]
  52. for config_key in config_keys:
  53. if self.item.get(config_key):
  54. continue
  55. else:
  56. self.add_video_info(config_key, 0)
  57. # 必须存在的元素,若不存在则会报错
  58. must_keys = [
  59. "video_id",
  60. "user_id",
  61. "user_name",
  62. "out_video_id",
  63. "session",
  64. "video_url",
  65. "cover_url",
  66. "platform",
  67. "strategy",
  68. ]
  69. """
  70. video_id, out_video_id 均为站外视频 id
  71. usr_id: 站内用户 id
  72. out_user_id: 站外用户 id
  73. user_name: 站外用户名称
  74. """
  75. for m_key in must_keys:
  76. if self.item.get(m_key):
  77. continue
  78. else:
  79. # print(m_key)
  80. return False
  81. return True
  82. def produce_item(self):
  83. flag = self.check_item()
  84. if flag:
  85. return self.item
  86. else:
  87. return False