下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:7762
代码实现包含4个模块:主协议工具类、辅助工具函数、主程序入口和配置文件。这些代码模拟了主流社交平台的评论和点赞协议交互,包含签名生成、加密解密等核心功能。使用时需要替换config.py中的cookie信息。
import requests
import json
import time
import hashlib
import random
from urllib.parse import quote
class SocialAPI:
def init(self):
self.session = requests.Session()
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Accept-Language': 'zh-CN,zh;q=0.9'
}
def _generate_signature(self, params, secret_key):
"""通用签名生成方法"""
param_str = '&'.join([f'{k}={v}' for k,v in sorted(params.items())])
return hashlib.md5((param_str + secret_key).encode()).hexdigest()
def douyin_comment_like(self, video_id, comment_id, cookie):
"""抖音评论点赞协议"""
url = "https://www.douyin.com/aweme/v1/comment/digg/"
params = {
'aweme_id': video_id,
'comment_id': comment_id,
'action_type': '1', # 1点赞 0取消
'channel': '0',
'_rticket': int(time.time()*1000)
}
headers = self.headers.copy()
headers['Cookie'] = cookie
response = self.session.post(url, params=params, headers=headers)
return response.json()
def xiaohongshu_comment(self, note_id, content, cookie):
"""小红书评论协议"""
url = "https://edith.xiaohongshu.com/api/sns/web/v1/comment/post"
data = {
"note_id": note_id,
"content": content,
"at_users": []
}
headers = self.headers.copy()
headers.update({
'Cookie': cookie,
'Content-Type': 'application/json',
'X-Sign': self._generate_xhs_sign(note_id)
})
response = self.session.post(url, json=data, headers=headers)
return response.json()
def _generate_xhs_sign(self, note_id):
"""小红书签名生成"""
t = int(time.time())
nonce = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz0123456789', k=16))
data = f"note_id={note_id}&t={t}&nonce={nonce}"
return hashlib.md5(data.encode()).hexdigest()
def kuaishou_comment(self, photo_id, content, cookie):
"""快手评论协议"""
url = "https://gdfp.gifshow.com/rest/n/comment/publish"
params = {
'photoId': photo_id,
'content': content,
'client_key': '3c2cd3f3',
'os': 'android',
'ver': '9.3.30',
'c': 'GENERIC',
'net': 'WIFI',
'sys': 'ANDROID_9',
'app': '0',
'mod': 'Xiaomi(Mi 10)',
'country_code': 'cn',
'sig': self._generate_kuaishou_sig(photo_id)
}
headers = self.headers.copy()
headers['Cookie'] = cookie
response = self.session.post(url, params=params, headers=headers)
return response.json()
def bilibili_comment(self, oid, content, cookie, type_=1):
"""哔哩哔哩评论协议"""
url = "https://api.bilibili.com/x/v2/reply/add"
data = {
'oid': oid,
'type': type_,
'message': content,
'plat': '1',
'csrf': self._get_csrf(cookie)
}
headers = self.headers.copy()
headers.update({
'Cookie': cookie,
'Referer': 'https://www.bilibili.com/'
})
response = self.session.post(url, data=data, headers=headers)
return response.json()
def _get_csrf(self, cookie):
"""从cookie中提取csrf token"""
for item in cookie.split(';'):
if 'bili_jct' in item:
return item.split('=')[1]
return ''
def weibo_comment(self, weibo_id, content, cookie):
"""微博评论协议"""
url = "https://weibo.com/ajax/comments/create"
data = {
'id': weibo_id,
'content': content,
'mid': weibo_id,
'is_repost': '0',
'location': 'page_100505_home'
}
headers = self.headers.copy()
headers.update({
'Cookie': cookie,
'X-Requested-With': 'XMLHttpRequest',
'Referer': f'https://weibo.com/{weibo_id}'
})
response = self.session.post(url, data=data, headers=headers)
return response.json()
re
import base64
from Crypto.Cipher import AES
class SocialUtils:
@staticmethod
def decrypt_weibo_data(encrypted_data, key):
"""微博数据解密方法"""
iv = encrypted_data[:16].encode()
cipher = AES.new(key.encode(), AES.MODE_CBC, iv)
decrypted = cipher.decrypt(base64.b64decode(encrypted_data[16:]))
return decrypted.decode('utf-8').strip()
@staticmethod
def extract_video_id(url):
"""从URL中提取视频ID"""
patterns = {
'douyin': r'douyin\.com/video/(\d+)',
'xiaohongshu': r'xhslink\.com/(\w+)',
'kuaishou': r'gifshow\.com/fw/photo/(\w+)',
'bilibili': r'bilibili\.com/video/(BV\w+)',
'weibo': r'weibo\.com/\d+/(\w+)'
}
for platform, pattern in patterns.items():
match = re.search(pattern, url)
if match:
return platform, match.group(1)
return None, None
@staticmethod
def generate_device_id():
"""生成模拟设备ID"""
return ''.join([
random.choice('0123456789abcdef') for _ in range(16)
]).upper()
@staticmethod
def parse_cookie(cookie_str):
"""解析cookie字符串为字典"""
return dict(
item.strip().split('=', 1)
for item in cookie_str.split(';')
if '=' in item
)