demo.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2023/5/15
  4. import random
  5. import time
  6. from common.common import Common
  7. from common.scheduling_db import MysqlHelper
  8. class Demo:
  9. @classmethod
  10. def get_user(cls, log_type, crawler, env):
  11. select_user_sql = f"""select * from crawler_user_v3 where task_id=36"""
  12. user_list = MysqlHelper.get_values(log_type, crawler, select_user_sql, env, action="")
  13. print(user_list)
  14. our_uid_list = []
  15. for user in user_list:
  16. our_uid_list.append(user["uid"])
  17. print(our_uid_list)
  18. our_uid = random.choice(our_uid_list)
  19. print(our_uid)
  20. @classmethod
  21. def test_dict(cls):
  22. video_dict = {
  23. "play_cnt": 1000,
  24. "share_cnt": 1000,
  25. "duration": 55,
  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 有的 key,video_dict 中没有的问题
  54. for rule_key in rule_dict.keys():
  55. if rule_key not in video_dict.keys():
  56. video_dict[rule_key] = int(rule_dict[rule_key]["max"] / 2)
  57. # 比较结果,输出结果:True / False
  58. for video_key, video_value in video_dict.items():
  59. for rule_key, rule_value in rule_dict.items():
  60. if video_key == rule_key:
  61. result = rule_value["min"] <= video_value <= rule_value["max"]
  62. print(f'{video_key}: {rule_value["min"]} <= {video_value} <= {rule_value["max"]},{result}')
  63. # Common.logger(log_type, crawler).info(f'{video_key}: {rule_value["min"]} <= {video_value} <= {rule_value["max"]},{result}')
  64. if result is False:
  65. return False
  66. else:
  67. continue
  68. return True
  69. if __name__ == "__main__":
  70. # Demo.get_user("demo", "suisuiniannianyingfuqi", "dev")
  71. print(Demo.test_dict())
  72. # print(500 <= 1000 <= 100000000)
  73. pass