demo.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2023/5/15
  4. import random
  5. import time
  6. from common.scheduling_db import MysqlHelper
  7. class Demo:
  8. @classmethod
  9. def get_user(cls, log_type, crawler, env):
  10. select_user_sql = f"""select * from crawler_user_v3 where task_id=36"""
  11. user_list = MysqlHelper.get_values(log_type, crawler, select_user_sql, env, action="")
  12. print(user_list)
  13. our_uid_list = []
  14. for user in user_list:
  15. our_uid_list.append(user["uid"])
  16. print(our_uid_list)
  17. our_uid = random.choice(our_uid_list)
  18. print(our_uid)
  19. @classmethod
  20. def test_dict(cls):
  21. video_dict = {
  22. "play_cnt": 1000,
  23. "share_cnt": 1000,
  24. "duration": 55,
  25. "period": 5,
  26. "publish_time_stamp": 1683648000, # 2023-05-10 00:00:00
  27. "video_url": "www.baidu.com"
  28. }
  29. rule_dict = {
  30. "play_cnt": {"min": 0, "max": 0},
  31. "fans_cnt": {"min": 0, "max": 0},
  32. "videos_cnt": {"min": 0, "max": 0},
  33. "like_cnt": {"min": 0, "max": 0},
  34. "video_width": {"min": 0, "max": 0},
  35. "video_height": {"min": 0, "max": 0},
  36. "duration": {"min": 0, "max": 0},
  37. "share_cnt": {"min": 0, "max": 0},
  38. "comment_cnt": {"min": 0, "max": 0},
  39. "favorite_cnt": {"min": 0, "max": 0},
  40. "period": {"min": 10, "max": 0},
  41. "publish_time": {"min": 1673734400000, "max": 0}
  42. }
  43. # 格式化 video_dict:publish_time_stamp
  44. if "publish_time_stamp" in video_dict.keys():
  45. video_dict["publish_time"] = video_dict["publish_time_stamp"]*1000
  46. # 格式化 video_dict:period
  47. if "period" not in video_dict.keys() and "publish_time" in video_dict.keys():
  48. video_dict["period"] = int((int(time.time()*1000)-video_dict["publish_time"])/(3600*24*1000))
  49. # 格式化 rule_dict 最大值取值为 0 的问题
  50. for rule_value in rule_dict.values():
  51. if rule_value["max"] == 0:
  52. rule_value["max"] = 999999999999999
  53. rule_dict["period"]["max"] = rule_dict["period"]["min"]
  54. rule_dict["period"]["min"] = 0
  55. for k, v in rule_dict.items():
  56. print(f"{k}:{v}")
  57. # 格式化 rule_dict 有的 key,video_dict 中没有的问题
  58. for rule_key in rule_dict.keys():
  59. if rule_key not in video_dict.keys():
  60. video_dict[rule_key] = int(rule_dict[rule_key]["max"] / 2)
  61. # 比较结果,输出结果:True / False
  62. for video_key, video_value in video_dict.items():
  63. for rule_key, rule_value in rule_dict.items():
  64. if video_key == rule_key:
  65. result = rule_value["min"] <= video_value <= rule_value["max"]
  66. print(f'{video_key}: {rule_value["min"]} <= {video_value} <= {rule_value["max"]},{result}')
  67. # Common.logger(log_type, crawler).info(f'{video_key}: {rule_value["min"]} <= {video_value} <= {rule_value["max"]},{result}')
  68. if result is False:
  69. return False
  70. else:
  71. continue
  72. return True
  73. @classmethod
  74. def save_video_info(cls):
  75. video_dict = {'video_title': "测试视频标题",
  76. 'video_id': "id_1",
  77. 'play_cnt': 199,
  78. 'publish_time_stamp': 1683648000,
  79. 'publish_time_str': "2023-05-10 00:00:00",
  80. 'user_name': "岁岁年年迎福气",
  81. 'user_id': "suisuiniannianyingfuqi",
  82. 'avatar_url': "https://cdn.jzkksp.com/2022/3/22/lnchd2.jpg?auth_key=1684223012-0-0-2f8ddcf0e5d5f164f792b77c98e1ffde",
  83. 'cover_url': "https://cdn.jzkksp.com/2022/3/22/lnchd2.jpg?auth_key=1684223012-0-0-2f8ddcf0e5d5f164f792b77c98e1ffde",
  84. 'video_url': "https://cdn.jzkksp.com/2022/3/22/lnchd.mp4",
  85. 'session': f"suisuiniannianyingfuqi-{int(time.time())}"}
  86. save_dict = {
  87. "video_title": "video_title",
  88. "video_id": "video_id",
  89. "duration": 0,
  90. "play_cnt": 0,
  91. "comment_cnt": 0,
  92. "like_cnt": 0,
  93. "share_cnt": 0,
  94. "video_width": 1920,
  95. "video_height": 1080,
  96. "publish_time_stamp": 946656000, # 2000-01-01 00:00:00
  97. "user_name": "crawler",
  98. "avatar_url": "http://weapppiccdn.yishihui.com/resources/images/pic_normal.png",
  99. "video_url": "video_url",
  100. "cover_url": "cover_url",
  101. "session": f"session-{int(time.time())}",
  102. }
  103. for video_key, video_value in video_dict.items():
  104. for save_key, save_value in save_dict.items():
  105. if save_key == video_key:
  106. save_dict[save_key] = video_value
  107. for k, v in save_dict.items():
  108. print(f"{k}:{v}")
  109. with open(f"./info.txt", "w", encoding="UTF-8") as f_a:
  110. f_a.write(str(save_dict['video_id']) + "\n" +
  111. str(save_dict['video_title']) + "\n" +
  112. str(save_dict['duration']) + "\n" +
  113. str(save_dict['play_cnt']) + "\n" +
  114. str(save_dict['comment_cnt']) + "\n" +
  115. str(save_dict['like_cnt']) + "\n" +
  116. str(save_dict['share_cnt']) + "\n" +
  117. f"{save_dict['video_width']}*{save_dict['video_height']}" + "\n" +
  118. str(save_dict['publish_time_stamp']) + "\n" +
  119. str(save_dict['user_name']) + "\n" +
  120. str(save_dict['avatar_url']) + "\n" +
  121. str(save_dict['video_url']) + "\n" +
  122. str(save_dict['cover_url']) + "\n" +
  123. str(save_dict['session']))
  124. if __name__ == "__main__":
  125. # Demo.get_user("demo", "suisuiniannianyingfuqi", "dev")
  126. print(Demo.test_dict())
  127. # print(500 <= 1000 <= 100000000)
  128. # Demo.save_video_info()
  129. pass