updateAccountV3.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """
  2. @author: luojunhui
  3. """
  4. import time
  5. import schedule
  6. from tqdm import tqdm
  7. from datetime import datetime
  8. from applications import longArticlesMySQL, PQMySQL, DeNetMysql, Functions
  9. class UpdateAccountInfoVersion3(object):
  10. """
  11. 更新账号信息 v3
  12. """
  13. lam = longArticlesMySQL()
  14. pq = PQMySQL()
  15. de = DeNetMysql()
  16. @classmethod
  17. def getAccountFans(cls):
  18. """
  19. :return:
  20. """
  21. sql = f"""
  22. select t1.date_str, t1.fans_count, t2.gh_id, t2.name
  23. from datastat_wx t1
  24. join publish_account t2 on t1.account_id = t2.id
  25. where t2.channel = 5 and t2.status = 1 and t1.date_str >= '2024-07-01' order by t1.date_str;"""
  26. result = cls.de.select(sql)
  27. D = {}
  28. for line in result:
  29. dt = line[0]
  30. fans = line[1]
  31. account_name = line[3]
  32. if D.get(account_name):
  33. D[account_name][dt] = fans
  34. else:
  35. D[account_name] = {dt: fans}
  36. return D
  37. @classmethod
  38. def getAccountRate(cls):
  39. """
  40. 获取账号阅读率
  41. :return:
  42. """
  43. sql = "select account_name, position, read_rate_avg from long_articles_read_rate;"
  44. result = cls.lam.select(sql)
  45. D = {}
  46. for line in result:
  47. if D.get(line[0]):
  48. D[line[0]][line[1]] = line[2]
  49. else:
  50. D[line[0]] = {line[1]: line[2]}
  51. return D
  52. @classmethod
  53. def reverseSingleDay(cls, dt, fans_dict, rate_dict):
  54. """
  55. :return:
  56. """
  57. sql = f"""select * from account_avg_info_v3 where update_time = '2024-09-09';"""
  58. result = cls.pq.select(sql)
  59. for line in tqdm(result):
  60. temp = list(line)
  61. temp[2] = dt
  62. temp[4] = fans_dict.get(temp[3], {}).get(dt, 0)
  63. temp[-1] = rate_dict.get(temp[3], {}).get(temp[1], 0)
  64. temp[5] = fans_dict.get(temp[3], {}).get(dt, 0) * rate_dict.get(temp[3], {}).get(temp[1], 0)
  65. usql = f"""
  66. INSERT INTO account_avg_info_v3
  67. (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)
  68. values
  69. (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
  70. """
  71. try:
  72. cls.pq.update(
  73. sql=usql,
  74. params=tuple(temp)
  75. )
  76. except Exception as e:
  77. updateSQL = f"""
  78. UPDATE account_avg_info_v3
  79. set fans = %s, read_avg = %s, read_rate_avg = %s
  80. where account_name = %s and position = %s and update_time = %s
  81. """
  82. try:
  83. cls.pq.update(
  84. sql=updateSQL,
  85. params=(
  86. temp[4],
  87. temp[5],
  88. temp[-1],
  89. temp[3],
  90. temp[1],
  91. temp[2]
  92. )
  93. )
  94. except Exception as e:
  95. print(e)
  96. def updateDaily():
  97. """
  98. main job
  99. :return:
  100. """
  101. Up = UpdateAccountInfoVersion3()
  102. fd = Up.getAccountFans()
  103. rd = Up.getAccountRate()
  104. today_dt = datetime.today().__str__().split(" ")[0]
  105. Up.reverseSingleDay(today_dt, fd, rd)
  106. Up.lam.close()
  107. Up.pq.close()
  108. Up.de.close()
  109. if __name__ == '__main__':
  110. schedule.every().day.at("21:50").do(Functions().job_with_thread, updateDaily)
  111. while True:
  112. schedule.run_pending()
  113. time.sleep(1)