updateAccountV3.py 6.6 KB

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