demo.py 5.9 KB

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