Skip to content

Commit

Permalink
加入BV号自动转换为AV号的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
1574242600 committed Jul 20, 2020
1 parent 2278006 commit ad9ce6f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if: 'tag IS blank'
env:
global:
- TRAVIS_TAG=v2.3
- TRAVIS_TAG=v2.4
jobs:
include:
-
Expand Down
23 changes: 9 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<h1 align="center">- Bilibili Toolkit -</h1>

<p align="center">
<img src="https://img.shields.io/badge/version-2020.7.4-green.svg?longCache=true&style=for-the-badge">
<img src="https://img.shields.io/badge/version-2020.7.20-green.svg?longCache=true&style=for-the-badge">
<img src="https://img.shields.io/badge/license-SATA-blue.svg?longCache=true&style=for-the-badge">
<img src="https://img.shields.io/travis/com/Hsury/Bilibili-Toolkit?style=for-the-badge">
</p>
Expand All @@ -24,15 +24,15 @@
|get_user_info |2019/9/15 |获取用户信息 |
|set_privacy |2018/7/24 |修改隐私设置 |
|silver_to_coin |2018/8/8 |银瓜子兑换硬币 |
|watch |2018/8/30 |观看 |
|like |2018/7/8 |点赞 |
|reward |2018/11/22 |投币 |
|favour |2018/6/20 |收藏 |
|combo |2018/12/18 |三连推荐 |
|share |2018/6/20 |分享 |
|watch |2020/7/20 |观看 |
|like |2020/7/20 |点赞 |
|reward |2020/7/20 |投币 |
|favour |2020/7/20 |收藏 |
|combo |2020/7/20 |三连推荐 |
|share |2020/7/20 |分享 |
|follow |2018/7/8 |关注 |
|follow_batch |2020/7/2 |批量关注 |
|danmaku_post |2019/3/11 |弹幕发送 |
|danmaku_post |2020/7/20 |弹幕发送 |
|comment_like |2018/6/27 |评论点赞 |
|comment_post |2019/8/3 |评论发表 |
|dynamic_like |2018/6/29 |动态点赞 |
Expand Down Expand Up @@ -64,7 +64,7 @@ cd Bilibili-Toolkit
nano config.toml
```

2. 安装Python 3.6/3.7,并使用pip安装依赖
2. 安装Python 3.6或更高版本,并使用pip安装依赖

```
pip install -r requirements.txt -U -i https://pypi.tuna.tsinghua.edu.cn/simple
Expand Down Expand Up @@ -104,14 +104,9 @@ QQ群:[956399361](https://jq.qq.com/?_wv=1027&k=5BO0c7o)

## 捐赠

作者在本项目的开发过程中投入了大量的时间与精力,且验证码识别服务器的运行也需要一定的成本

若本项目帮助到了您,为您带来了直接或间接的收益,不要吝啬请我喝几杯~~妹汁~~喔 (=・ω・=)

<p align="center">
<img src="https://cdn.kagamiz.com/Bilibili-Toolkit/donate_alipay.png" width="250">
<img src="https://cdn.kagamiz.com/Bilibili-Toolkit/donate_wechat.png" width="250">
<img src="https://cdn.kagamiz.com/Bilibili-Toolkit/donate_alipay_redpacket.png" width="250">
</p>

## 鸣谢
Expand Down
48 changes: 47 additions & 1 deletion bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import base64
import chardet
import functools
import hashlib
import json
import os
Expand All @@ -40,7 +41,7 @@
__author__ = "Hsury"
__email__ = "[email protected]"
__license__ = "SATA"
__version__ = "2020.7.4"
__version__ = "2020.7.20"

class Bilibili:
app_key = "bca7e84c2d947ac6"
Expand Down Expand Up @@ -112,6 +113,26 @@ def _solve_captcha(self, image):
response = self._requests("post", url, json=payload)
return response['message'] if response and response.get("code") == 0 else None

def __bvid_handle(args_index=None, kwargs_key="aid"):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
self = args[0]
if args_index is not None and args_index < len(args):
result = Bilibili.bvid_to_aid(args[args_index])
if result:
args = list(args)
self._log(f"{args[args_index]}被自动转换为av{result}")
args[args_index] = result
if kwargs_key is not None and kwargs_key in kwargs:
result = Bilibili.bvid_to_aid(kwargs[kwargs_key])
if result:
self._log(f"{kwargs[kwargs_key]}被自动转换为av{result}")
kwargs[kwargs_key] = result
return func(*args, **kwargs)
return wrapper
return decorator

def __push_to_queue(self, manufacturer, data):
if self.__queue:
self.__queue.put({
Expand All @@ -121,6 +142,24 @@ def __push_to_queue(self, manufacturer, data):
'data': data,
})

@staticmethod
def bvid_to_aid(bvid="BV17x411w7KC"):
# Snippet source: https://www.zhihu.com/question/381784377/answer/1099438784
table = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF"
tr = {}
for i in range(58):
tr[table[i]] = i
s = [11, 10, 3, 8, 4, 6]
xor = 177451812
add = 8728348608
r = 0
try:
for i in range(6):
r += tr[bvid[s[i]]] * 58 ** i
return (r - add) ^ xor
except:
return None

@staticmethod
def calc_sign(param):
salt = "60698ba2f68e01ce44738920a0ffe768"
Expand Down Expand Up @@ -381,6 +420,7 @@ def silver_to_coin(self, app=True, pc=False):
self._log(f"银瓜子兑换硬币(PC通道)失败 {response}")

# 观看
@__bvid_handle(1, "aid")
def watch(self, aid):
# aid = 稿件av号
url = f"{self.protocol}://api.bilibili.com/x/web-interface/view?aid={aid}"
Expand Down Expand Up @@ -439,6 +479,7 @@ def watch(self, aid):
return False

# 点赞
@__bvid_handle(1, "aid")
def like(self, aid):
# aid = 稿件av号
url = f"{self.protocol}://api.bilibili.com/x/web-interface/archive/like"
Expand All @@ -461,6 +502,7 @@ def like(self, aid):
return False

# 投币
@__bvid_handle(1, "aid")
def reward(self, aid, double=True):
# aid = 稿件av号
# double = 双倍投币
Expand All @@ -485,6 +527,7 @@ def reward(self, aid, double=True):
return self.reward(aid, False) if double else False

# 收藏
@__bvid_handle(1, "aid")
def favour(self, aid):
# aid = 稿件av号
url = f"{self.protocol}://api.bilibili.com/x/v2/fav/folder"
Expand Down Expand Up @@ -516,6 +559,7 @@ def favour(self, aid):
return False

# 三连推荐
@__bvid_handle(1, "aid")
def combo(self, aid):
# aid = 稿件av号
url = f"{self.protocol}://api.bilibili.com/x/web-interface/archive/like/triple"
Expand All @@ -537,6 +581,7 @@ def combo(self, aid):
return False

# 分享
@__bvid_handle(1, "aid")
def share(self, aid):
# aid = 稿件av号
url = f"{self.protocol}://api.bilibili.com/x/web-interface/share/add"
Expand Down Expand Up @@ -606,6 +651,7 @@ def follow_batch(self, mids):
return False

# 弹幕发送
@__bvid_handle(1, "aid")
def danmaku_post(self, aid, message, page=1, moment=-1):
# aid = 稿件av号
# message = 弹幕内容
Expand Down
22 changes: 11 additions & 11 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,33 @@ enable = false # 开关
# 观看
[watch]
enable = false # 开关
aid = [120040, 21322566] # 稿件av号
aid = ["120040", "21322566", "BV17x411w7KC"] # 稿件av/bv号

# 点赞
[like]
enable = false # 开关
aid = [120040, 21322566] # 稿件av号
aid = ["120040", "21322566", "BV17x411w7KC"] # 稿件av/bv号

# 投币
[reward]
enable = false # 开关
aid = [120040, 21322566] # 稿件av号
double = [false, true] # 双倍投币
aid = ["120040", "21322566", "BV17x411w7KC"] # 稿件av/bv号
double = [false, true, false] # 双倍投币

# 收藏
[favour]
enable = false # 开关
aid = [120040, 21322566] # 稿件av号
aid = ["120040", "21322566", "BV17x411w7KC"] # 稿件av/bv号

# 三连推荐
[combo]
enable = false # 开关
aid = [120040, 21322566] # 稿件av号
aid = ["120040", "21322566", "BV17x411w7KC"] # 稿件av/bv号

# 分享
[share]
enable = false # 开关
aid = [120040, 21322566] # 稿件av号
aid = ["120040", "21322566", "BV17x411w7KC"] # 稿件av/bv号

# 关注
[follow]
Expand All @@ -85,10 +85,10 @@ mid = [2, 208259] # 被关注用户UID
# 弹幕发送
[danmaku_post]
enable = false # 开关
aid = [120040, 21322566] # 稿件av号
message = ["哔哩哔哩 (゜-゜)つロ 干杯~", "哔哩哔哩 (゜-゜)つロ 干杯~"] # 弹幕内容
page = [1, 1] # 分P
moment = [-1, -1] # 弹幕发送时间(为-1则随机选择发送时间)
aid = ["120040", "21322566", "BV17x411w7KC"] # 稿件av/bv号
message = ["哔哩哔哩 (゜-゜)つロ 干杯~", "哔哩哔哩 (゜-゜)つロ 干杯~", "哔哩哔哩 (゜-゜)つロ 干杯~"] # 弹幕内容
page = [1, 1, 1] # 分P
moment = [-1, -1, -1] # 弹幕发送时间(为-1则随机选择发送时间)

# 评论点赞
[comment_like]
Expand Down

0 comments on commit ad9ce6f

Please sign in to comment.