Skip to content

Commit

Permalink
升级观看组件
Browse files Browse the repository at this point in the history
  • Loading branch information
Hsury committed Aug 30, 2018
1 parent e0ea072 commit 3661477
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
6 changes: 3 additions & 3 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-2018.8.28-green.svg?longCache=true&style=for-the-badge">
<img src="https://img.shields.io/badge/version-2018.8.30-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">
</p>

Expand All @@ -19,7 +19,7 @@
|query |2018/8/28 |获取用户信息 |
|setPrivacy |2018/7/24 |修改隐私设置 |
|silver2Coins |2018/8/8 |银瓜子兑换硬币 |
|watch |2018/6/20 |观看 |
|watch |2018/8/30 |观看 |
|like |2018/7/8 |好评 |
|reward |2018/6/20 |投币 |
|favour |2018/6/20 |收藏 |
Expand All @@ -35,7 +35,7 @@
|mallLottery |2018/6/23 |会员购周年庆活动抽奖 |
|mallPrize |2018/6/23 |会员购周年庆活动中奖查询 |
|mi6XLottery |2018/6/30 |小米6X抢F码活动抽奖 |
|liveTool |2018/8/25 |直播助手 |
|liveTool |2018/8/30 |直播助手 |

*注:liveTool直播助手组件编译自[yjqiang/bili2.0](https://github.com/yjqiang/bili2.0)*

Expand Down
72 changes: 49 additions & 23 deletions bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, https=True):
self.cookie = ""
self.csrf = ""
self.uid = ""
self.sid = ""
self.accessToken = ""
self.refreshToken = ""
self.username = ""
Expand All @@ -48,7 +49,7 @@ def __init__(self, https=True):
def log(self, message):
print(f"[{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))}][{self.uid}] {message}", flush=True)

def get(self, url, headers=None, decodeLevel=2, timeout=10, retry=5):
def get(self, url, headers=None, decodeLevel=2, timeout=15, retry=10):
for i in range(retry + 1):
try:
response = requests.get(url, headers=headers, timeout=timeout, proxies=self.proxy)
Expand All @@ -58,7 +59,7 @@ def get(self, url, headers=None, decodeLevel=2, timeout=10, retry=5):
self.setProxy()
return None

def post(self, url, data=None, headers=None, decodeLevel=2, timeout=10, retry=5):
def post(self, url, data=None, headers=None, decodeLevel=2, timeout=15, retry=10):
for i in range(retry + 1):
try:
response = requests.post(url, data=data, headers=headers, timeout=timeout, proxies=self.proxy)
Expand Down Expand Up @@ -92,6 +93,8 @@ def importCredential(self, pairs):
self.csrf = value
elif key == "DedeUserID":
self.uid = value
elif key == "sid":
self.sid = value
self.cookie = f"{self.cookie}{key}={value};"
elif key == "accessToken":
self.accessToken = value
Expand All @@ -104,7 +107,7 @@ def importCredential(self, pairs):

