Skip to content

Instantly share code, notes, and snippets.

@sgqy
Last active September 10, 2020 07:58
Show Gist options
  • Save sgqy/51548b788ec9077721c15f8c25960dba to your computer and use it in GitHub Desktop.
Save sgqy/51548b788ec9077721c15f8c25960dba to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Auto remove fans in bilibili.com
# 全天候移除 B 站粉丝, 彻底变成小透明
# 操作不可逆, 后果自负
# 用法: 把 B 站的 cookies 保存为 Netscape 格式的 cookie.txt 后, 再执行此脚本
# 另外, 稍作修改就可以变成自动关注或自动拉黑新增粉丝的奇怪脚本
# 已知的 act 代码:
# 1, 公开关注
# 2, 取消公开关注
# 3, 悄悄关注
# 4, 取消悄悄关注
# 5, 拉黑
# 6, 取消拉黑
# 7, 移除粉丝
import re
import requests
import json
import time
from datetime import datetime as dt
def parseCookieFile(cookiefile):
"""Parse a cookies.txt file and return a dictionary of key value pairs
compatible with requests."""
cookies = {}
with open (cookiefile, 'r') as fp:
for line in fp:
if not re.match(r'^\#', line):
lineFields = line.strip().split('\t')
cookies[lineFields[5]] = lineFields[6]
return cookies
def cleanStr(s):
return s.strip().replace('\r', '').replace('\n', '')
def printUser(u):
uid = str(u['mid'])
n = cleanStr(u['uname'])
t = dt.utcfromtimestamp(u['mtime']).strftime('%F %T')
now = dt.utcnow().strftime('%F %T')
print('[{}]\t{}\t{}\t({})'.format(now, uid, n, t))
cj = parseCookieFile('cookie.txt')
me = cj['DedeUserID']
print(me)
s = requests.Session()
s.cookies.update(cj)
while True:
# get a fan
uri = 'https://api.bilibili.com/x/relation/followers?ps=1&vmid=' + me
r = s.get(uri)
j = json.loads(r.text)
if j['code'] != 0:
print(r.text)
break
fans = j['data']['list']
if len(fans) < 1:
print('[{}]\t(no fans)'.format(dt.utcnow().strftime('%F %T')))
time.sleep(600) # wait for 10 minutes
continue
# delete the fan
printUser(fans[0])
d = {
"fid": fans[0]['mid'],
"act": 7,
"re_src": 11,
"csrf": cj['bili_jct'],
}
time.sleep(2)
r = s.post('https://api.bilibili.com/x/relation/modify', data = d)
j = json.loads(r.text)
if j['code'] != 0:
print(r.text)
break
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment