吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3878|回复: 33
收起左侧

[Python 原创] B站一键删除动态、关注、被回复的消息、被点赞的消息。

  [复制链接]
雨陌 发表于 2022-11-30 23:09
本帖最后由 雨陌 于 2022-11-30 23:12 编辑

注意,因为B站有限制,所以每次只删除两百条,删除过多ip会被封禁几分钟。其次,需要手动获取B站的Cookie。
import pprint
import requests
import json
import re
import os

# 最大删除条数
MAX_COUNT = 200
# 保存cookie的路径
COOKIE_FILE_PATH = r"./cookie.txt"

class BWebSite(object):

    def __init__(self):
        if not os.path.exists(COOKIE_FILE_PATH):
            print("未检测到cookie,请手动输入:")
            cookie = input()
            if re.search('DedeUserID=(.+?);', cookie) and re.search('bili_jct=(.+?);', cookie):
                with open(COOKIE_FILE_PATH, 'w', encoding='utf-8') as f:
                    f.write(cookie)
                print("*" * 50)
                print("输入成功".center(50))
                print("*" * 50)
            else:
                print('cookie输入错误')
                exit()
        else:
            with open(COOKIE_FILE_PATH, 'r', encoding='utf-8') as f:
                cookie = f.read()
                if not cookie:
                    print("cookie为空,请重新填入cookie")
                    os.remove(COOKIE_FILE_PATH)
                    exit()
        self.uid = re.search('DedeUserID=(.+?);', cookie)
        self.token = re.search('bili_jct=(.+?);', cookie)
        if not (self.uid and self.token):
            print("cookie错误,请重新填入")
            os.remove(COOKIE_FILE_PATH)
            exit()
        self.uid = self.uid.group(1)
        self.token = self.token.group(1)
        self.headers = {
            'authority': 'api.vc.bilibili.com',
            'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
            'accept': 'application/json, text/plain, */*',
            'sec-ch-ua-mobile': '?0',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36',
            'sec-ch-ua-platform': '"Windows"',
            'origin': 'https://space.bilibili.com',
            'sec-fetch-site': 'same-site',
            'sec-fetch-mode': 'cors',
            'sec-fetch-dest': 'empty',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cookie': f'{cookie}',
        }

    # 获取动态信息
    @property
    def dynamic(self):
        dynamic_id = 0
        params = (
            ('visitor_uid', f'{self.uid}'),
            ('host_uid', f'{self.uid}'),
            ('offset_dynamic_id', f'{dynamic_id}'),
            ('need_top', '1'),
            ('platform', 'web'),
        )
        list_cards = []
        response = requests.get('https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history',
                                headers=self.headers,
                                params=params)
        data = json.loads(response.text)['data']
        while data.get('cards'):
            cards = data['cards']
            dynamic_id = data['next_offset']
            for card in cards:
                list_cards.append(card)
            if len(list_cards) >= MAX_COUNT:
                return list_cards
            params = (
                ('visitor_uid', f'{self.uid}'),
                ('host_uid', f'{self.uid}'),
                ('offset_dynamic_id', f'{dynamic_id}'),
                ('need_top', '1'),
                ('platform', 'web'),
            )
            response = requests.get('https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history',
                                    headers=self.headers,
                                    params=params)
            data = json.loads(response.text)['data']
        return list_cards

    # 删除动态信息
    def delete_dynamic(self):
        list_dynamic_id = []
        for dynamic in self.dynamic:
            list_dynamic_id.append(dynamic['desc']['dynamic_id'])
        for dynamic_id in list_dynamic_id:
            data = {
                'dynamic_id': f'{dynamic_id}',
                'csrf_token': f'{self.token}',
                'csrf': f'{self.token}'
            }
            response = requests.post('https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/rm_dynamic',
                                     headers=self.headers,
                                     data=data)
            code = json.loads(response.text)['code']
            if code == 0:
                print(f"{dynamic_id}删除成功")

    # 获取所有回复
    @property
    def comments(self):
        params = (
            ('platform', 'web'),
            ('build', '0'),
            ('mobi_app', 'web'),
        )
        response = requests.get('https://api.bilibili.com/x/msgfeed/reply', headers=self.headers, params=params)
        ls = []
        data = json.loads(response.text)['data']
        while not data['cursor']['is_end']:
            ls.append(data)
            if len(ls) > MAX_COUNT:
                return ls
            cursor = data['cursor']
            comment_id = cursor['id']
            time = cursor['time']
            params = (
                ('id', f'{comment_id}'),
                ('reply_time', f'{time}'),
                ('platform', 'web'),
                ('build', '0'),
                ('mobi_app', 'web'),
            )
            response = requests.get('https://api.bilibili.com/x/msgfeed/reply', headers=self.headers, params=params)
            data = json.loads(response.text)['data']
        return ls

    # 删除回复
    def delete_comment(self):
        for comment in self.comments:
            for item in comment['items']:
                subject_id = item['item']['subject_id']
                target_id = item['item']['target_id']
                title = item['item']['title']
                data = {
                    'oid': f'{subject_id}',
                    'type': '1',
                    'jsonp': 'jsonp',
                    'rpid': f'{target_id}',
                    'csrf': f'{self.token}'
                }
                response = requests.post('https://api.bilibili.com/x/v2/reply/del', headers=self.headers, data=data)
                # data = json.loads(response.text)
                # print(data)
                code = json.loads(response.text)['code']
                if code == 0:
                    print(f"{title}-----------下面的回复已删除")

    # 获取所有收到赞的回复
    @property
    def likes(self):
        params = (
            ('platform', 'web'),
            ('build', '0'),
            ('mobi_app', 'web'),
        )
        response = requests.get('https://api.bilibili.com/x/msgfeed/like', headers=self.headers, params=params)
        ls = []
        data = json.loads(response.text)['data']
        total = json.loads(response.text)['data']['total']
        while not total['cursor']['is_end']:
            ls.append(data)
            if len(ls) > MAX_COUNT:
                return ls
            cursor = total['cursor']
            comment_id = cursor['id']
            time = cursor['time']
            params = (
                ('id', f'{comment_id}'),
                ('like_time', f'{time}'),
                ('platform', 'web'),
                ('build', '0'),
                ('mobi_app', 'web'),
            )
            response = requests.get('https://api.bilibili.com/x/msgfeed/like', headers=self.headers, params=params)
            data = json.loads(response.text)['data']
            total = json.loads(response.text)['data']['total']
        return ls

    def delete_like(self):
        for like in self.likes:
            for item in like['total']['items']:
                native_uri = item['item']['native_uri']
                item_id = item['item']['item_id']
                title = item['item']['title']
                rpid = item_id
                print(native_uri, title)
                oid = re.search("//video/(\d+)", native_uri)
                if not oid:
                    oid = re.search('//following/detail/(\d+)', native_uri)
                    if not oid:
                        oid = re.search('//album/(\d+)', native_uri)
                if not oid:
                    continue
                oid = oid.group(1)
                print(oid, rpid)
                data = {
                    'oid': f'{oid}',
                    'type': '1',
                    'jsonp': 'jsonp',
                    'rpid': f'{rpid}',
                    'csrf': f'{self.token}'
                }
                response = requests.post('https://api.bilibili.com/x/v2/reply/del', headers=self.headers, data=data)
                # data = json.loads(response.text)
                # print(data)
                code = json.loads(response.text)['code']
                if code == 0:
                    print(f"{title}-----------下面的回复已删除")

    # 获取所有关注
    @property
    def follows(self):
        vmid = f'{self.uid}'
        pn = 1
        list_follows = []
        h = self.headers
        h.update(
            {
                'referer': f'https://space.bilibili.com/{self.uid}/fans/follow?tagid=-10',
            }
        )
        while True:
            params = (
                ('vmid', f'{vmid}'),
                ('pn', f'{pn}'),
                ('ps', '20'),
                ('order', 'desc'),
                ('order_type', 'attention'),
                ('jsonp', 'jsonp'),
                ('callback', '__jp7'),
            )
            response = requests.get('https://api.bilibili.com/x/relation/followings', headers=h,
                                    params=params)
            data = json.loads(response.text[response.text.index('(') + 1:response.text.rindex(')')])['data']
            list_data = data['list']
            total = data['total']
            for item in list_data:
                list_follows.append(item)
            if total > MAX_COUNT and len(list_follows) == MAX_COUNT:
                break
            if len(list_follows) == total:
                break
            pn += 1
        return list_follows

    # 删除所有
    def delete_follows(self):
        follows = self.follows
        for follow in follows:
            mid = follow['mid']
            uname = follow['uname']
            data = {
                'fid': f'{mid}',
                'act': '2',
                're_src': '11',
                'spmid': '333.999.0.0',
                'extend_content': f'{{"entity":"user","entity_id":{mid}}}',
                'jsonp': 'jsonp',
                'csrf': f'{self.token}'
            }
            response = requests.post('https://api.bilibili.com/x/relation/modify', headers=self.headers, data=data)
            code = json.loads(response.text)['code']
            if code == 0:
                print(f'{uname}----------取消关注成功')