# 登录
def login(self):
def useCookie():
def byCookie():
url = f"{self.protocol}://api.bilibili.com/x/space/myinfo"
headers = {'Cookie': self.cookie,
'Host': "api.bilibili.com",
Expand All @@ -118,7 +121,7 @@ def useCookie():
self.log("Cookie已失效")
return False

def useToken():
def byToken():
param = f"access_key={self.accessToken}&appkey={Bilibili.appKey}&ts={int(time.time())}"
url = f"{self.protocol}://passport.bilibili.com/api/v2/oauth2/info?{param}&sign={self.getSign(param)}"
response = self.get(url)
Expand All @@ -143,17 +146,18 @@ def useToken():
self.cookie = "".join(f"{i['name']}={i['value']};" for i in response['data']['cookie_info']['cookies'])
self.csrf = response['data']['cookie_info']['cookies'][0]['value']
self.uid = response['data']['cookie_info']['cookies'][1]['value']
self.sid = response['data']['cookie_info']['cookies'][3]['value']
self.accessToken = response['data']['token_info']['access_token']
self.refreshToken = response['data']['token_info']['refresh_token']
self.log(f"Token刷新成功, 有效期至{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + int(response['data']['expires_in'])))}")
self.log(f"Token刷新成功, 有效期至{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + int(response['data']['token_info']['expires_in'])))}")
return True
else:
self.accessToken = ""
self.refreshToken = ""
self.log("Token刷新失败")
return False

def usePassword():
def byPassword():
url = f"{self.protocol}://passport.bilibili.com/api/oauth2/getKey"
data = {'appkey': Bilibili.appKey,
'sign': self.getSign(f"appkey={Bilibili.appKey}")}
Expand Down Expand Up @@ -202,6 +206,7 @@ def usePassword():
self.cookie = "".join(f"{i['name']}={i['value']};" for i in response['data']['cookie_info']['cookies'])
self.csrf = response['data']['cookie_info']['cookies'][0]['value']
self.uid = response['data']['cookie_info']['cookies'][1]['value']
self.sid = response['data']['cookie_info']['cookies'][3]['value']
self.accessToken = response['data']['token_info']['access_token']
self.refreshToken = response['data']['token_info']['refresh_token']
self.log(f"{self.username}登录成功")
Expand All @@ -216,11 +221,11 @@ def usePassword():
else:
time.sleep(30)

if self.cookie and useCookie():
if self.cookie and byCookie():
return True
elif self.accessToken and self.refreshToken and useToken():
elif self.accessToken and self.refreshToken and byToken():
return True
elif self.username and self.password and usePassword():
elif self.username and self.password and byPassword():
return True
else:
return False
Expand Down Expand Up @@ -331,31 +336,52 @@ def watch(self, aid):
response = self.get(url)
if response:
cid = response['data']['cid']
duration = response['data']['duration']
else:
self.log(f"av{aid}信息解析失败")
return False
url = f"{self.protocol}://api.bilibili.com/x/report/web/heartbeat"
url = f"{self.protocol}://api.bilibili.com/x/report/click/h5"
data = {'aid': aid,
'cid': cid,
'part': 1,
'did': self.sid,
'ftime': int(time.time()),
'jsonp': "jsonp",
'lv': None,
'mid': self.uid,
'csrf': self.csrf,
'played_time': 0,
'realtime': 0,
'start_ts': int(time.time()),
'type': 3,
'dt': 2,
'play_type': 1}
'stime': int(time.time())}
headers = {'Cookie': self.cookie,
'Host': "api.bilibili.com",
'Origin': "https://www.bilibili.com",
'Referer': f"https://www.bilibili.com/video/av{aid}",
'User-Agent': Bilibili.ua}
response = self.post(url, data=data, headers=headers)
if response and response['code'] == 0 or response is None:
self.log(f"av{aid}观看成功")
return True
else:
self.log(f"av{aid}观看失败 {response}")
return False
if response and response.get("code") == 0:
url = f"{self.protocol}://api.bilibili.com/x/report/web/heartbeat"
data = {'aid': aid,
'cid': cid,
'jsonp': "jsonp",
'mid': self.uid,
'csrf': self.csrf,
'played_time': 0,
'pause': False,
'realtime': duration,
'dt': 7,
'play_type': 1,
'start_ts': int(time.time())}
response = self.post(url, data=data, headers=headers)
if response and response.get("code") == 0:
time.sleep(5)
data['played_time'] = duration - 1
data['play_type'] = 0
data['start_ts'] = int(time.time())
response = self.post(url, data=data, headers=headers)
if response and response.get("code") == 0:
self.log(f"av{aid}观看成功")
return True
self.log(f"av{aid}观看失败 {response}")
return False

