updateAccountV3.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. import time
  6. import schedule
  7. from tqdm import tqdm
  8. from datetime import datetime, timedelta
  9. from applications import PQMySQL, DeNetMysql, Functions
  10. def get_accounts_read_rate():
  11. """
  12. 获取阅读率表
  13. :return:
  14. """
  15. with open("config/account_read_rate_map.json", encoding="utf-8") as f:
  16. account_list = json.loads(f.read())
  17. return account_list
  18. class UpdateAccountInfoVersion3(object):
  19. """
  20. 更新账号信息 v3
  21. """
  22. def __init__(self):
  23. self.pq = PQMySQL()
  24. self.de = DeNetMysql()
  25. def get_account_fans_by_date(self, date):
  26. """
  27. 通过日期来获取所有账号的粉丝 dict
  28. :param date 2024-01-01
  29. :return: dict key: gh_id, value: fans(int)
  30. """
  31. sql = f"""
  32. select
  33. t1.fans_count, t2.gh_id
  34. from datastat_wx t1
  35. join publish_account t2 on t1.account_id = t2.id
  36. where t2.channel = 5 and t2.status = 1 and t1.date_str >= '{date}' order by t1.date_str;
  37. """
  38. result = self.de.select(sql)
  39. fans_dict = {}
  40. for account_info in result:
  41. fans_count, gh_id = account_info
  42. fans_dict[gh_id] = fans_count
  43. return fans_dict
  44. def update_single_day(self, dt, fans_dict):
  45. """
  46. :return:
  47. """
  48. task_list = get_accounts_read_rate()
  49. for item in tqdm(task_list):
  50. fans = fans_dict.get(item['gh_id'], 0)
  51. read_avg = fans * item['read_rate_avg']
  52. insert_sql = f"""
  53. INSERT INTO account_avg_info_v3
  54. (gh_id, position, update_time, account_name, fans, read_avg, like_avg, status, account_type, account_mode, account_source, account_status, business_type, read_rate_avg)
  55. values
  56. (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
  57. """
  58. try:
  59. self.pq.update(
  60. sql=insert_sql,
  61. params=(
  62. item['gh_id'],
  63. item['position'],
  64. dt,
  65. item['account_name'],
  66. fans,
  67. read_avg,
  68. item['like_avg'],
  69. 1,
  70. item['account_type'],
  71. item['account_mode'],
  72. item['account_source'],
  73. item['account_status'],
  74. item['business_type'],
  75. item['read_rate_avg']
  76. )
  77. )
  78. except Exception as e:
  79. updateSQL = f"""
  80. UPDATE account_avg_info_v3
  81. set fans = %s, read_avg = %s
  82. where gh_id = %s and position = %s and update_time = %s
  83. """
  84. try:
  85. self.pq.update(
  86. sql=updateSQL,
  87. params=(
  88. fans,
  89. read_avg,
  90. item['gh_id'],
  91. item['position'],
  92. dt
  93. )
  94. )
  95. print("update success")
  96. except Exception as e:
  97. print(e)
  98. # 修改前一天的状态为 0
  99. uuu_sql = f"""
  100. UPDATE account_avg_info_v3
  101. SET status = %s
  102. where update_time != %s and gh_id = %s and position = %s;
  103. """
  104. self.pq.update(
  105. sql=uuu_sql,
  106. params=(
  107. 0, dt, item['gh_id'], item['position']
  108. )
  109. )
  110. print("修改成功")
  111. def updateDaily():
  112. """
  113. main job
  114. :return:
  115. """
  116. Up = UpdateAccountInfoVersion3()
  117. dt_object = datetime.fromtimestamp(int(time.time()))
  118. one_day = timedelta(days=1)
  119. yesterday = dt_object - one_day
  120. yesterday_str = yesterday.strftime('%Y-%m-%d')
  121. fans_dict = Up.get_account_fans_by_date(yesterday_str)
  122. Up.update_single_day(yesterday_str, fans_dict)
  123. if __name__ == '__main__':
  124. schedule.every().day.at("10:15").do(Functions().job_with_thread, updateDaily)
  125. schedule.every().day.at("10:30").do(Functions().job_with_thread, updateDaily)
  126. schedule.every().day.at("10:50").do(Functions().job_with_thread, updateDaily)
  127. while True:
  128. schedule.run_pending()
  129. time.sleep(1)