web = BWebSite()
# web.delete_comment()
# web.delete_like()
# web.delete_dynamic()
# pprint.pprint(web.follows)
# web.delete_follows()

免费评分

参与人数 9吾爱币 +7 热心值 +9 收起 理由
liyunzhe + 1 我很赞同!
FKRiX + 1 + 1 我很赞同!
balter + 1 + 1 回复的接口是不是不好用了获取不到回复内容
Bob5230 + 1 谢谢@Thanks!
Akichan1 + 1 + 1 谢谢@Thanks!
Linxiaomo + 1 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
skiss + 1 + 1 谢谢@Thanks!
且月九日序 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
麦子1995 + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| 雨陌 发表于 2022-12-1 13:18
wushouwen 发表于 2022-12-1 12:39
会不会有什么限制

限制倒是没有,抓到cookie就能删除了,但是短时间删除过快,B站那边会做一定的限制的。
 楼主| 雨陌 发表于 2022-12-1 13:21
weliong 发表于 2022-12-1 09:12
为什么要删除这些东西呢?

可以删除抽奖动态,或者所有的关注。抽奖信息太多手动删除很麻烦。
xiaoshu1688 发表于 2022-12-1 09:10
Ah129z 发表于 2022-12-1 09:12
感谢分享
weliong 发表于 2022-12-1 09:12
为什么要删除这些东西呢?
angle951 发表于 2022-12-1 09:13
这个好用!
lcg2014 发表于 2022-12-1 09:29
为什么要删除?网络交流不好吗?
qazqazwang 发表于 2022-12-1 09:48
lcg2014 发表于 2022-12-1 09:29
为什么要删除?网络交流不好吗?

抽奖哥正需要这个
orb001 发表于 2022-12-1 10:04
抽奖哥是谁啊?为啥需要大佬的这个啊。
独立勿扰 发表于 2022-12-1 10:12
weliong 发表于 2022-12-1 09:12
为什么要删除这些东西呢?

转发动态抽奖,从未缺席,从未中过。
lhp462 发表于 2022-12-1 10:23
动态抽奖的消息全部可以删掉了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-27 01:04

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表