# 好评
def like(self, aid):
Expand Down Expand Up @@ -1059,7 +1085,7 @@ def main():
LiveToolCurrentCommit = f.read()
except:
LiveToolCurrentCommit = None
liveToolLatestCommit = LiveToolCurrentCommit if LiveToolCurrentCommit else "77c4c85"
liveToolLatestCommit = LiveToolCurrentCommit if LiveToolCurrentCommit else "058fac1"
if config['liveTool']['autoUpdate']:
try:
liveToolLatestCommit = requests.get("https://api.github.com/repos/Hsury/Bilibili-Live-Tool/releases/latest").json()['tag_name']
Expand Down
4 changes: 2 additions & 2 deletions bilibili.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ account = """
[proxy]
enable = true # 开关
https = true # 使用HTTPS协议
pool = ["140.143.96.216:80", "118.24.61.22:3128", "113.200.56.13:8010", "221.7.255.168:8080", "223.93.172.248:3128", "221.7.255.168:80", "125.62.26.197:3128", "39.135.35.19:80", "39.106.160.36:3128", "119.63.99.205:3128", "39.135.35.18:80", "118.31.223.194:3128", "218.60.8.99:3129", "47.94.241.237:3128", "202.116.64.201:80","116.62.194.248:3128", "58.49.72.141:8888", "183.62.196.10:3128", "120.78.215.151:808", "218.207.212.86:80", "114.215.95.188:3128", "52.230.0.119:3128", "39.135.35.16:80", "218.66.253.146:8800", "35.185.179.37:3128", "154.8.182.226:3128", "119.196.18.50:8080", "112.126.65.236:80", "119.27.177.169:80", "221.219.147.182:3128", "121.152.17.96:3128", "101.37.27.173:3128", "185.22.174.69:10010", "119.36.221.13:8888", "185.22.174.69:1448", "118.27.29.54:3128", "159.138.21.53:3128", "94.242.58.108:10010", "173.212.192.72:3128", "159.89.22.167:8080", "178.128.46.132:8080", "194.67.201.106:3128", "94.242.58.14:10010", "111.47.192.141:8888","178.128.37.162:8080", "178.128.208.145:8080", "113.57.178.141:8888", "209.97.167.247:8080", "206.189.144.21:8080", "178.62.51.192:8080", "178.128.47.113:8080", "207.154.208.76:8080", "178.128.39.212:8080", "188.165.164.9:3128", "109.205.201.3:3128", "103.15.51.160:8080", "178.128.91.23:8080", "121.156.109.92:8080", "95.87.255.120:8080", "203.86.26.9:3128", "167.99.197.73:8080", "94.242.58.108:1448", "185.22.174.65:1448", "5.196.224.39:8080", "46.101.188.94:8080", "178.128.87.158:8080", "91.185.191.163:3128", "95.87.220.19:15600", "217.182.103.42:3128","94.242.58.142:10010", "43.239.178.90:8888", "94.242.57.136:10010", "91.121.208.196:3128", "185.216.35.170:3128", "159.89.118.85:8080", "185.22.174.65:10010", "203.150.192.86:46200", "159.89.126.130:8080", "206.81.13.127:8080", "178.128.247.187:8080", "188.120.238.38:5555", "217.182.119.131:3128", "51.15.229.200:3128", "157.65.28.91:3128", "64.33.247.157:3128", "112.140.186.70:8118", "91.121.108.164:3128", "5.189.162.175:3128", "167.99.156.71:8080", "178.176.28.164:8080", "188.166.69.172:8080", "188.166.43.167:8080", "142.93.199.214:8080", "101.37.79.125:3128", "199.119.1.133:80", "122.50.6.226:8080", "94.242.57.136:1448", "178.128.87.55:8080", "64.33.247.157:8080", "5.135.164.72:3128"] # 代理池
pool = ["118.24.61.22:3128", "140.143.96.216:80", "221.7.255.168:8080", "183.220.43.89:8080", "221.7.255.168:80", "47.94.241.237:3128", "119.27.177.169:80", "39.135.35.19:80", "125.62.26.197:3128", "39.135.35.18:80", "113.200.56.13:8010", "223.93.172.248:3128", "218.60.8.83:3129", "218.60.8.98:3129", "223.167.8.211:3128", "47.96.239.158:3128", "116.62.194.248:3128", "118.31.223.194:3128", "218.60.58.66:80", "183.62.196.10:3128", "114.215.95.188:3128", "52.230.0.119:3128", "35.185.179.37:3128", "218.207.212.86:80", "120.78.215.151:808", "47.93.18.195:80", "39.135.35.16:80", "221.229.18.220:3128", "203.174.90.201:8080", "118.24.121.231:3128", "111.248.194.64:8080", "154.8.182.226:3128", "112.95.89.38:53281", "112.126.65.236:80", "39.104.175.142:3128", "13.228.173.226:8080", "94.242.58.108:10010", "35.185.201.225:8080", "185.22.174.69:10010", "118.27.29.54:3128", "94.242.58.142:1448", "94.242.57.136:1448", "94.242.57.136:10010", "37.17.229.158:3128", "88.191.33.53:3128", "103.246.2.244:8080", "207.154.208.76:8080", "119.196.18.50:8080", "46.101.188.94:8080", "221.219.147.182:3128", "185.22.174.69:1448", "157.65.28.91:3128", "94.242.58.14:10010", "173.212.192.72:3128", "159.89.22.167:8080", "112.140.186.70:8118", "94.242.55.108:10010", "206.189.84.37:8000", "37.46.71.202:3128", "74.82.50.155:3128", "178.128.104.237:8080", "178.128.93.209:8080", "178.128.83.149:8080", "183.2.212.2:3128", "178.128.111.250:8080", "178.128.83.145:8080", "178.128.87.244:8080", "183.151.41.104:3128", "178.128.208.145:8080", "178.128.83.197:8080", "47.91.225.36:3128", "209.97.164.154:8080", "185.216.35.170:3128", "178.128.83.193:8080", "178.128.104.169:8080", "203.154.82.167:8080", "95.87.255.120:8080", "178.128.60.172:8080", "178.128.91.238:8080", "157.65.25.141:3129", "178.128.247.187:8080", "159.89.118.85:8080", "159.203.45.186:8080", "188.166.43.167:8080", "206.81.13.127:8080", "159.89.126.130:8080", "178.128.248.112:8080", "128.199.54.182:8080", "188.166.69.172:8080", "188.166.33.184:8080", "82.151.220.118:3128", "118.31.0.140:3128", "5.135.164.72:3128", "167.99.197.73:8080", "142.93.51.106:8080", "178.128.39.212:8080", "222.221.11.119:3128", "178.62.95.149:8080", "202.116.64.201:80", "142.93.199.216:8080"] # 代理池

# 获取用户信息
[query]
Expand All @@ -36,7 +36,7 @@ enable = true # 开关

# 观看
[watch]
enable = false # 开关
enable = true # 开关
aid = [29278585, 20032006, 14594803] # 稿件av号

# 好评
Expand Down

0 comments on commit 3661477

Please sign in to comment.