getTencentReadDetails.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """
  2. @author: luojunhui
  3. 获取腾讯账号的 detail_info
  4. """
  5. import requests
  6. import json
  7. import time
  8. import schedule
  9. from tqdm import tqdm
  10. from datetime import datetime, timedelta
  11. from applications.decoratorApi import retryOnTimeout
  12. from applications import longArticlesMySQL, Functions
  13. class getTencentAccountReadDetail(object):
  14. """
  15. 获取腾讯平台前一天所有公众号群发文章的阅读
  16. """
  17. lam = longArticlesMySQL()
  18. @classmethod
  19. def generateAccountList(cls):
  20. """
  21. :return:
  22. """
  23. with open("config/accountInfoV0914.json", encoding="utf-8") as f:
  24. account_list = json.loads(f.read())
  25. account_list = [
  26. i for i in account_list if i.get('follower_count') is not None
  27. ]
  28. return account_list
  29. @classmethod
  30. @retryOnTimeout()
  31. def getAccountAccessToken(cls, ghId):
  32. """
  33. 通过 ghId获取账号的 accessToken
  34. :param ghId:
  35. :return:
  36. """
  37. url = "http://aigc-api.cybertogether.net/aigc/publish/account/getAccessTokenByGhId?ghId={}".format(ghId)
  38. response = requests.get(url, headers={"Content-Type": "Application/json"}, timeout=10)
  39. return response.text
  40. @classmethod
  41. @retryOnTimeout()
  42. def getAccountYesterdayReadInfo(cls, accessToken, daysCount):
  43. """
  44. 获取账号昨天当天所有群发文章的阅读
  45. :param accessToken:
  46. :return:
  47. """
  48. yesterday = (datetime.today() - timedelta(days=daysCount)).strftime('%Y-%m-%d')
  49. tencentUrl = "https://api.weixin.qq.com/datacube/getarticlesummary?access_token={}".format(accessToken)
  50. response = requests.post(
  51. url=tencentUrl,
  52. json={
  53. "begin_date": yesterday,
  54. "end_date": yesterday
  55. },
  56. timeout=10
  57. )
  58. return response.json()
  59. @classmethod
  60. def saveIntoDataBase(cls, infoTupleList):
  61. """
  62. :param infoTupleList:
  63. :param infoTuple:
  64. :return:
  65. info = (
  66. ghId,
  67. accountName,
  68. item['ref_date'],
  69. item['user_source'],
  70. item['msgid'].split("_")[0],
  71. item['msgid'].split("_")[1],
  72. item['title'],
  73. item['int_page_read_user'],
  74. item['int_page_read_count'],
  75. item['ori_page_read_user'],
  76. item['ori_page_read_count'],
  77. item['share_user'],
  78. item['share_count'],
  79. item['add_to_fav_user'],
  80. item['add_to_fav_count']
  81. )
  82. """
  83. insertSql = f"""
  84. INSERT INTO account_daily_read_info
  85. (ghId, accountName, refDate, userSource, msgId, position, title, readUser, readCount, oriPageReadUser, oriPageReadCount, shareUser, shareCount, favUser, favCount)
  86. values
  87. (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
  88. """
  89. cls.lam.insertMany(
  90. sql=insertSql,
  91. params_list=infoTupleList
  92. )
  93. @classmethod
  94. def deal(cls):
  95. """
  96. main function
  97. :return:
  98. """
  99. accountList = cls.generateAccountList()
  100. for i in range(2, 32):
  101. for account in tqdm(accountList):
  102. try:
  103. accountName = account['name']
  104. ghId = account['ghId']
  105. accessToken = cls.getAccountAccessToken(ghId=ghId)
  106. detailList = cls.getAccountYesterdayReadInfo(accessToken=accessToken, daysCount=i).get('list', [])
  107. if detailList:
  108. infoList = []
  109. for item in detailList:
  110. info = (
  111. ghId,
  112. accountName,
  113. item['ref_date'],
  114. item['user_source'],
  115. item['msgid'].split("_")[0],
  116. item['msgid'].split("_")[1],
  117. item['title'],
  118. item['int_page_read_user'],
  119. item['int_page_read_count'],
  120. item['ori_page_read_user'],
  121. item['ori_page_read_count'],
  122. item['share_user'],
  123. item['share_count'],
  124. item['add_to_fav_user'],
  125. item['add_to_fav_count']
  126. )
  127. infoList.append(info)
  128. cls.saveIntoDataBase(infoTupleList=infoList)
  129. except Exception as e:
  130. log = {
  131. "error": str(e),
  132. "msg": "更新账号文章失败",
  133. "accountInfo": account
  134. }
  135. print(log)
  136. def job():
  137. """
  138. job task
  139. :return:
  140. """
  141. g = getTencentAccountReadDetail()
  142. g.deal()
  143. if __name__ == '__main__':
  144. schedule.every().day.at("08:15").do(Functions().job_with_thread, job())
  145. while True:
  146. schedule.run_pending()
  147. time.sleep(1)