nets_comment_class.py-网易单曲评论-music-163
'''
#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Software : PyCharm
# @CreateTime: 2022-03-23 14:38
# @Author : Lenovo
# @File : nets_comment_class.py
'''
import re, json, time
import requests
import base64
from Crypto.Cipher import AES
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
class Class_comment():
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36',
'Referer': 'https://music.163.com',
'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'http://music.163.com',
'Host': 'music.163.com'
}
def __init__(self, song_id):
self.song_id = song_id
self.encSecKey = '5dec9ded1d7223302cc7db8d7e0428b04139743ab7e3d451ae47837f34e66f9a86f63e45ef20d147c33d88530a6c3c9d9d88e38586b42ee30ce43fbf3283a2b10e3118b76e11d6561d80e33ae38deb96832b1a358665c0579b1576b21f995829d45fc43612eede2ac243c6ebb6c2d16127742f3ac913d3ac7d6026b44cee424e'
self.h_encText = self.get_encText()
self.url_post_api = 'https://music.163.com/weapi/comment/resource/comments/get?csrf_token=&song_id={}'.format(self.song_id)
def get_api(self):
dict_api = {
'url': self.url_post_api,
'data': {
'params': self.h_encText,
'encSecKey': self.encSecKey,
},
}
str_api = json.dumps(dict_api)
return str_api
def AES_encrypt(self, text, key, iv):
pad = 16 - len(text) % 16
text = text + pad * chr(pad)
text = text.encode('utf-8')
encryptor = AES.new(key.encode('utf-8'), AES.MODE_CBC, iv)
encrypt_text = encryptor.encrypt(text)
encrypt_text = base64.b64encode(encrypt_text)
return encrypt_text.decode('utf-8')
def get_param1(self):
rid = 'R_SO_4_29004400'
rid = 'R_SO_4_{}'.format(self.song_id)
curr_time = int(time.time() * 1000)
dict_param = {
'csrf_token': '',
'cursor': str(curr_time),
'offset': '0',
'orderType': '2',
'pageNo': '1',
'pageSize': '20',
'rid': rid,
'threadId': rid,
}
param1 = json.dumps(dict_param)
return param1
def get_encText(self):
param1 = self.get_param1()
vi = b'0102030405060708'
key = '0CoJUm6Qyw8W8jud'
h_encText = self.AES_encrypt(param1, key, vi)
rand_num = 'aq9d7cvBOJ1tzj1o'
h_encText = self.AES_encrypt(h_encText, rand_num, vi)
return h_encText
def get_total_comment_count(self):
param_data = {
'params': self.h_encText,
'encSecKey': self.encSecKey,
}
res = requests.post(
self.url_post_api,
headers=self.headers,
data=param_data,
verify=False
)
json_res = res.json()
totalCount = json_res['data']['totalCount']
return totalCount
if __name__ == '__main__':
obj = Class_comment(1930213637)
total = obj.get_total_comment_count()
print(total)