|
@@ -1,12 +1,15 @@
|
|
|
# -*- coding: utf-8 -*-
|
|
|
# @Author: wangkun
|
|
|
# @Time: 2023/2/10
|
|
|
+import os
|
|
|
+import sys
|
|
|
+import time
|
|
|
from datetime import date, timedelta
|
|
|
-
|
|
|
import requests
|
|
|
import json
|
|
|
-
|
|
|
+sys.path.append(os.getcwd())
|
|
|
from common.feishu import Feishu
|
|
|
+from common.common import Common
|
|
|
|
|
|
|
|
|
class Weixinzhishu:
|
|
@@ -16,26 +19,23 @@ class Weixinzhishu:
|
|
|
|
|
|
@classmethod
|
|
|
def wechat_key(cls, log_type, crawler):
|
|
|
- sheet = Feishu.get_values_batch(log_type, crawler, 'sVL74k')
|
|
|
- for i in range(len(sheet)):
|
|
|
- search_key = sheet[1][1]
|
|
|
- openid = sheet[1][2]
|
|
|
- return search_key, openid
|
|
|
+ try:
|
|
|
+ sheet = Feishu.get_values_batch(log_type, crawler, 'sVL74k')
|
|
|
+ for i in range(len(sheet)):
|
|
|
+ search_key = sheet[1][1]
|
|
|
+ openid = sheet[1][2]
|
|
|
+ return search_key, openid
|
|
|
+ except Exception as e:
|
|
|
+ Common.logger(log_type, crawler).error(f"wechat_key:{e}\n")
|
|
|
|
|
|
@classmethod
|
|
|
- def weixinzhishu(cls, log_type, crawler):
|
|
|
- search_word_list = cls.search_word()
|
|
|
- wechat_key = cls.wechat_key(log_type, crawler)
|
|
|
- search_key = wechat_key[0]
|
|
|
- openid = wechat_key[-1]
|
|
|
- start_ymd = (date.today() + timedelta(days=-1)).strftime("%Y-%m-%d").replace("-", "")
|
|
|
- end_ymd = (date.today() + timedelta(days=-8)).strftime("%Y-%m-%d").replace("-", "")
|
|
|
- print(f"search_key:{search_key}")
|
|
|
- print(f"openid:{openid}")
|
|
|
- print(f"start_ymd:{start_ymd}")
|
|
|
- print(f"start_ymd:{end_ymd}")
|
|
|
- for word in search_word_list:
|
|
|
- print(f"word:{word}")
|
|
|
+ def weixinzhishu(cls, log_type, crawler, word_id, word):
|
|
|
+ try:
|
|
|
+ wechat_key = cls.wechat_key(log_type, crawler)
|
|
|
+ search_key = wechat_key[0]
|
|
|
+ openid = wechat_key[-1]
|
|
|
+ end_ymd = (date.today() + timedelta(days=0)).strftime("%Y%m%d")
|
|
|
+ start_ymd = (date.today() + timedelta(days=-7)).strftime("%Y%m%d")
|
|
|
url = "https://search.weixin.qq.com/cgi-bin/wxaweb/wxindex"
|
|
|
payload = json.dumps({
|
|
|
"openid": openid,
|
|
@@ -52,13 +52,89 @@ class Weixinzhishu:
|
|
|
'Referer': 'https://servicewechat.com/wxc026e7662ec26a3a/42/page-frame.html'
|
|
|
}
|
|
|
response = requests.request("POST", url, headers=headers, data=payload)
|
|
|
- if response.json()['code'] == -10000:
|
|
|
- print(response.text)
|
|
|
+ if response.json()['code'] != 0 and response.json()['code'] != -10002:
|
|
|
+ Common.logger(log_type, crawler).warning(f"response:{response.text}\n")
|
|
|
+ elif response.json()['code'] == -10002:
|
|
|
+ # 数据写入飞书
|
|
|
+ now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time())))
|
|
|
+ values = [[now, word, "该词暂未收录"]]
|
|
|
+ Feishu.insert_columns(log_type, crawler, "5011a2", "ROWS", 1, 2)
|
|
|
+ time.sleep(0.5)
|
|
|
+ Feishu.update_values(log_type, crawler, "5011a2", "F2:Z2", values)
|
|
|
+ Common.logger(log_type, crawler).info(f'热词"{word}"微信指数数据写入飞书成功\n')
|
|
|
+
|
|
|
+ word_wechat_score_dict = {
|
|
|
+ "id": word_id,
|
|
|
+ "word": word,
|
|
|
+ "wechatScores": [],
|
|
|
+ }
|
|
|
+ # print(word_wechat_score_dict)
|
|
|
+ return word_wechat_score_dict
|
|
|
else:
|
|
|
- print(response.text)
|
|
|
time_index = response.json()['content']['resp_list'][0]['indexes'][0]['time_indexes']
|
|
|
- print(time_index)
|
|
|
+ wechat_score_list = []
|
|
|
+ for i in range(len(time_index)):
|
|
|
+ score_time = time_index[i]['time']
|
|
|
+ score_time_str = f"{str(score_time)[:4]}-{str(score_time)[4:6]}-{str(score_time)[6:]}"
|
|
|
+ score = time_index[i]['score']
|
|
|
+ wechat_score_dict = {"score": score, "scoreDate": score_time_str}
|
|
|
+ wechat_score_list.append(wechat_score_dict)
|
|
|
+
|
|
|
+ # 数据写入飞书
|
|
|
+ now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time())))
|
|
|
+ values = [[now, word, score_time_str, score]]
|
|
|
+ Feishu.insert_columns(log_type, crawler, "5011a2", "ROWS", 1, 2)
|
|
|
+ time.sleep(0.5)
|
|
|
+ Feishu.update_values(log_type, crawler, "5011a2", "F2:Z2", values)
|
|
|
+ Common.logger(log_type, crawler).info(f'热词"{word}"微信指数数据写入飞书成功\n')
|
|
|
+
|
|
|
+ word_wechat_score_dict = {
|
|
|
+ "id": word_id,
|
|
|
+ "word": word,
|
|
|
+ "wechatScores": wechat_score_list,
|
|
|
+ }
|
|
|
+ # print(word_wechat_score_dict)
|
|
|
+ return word_wechat_score_dict
|
|
|
+ except Exception as e:
|
|
|
+ Common.logger(log_type, crawler).error(f"weixinzhishu异常:{e}\n")
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def get_weixinzhishu(cls, log_type, crawler):
|
|
|
+ our_word_list = []
|
|
|
+ out_word_list = []
|
|
|
+ our_word_sheet = Feishu.get_values_batch(log_type, 'weixinzhishu_search_word', "nCudsM")
|
|
|
+ out_word_sheet = Feishu.get_values_batch(log_type, 'weixinzhishu_search_word', "D80uEf")
|
|
|
+ for x in our_word_sheet:
|
|
|
+ for y in x:
|
|
|
+ if y is None:
|
|
|
+ pass
|
|
|
+ else:
|
|
|
+ our_word_list.append(y)
|
|
|
+ for x in out_word_sheet:
|
|
|
+ for y in x:
|
|
|
+ if y is None:
|
|
|
+ pass
|
|
|
+ else:
|
|
|
+ out_word_list.append(y)
|
|
|
+ word_list = our_word_list+out_word_list
|
|
|
+ word_score_list = []
|
|
|
+ # for i in range(len(word_list)):
|
|
|
+ for i in range(100):
|
|
|
+ word_score = cls.weixinzhishu(log_type, crawler, int(i+1), word_list[i])
|
|
|
+ word_score_list.append(word_score)
|
|
|
+ Common.logger(log_type, crawler).info(f'"{word_list[i]}"微信指数:{word_score}\n')
|
|
|
+
|
|
|
+ word_dict = {
|
|
|
+ "data": word_score_list
|
|
|
+ }
|
|
|
+ return word_dict
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
- Weixinzhishu.weixinzhishu('weixin', 'weixinzhishu')
|
|
|
+ # word_dict = Weixinzhishu.weixinzhishu('weixin', 'weixinzhishu', 1, "出大")
|
|
|
+ # print(word_dict)
|
|
|
+
|
|
|
+ word_dict_demo = Weixinzhishu.get_weixinzhishu('weixin', 'weixinzhishu')
|
|
|
+ print(word_dict_demo)
|
|
|
+
|
|
|
+ pass
|