updateAccountV3.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. import time
  6. from tqdm import tqdm
  7. from datetime import datetime, timedelta
  8. from argparse import ArgumentParser
  9. from applications import PQMySQL, DeNetMysql, longArticlesMySQL
  10. TOULIU_ACCOUNTS = {
  11. 'gh_93e00e187787',
  12. 'gh_ac43e43b253b',
  13. 'gh_68e7fdc09fe4',
  14. 'gh_77f36c109fb1',
  15. 'gh_b181786a6c8c',
  16. 'gh_1ee2e1b39ccf'
  17. }
  18. ARTICLES_DAILY = 1
  19. TOULIU = 2
  20. def get_account_fans_by_dt(db_client) -> dict:
  21. """
  22. 获取每个账号发粉丝,通过日期来区分
  23. :return:
  24. """
  25. sql = f"""
  26. SELECT
  27. t1.date_str,
  28. t1.fans_count,
  29. t2.gh_id
  30. FROM datastat_wx t1
  31. JOIN publish_account t2 ON t1.account_id = t2.id
  32. WHERE
  33. t2.channel = 5
  34. AND t2.status = 1
  35. AND t1.date_str >= '2024-09-01'
  36. ORDER BY t1.date_str;
  37. """
  38. result = db_client.select(sql)
  39. D = {}
  40. for line in result:
  41. dt = line[0]
  42. fans = line[1]
  43. gh_id = line[2]
  44. if D.get(gh_id):
  45. D[gh_id][dt] = fans
  46. else:
  47. D[gh_id] = {dt: fans}
  48. return D
  49. class UpdateAccountInfoVersion3(object):
  50. """
  51. 更新账号信息 v3
  52. """
  53. def __init__(self):
  54. self.pq = PQMySQL()
  55. self.de = DeNetMysql()
  56. self.lam = longArticlesMySQL()
  57. def get_account_position_read_rate(self, dt):
  58. """
  59. 从长文数据库获取账号阅读均值
  60. :return:
  61. """
  62. dt = int(dt.replace("-", ""))
  63. sql = f"""
  64. SELECT
  65. gh_id, position, read_rate_avg
  66. FROM
  67. long_articles_read_rate
  68. WHERE dt_version = {dt};
  69. """
  70. result = self.lam.select(sql)
  71. account_read_rate_dict = {}
  72. for item in result:
  73. gh_id = item[0]
  74. position = item[1]
  75. rate = item[2]
  76. key = "{}_{}".format(gh_id, position)
  77. account_read_rate_dict[key] = rate
  78. return account_read_rate_dict
  79. def get_publishing_accounts(self):
  80. """
  81. 获取每日正在发布的账号
  82. :return:
  83. """
  84. sql = f"""
  85. SELECT DISTINCT
  86. t3.`name`,
  87. t3.gh_id,
  88. t3.follower_count,
  89. t6.account_source_name,
  90. t6.mode_type,
  91. t6.account_type,
  92. t6.`status`
  93. FROM
  94. publish_plan t1
  95. JOIN publish_plan_account t2 ON t1.id = t2.plan_id
  96. JOIN publish_account t3 ON t2.account_id = t3.id
  97. LEFT JOIN publish_account_wx_type t4 on t3.id = t4.account_id
  98. LEFT JOIN wx_statistics_group_source_account t5 on t3.id = t5.account_id
  99. LEFT JOIN wx_statistics_group_source t6 on t5.group_source_name = t6.account_source_name
  100. WHERE
  101. t1.plan_status = 1
  102. AND t3.channel = 5
  103. GROUP BY t3.id;
  104. """
  105. account_list = self.de.select(sql)
  106. result_list = [
  107. {
  108. "account_name": i[0],
  109. "gh_id": i[1],
  110. "fans": i[2],
  111. "account_source_name": i[3],
  112. "mode_type": i[4],
  113. "account_type": i[5],
  114. "status": i[6]
  115. } for i in account_list
  116. ]
  117. return result_list
  118. def do_task_list(self, dt):
  119. """
  120. do it
  121. """
  122. fans_dict = get_account_fans_by_dt(db_client=self.de)
  123. account_list = self.get_publishing_accounts()
  124. rate_dict = self.get_account_position_read_rate(dt)
  125. for account in tqdm(account_list):
  126. business_type = TOULIU if account['gh_id'] in TOULIU_ACCOUNTS else ARTICLES_DAILY
  127. fans = fans_dict.get(account['gh_id'], {}).get(dt, 0)
  128. if fans:
  129. for index in range(1, 9):
  130. gh_id_position = "{}_{}".format(account['gh_id'], index)
  131. if rate_dict.get(gh_id_position):
  132. rate = rate_dict[gh_id_position]
  133. read_avg = fans * rate
  134. print(rate, read_avg)
  135. insert_sql = f"""
  136. INSERT INTO account_avg_info_v3
  137. (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)
  138. values
  139. (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
  140. """
  141. try:
  142. self.pq.update(
  143. sql=insert_sql,
  144. params=(
  145. account['gh_id'],
  146. index,
  147. dt,
  148. account['account_name'],
  149. fans,
  150. read_avg,
  151. 0,
  152. 1,
  153. account['account_type'],
  154. account['mode_type'],
  155. account['account_source_name'],
  156. account['status'],
  157. business_type,
  158. rate
  159. )
  160. )
  161. except Exception as e:
  162. updateSQL = f"""
  163. UPDATE account_avg_info_v3
  164. set fans = %s, read_avg = %s, read_rate_avg = %s
  165. where gh_id = %s and position = %s and update_time = %s
  166. """
  167. try:
  168. affected_rows = self.pq.update(
  169. sql=updateSQL,
  170. params=(
  171. fans,
  172. read_avg,
  173. rate,
  174. account['gh_id'],
  175. index,
  176. dt
  177. )
  178. )
  179. except Exception as e:
  180. print(e)
  181. # 修改前一天的状态为 0
  182. update_status_sql = f"""
  183. UPDATE account_avg_info_v3
  184. SET status = %s
  185. where update_time != %s and gh_id = %s and position = %s;
  186. """
  187. rows_affected = self.pq.update(
  188. sql=update_status_sql,
  189. params=(
  190. 0, dt, account['gh_id'], index
  191. )
  192. )
  193. print("修改成功")
  194. def main():
  195. """
  196. main job
  197. :return:
  198. """
  199. parser = ArgumentParser()
  200. parser.add_argument("--run-date",
  201. help="Run only once for date in format of %Y-%m-%d. \
  202. If no specified, run as daily jobs.")
  203. args = parser.parse_args()
  204. Up = UpdateAccountInfoVersion3()
  205. if args.run_date:
  206. Up.do_task_list(dt=args.run_date)
  207. else:
  208. dt_object = datetime.fromtimestamp(int(time.time()))
  209. one_day = timedelta(days=1)
  210. yesterday = dt_object - one_day
  211. yesterday_str = yesterday.strftime('%Y-%m-%d')
  212. Up.do_task_list(dt=yesterday_str)
  213. if __name__ == '__main__':
  214. main()