123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- """
- @author: luojunhui
- 获取腾讯账号的 detail_info
- """
- import requests
- import json
- import time
- import schedule
- from tqdm import tqdm
- from datetime import datetime, timedelta
- from applications.decoratorApi import retryOnTimeout
- from applications import longArticlesMySQL, Functions
- class getTencentAccountReadDetail(object):
- """
- 获取腾讯平台前一天所有公众号群发文章的阅读
- """
- lam = longArticlesMySQL()
- @classmethod
- def generateAccountList(cls):
- """
- :return:
- """
- with open("config/accountInfoV0914.json", encoding="utf-8") as f:
- account_list = json.loads(f.read())
- account_list = [
- i for i in account_list if i.get('follower_count') is not None
- ]
- return account_list
- @classmethod
- @retryOnTimeout()
- def getAccountAccessToken(cls, ghId):
- """
- 通过 ghId获取账号的 accessToken
- :param ghId:
- :return:
- """
- url = "http://aigc-api.cybertogether.net/aigc/publish/account/getAccessTokenByGhId?ghId={}".format(ghId)
- response = requests.get(url, headers={"Content-Type": "Application/json"}, timeout=10)
- return response.text
- @classmethod
- @retryOnTimeout()
- def getAccountYesterdayReadInfo(cls, accessToken, daysCount):
- """
- 获取账号昨天当天所有群发文章的阅读
- :param accessToken:
- :return:
- """
- yesterday = (datetime.today() - timedelta(days=daysCount)).strftime('%Y-%m-%d')
- print(yesterday)
- tencentUrl = "https://api.weixin.qq.com/datacube/getarticlesummary?access_token={}".format(accessToken)
- response = requests.post(
- url=tencentUrl,
- json={
- "begin_date": yesterday,
- "end_date": yesterday
- },
- timeout=10
- )
- print(response.json())
- return response.json()
- @classmethod
- def saveIntoDataBase(cls, infoTupleList):
- """
- :param infoTupleList:
- :param infoTuple:
- :return:
- info = (
- ghId,
- accountName,
- item['ref_date'],
- item['user_source'],
- item['msgid'].split("_")[0],
- item['msgid'].split("_")[1],
- item['title'],
- item['int_page_read_user'],
- item['int_page_read_count'],
- item['ori_page_read_user'],
- item['ori_page_read_count'],
- item['share_user'],
- item['share_count'],
- item['add_to_fav_user'],
- item['add_to_fav_count']
- )
- """
- insertSql = f"""
- INSERT INTO account_daily_read_info
- (ghId, accountName, refDate, userSource, msgId, position, title, readUser, readCount, oriPageReadUser, oriPageReadCount, shareUser, shareCount, favUser, favCount)
- values
- (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
- """
- cls.lam.insertMany(
- sql=insertSql,
- params_list=infoTupleList
- )
- @classmethod
- def deal(cls):
- """
- main function
- :return:
- """
- accountList = cls.generateAccountList()
- for account in tqdm(accountList):
- try:
- accountName = account['name']
- ghId = account['ghId']
- accessToken = cls.getAccountAccessToken(ghId=ghId)
- detailList = cls.getAccountYesterdayReadInfo(accessToken=accessToken, daysCount=1).get('list', [])
- if detailList:
- infoList = []
- for item in detailList:
- info = (
- ghId,
- accountName,
- item['ref_date'],
- item['user_source'],
- item['msgid'].split("_")[0],
- item['msgid'].split("_")[1],
- item['title'],
- item['int_page_read_user'],
- item['int_page_read_count'],
- item['ori_page_read_user'],
- item['ori_page_read_count'],
- item['share_user'],
- item['share_count'],
- item['add_to_fav_user'],
- item['add_to_fav_count']
- )
- infoList.append(info)
- cls.saveIntoDataBase(infoTupleList=infoList)
- except Exception as e:
- log = {
- "error": str(e),
- "msg": "更新账号文章失败",
- "accountInfo": account
- }
- print(log)
- def job():
- """
- job task
- :return:
- """
- g = getTencentAccountReadDetail()
- g.deal()
- if __name__ == '__main__':
- schedule.every().day.at("08:15").do(Functions().job_with_thread, job)
- while True:
- schedule.run_pending()
- time.sleep(1)
|