nets_comment_class.py-网易单曲评论-music-163

nets_comment_class.py-网易单曲评论-music-163

# -*- coding=utf-8 -*-
'''
#!/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']
        # url_song = 'https://music.163.com/#/song?id={}'.format(self.song_id)
        # print(url_song, totalCount)
        return totalCount


if __name__ == '__main__':
    obj = Class_comment(1930213637)
    total = obj.get_total_comment_count()
    print(total)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值