getTencentReadDetails.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. print(yesterday)
  50. tencentUrl = "https://api.weixin.qq.com/datacube/getarticlesummary?access_token={}".format(accessToken)
  51. response = requests.post(
  52. url=tencentUrl,
  53. json={
  54. "begin_date": yesterday,
  55. "end_date": yesterday
  56. },
  57. timeout=10
  58. )
  59. print(response.json())
  60. return response.json()
  61. @classmethod
  62. def saveIntoDataBase(cls, infoTupleList):
  63. """
  64. :param infoTupleList:
  65. :param infoTuple:
  66. :return:
  67. info = (
  68. ghId,
  69. accountName,
  70. item['ref_date'],
  71. item['user_source'],
  72. item['msgid'].split("_")[0],
  73. item['msgid'].split("_")[1],
  74. item['title'],
  75. item['int_page_read_user'],
  76. item['int_page_read_count'],
  77. item['ori_page_read_user'],
  78. item['ori_page_read_count'],
  79. item['share_user'],
  80. item['share_count'],
  81. item['add_to_fav_user'],
  82. item['add_to_fav_count']
  83. )
  84. """
  85. insertSql = f"""
  86. INSERT INTO account_daily_read_info
  87. (ghId, accountName, refDate, userSource, msgId, position, title, readUser, readCount, oriPageReadUser, oriPageReadCount, shareUser, shareCount, favUser, favCount)
  88. values
  89. (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
  90. """
  91. cls.lam.insertMany(
  92. sql=insertSql,
  93. params_list=infoTupleList
  94. )
  95. @classmethod
  96. def deal(cls):
  97. """
  98. main function
  99. :return:
  100. """
  101. accountList = cls.generateAccountList()
  102. for account in tqdm(accountList):
  103. try:
  104. accountName = account['name']
  105. ghId = account['ghId']
  106. accessToken = cls.getAccountAccessToken(ghId=ghId)
  107. detailList = cls.getAccountYesterdayReadInfo(accessToken=accessToken, daysCount=1).get('list', [])
  108. if detailList:
  109. infoList = []
  110. for item in detailList:
  111. info = (
  112. ghId,
  113. accountName,
  114. item['ref_date'],
  115. item['user_source'],
  116. item['msgid'].split("_")[0],
  117. item['msgid'].split("_")[1],
  118. item['title'],
  119. item['int_page_read_user'],
  120. item['int_page_read_count'],
  121. item['ori_page_read_user'],
  122. item['ori_page_read_count'],
  123. item['share_user'],
  124. item['share_count'],
  125. item['add_to_fav_user'],
  126. item['add_to_fav_count']
  127. )
  128. infoList.append(info)
  129. cls.saveIntoDataBase(infoTupleList=infoList)
  130. except Exception as e:
  131. log = {
  132. "error": str(e),
  133. "msg": "更新账号文章失败",
  134. "accountInfo": account
  135. }
  136. print(log)
  137. def job():
  138. """
  139. job task
  140. :return:
  141. """
  142. g = getTencentAccountReadDetail()
  143. g.deal()
  144. if __name__ == '__main__':
  145. schedule.every().day.at("08:15").do(Functions().job_with_thread, job)
  146. while True:
  147. schedule.run_pending()
  148. time.sleep(1)