add bilibiliupmonitor

This commit is contained in:
CharlesPikachu
2022-03-09 15:39:43 +08:00
parent bb5a21fb5f
commit fafe52ea96
5 changed files with 188 additions and 3 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ __author__ = 'Charles'
'''标题'''
__title__ = 'DecryptLogin'
'''描述'''
__description__ = 'APIs for loginning some websites by using requests.'
__description__ = 'DecryptLogin: APIs for loginning some websites by using requests.'
'''链接'''
__url__ = 'https://github.com/CharlesPikachu/DecryptLogin'
'''邮件'''
+1
View File
@@ -104,6 +104,7 @@ If I find that anyone leverage this project in an illegal way, I will delete thi
| modifymihealthsteps | [click](https://mp.weixin.qq.com/s/TQLM9GIW50UWAsKoXb7pzQ) | [click](https://github.com/CharlesPikachu/DecryptLogin/tree/master/examples/modifymihealthsteps) | 修改小米运动中的步数 |
| taobaosnap | [click](https://mp.weixin.qq.com/s/vCZYtynHtQAOuQJHvjhpWA) | [click](https://github.com/CharlesPikachu/DecryptLogin/tree/master/examples/taobaosnap) | 淘宝抢购脚本 |
| jingdongsnap | [click](https://mp.weixin.qq.com/s/-H8bwuUIPDi41d09tTlvRw) | [click](https://github.com/CharlesPikachu/DecryptLogin/tree/master/examples/jingdongsnap) | 京东抢购脚本 |
| bilibiliupmonitor | [click]() | [click](https://github.com/CharlesPikachu/DecryptLogin/tree/master/examples/bilibiliupmonitor) | B站UP主监控 |
# Install
+38 -1
View File
@@ -2,6 +2,7 @@
## 项目声明
本系列文章源代码仅供python学习交流,请勿用于商业等违法相关法律法规的用途,如有发现,我会直接删除这个系列的文章和代码。
@@ -29,6 +30,7 @@
| 修改小米运动中的步数 | [click](https://mp.weixin.qq.com/s/TQLM9GIW50UWAsKoXb7pzQ) | [click](https://github.com/CharlesPikachu/DecryptLogin/tree/master/examples/modifymihealthsteps) |
| 淘宝抢购脚本 | [click](https://mp.weixin.qq.com/s/vCZYtynHtQAOuQJHvjhpWA) | [click](https://github.com/CharlesPikachu/DecryptLogin/tree/master/examples/taobaosnap) |
| 京东抢购脚本 | [click](https://mp.weixin.qq.com/s/-H8bwuUIPDi41d09tTlvRw) | [click](https://github.com/CharlesPikachu/DecryptLogin/tree/master/examples/jingdongsnap) |
| B站UP主监控 | [click]() | [click](https://github.com/CharlesPikachu/DecryptLogin/tree/master/examples/bilibiliupmonitor) |
## 使用简介
@@ -710,7 +712,6 @@ pip install DecryptLogin, pyttsx3, prettytable, beautifulsoup4
脚本运行方式如下:
```sh
usage: jingdongsnap.py [-h] [--interval INTERVAL] [--paywd PAYWD] [--key KEY]
京东抢购脚本
@@ -726,4 +727,40 @@ optional arguments:
```sh
python jingdongsnap.py --interval 1800 --key Server酱的Key --paywd 支付密码
```
#### B站UP主监控
1.相关依赖
在终端运行如下命令即可安装运行该项目所需要的相关依赖包:
```sh
pip install DecryptLogin, videofetch
```
2.环境配置
- 操作系统: Linux or macOS or Windows
- Python版本: Python3.6+
3.运行方式
脚本运行方式如下:
```sh
usage: bilibiliupmonitor.py [-h] [--ids IDS] [--key KEY]
B站UP主监控
optional arguments:
-h, --help show this help message and exit
--ids IDS 监控的UP主ID, 例如"406756145,406756146"
--key KEY Server酱的Key
```
例如:
```sh
python bilibiliupmonitor.py -ids 406756145 --key Server酱的Key
```
+2 -1
View File
@@ -21,4 +21,5 @@
| moocdl | [click](https://mp.weixin.qq.com/s/KsXU-pMvT8GzpPWVpcWIOA) | [click](https://github.com/CharlesPikachu/DecryptLogin/tree/master/examples/moocdl) |
| modifymihealthsteps | [click](https://mp.weixin.qq.com/s/TQLM9GIW50UWAsKoXb7pzQ) | [click](https://github.com/CharlesPikachu/DecryptLogin/tree/master/examples/modifymihealthsteps) |
| taobaosnap | [click](https://mp.weixin.qq.com/s/vCZYtynHtQAOuQJHvjhpWA) | [click](https://github.com/CharlesPikachu/DecryptLogin/tree/master/examples/taobaosnap) |
| jingdongsnap | [click](https://mp.weixin.qq.com/s/-H8bwuUIPDi41d09tTlvRw) | [click](https://github.com/CharlesPikachu/DecryptLogin/tree/master/examples/jingdongsnap) |
| jingdongsnap | [click](https://mp.weixin.qq.com/s/-H8bwuUIPDi41d09tTlvRw) | [click](https://github.com/CharlesPikachu/DecryptLogin/tree/master/examples/jingdongsnap) |
| bilibiliupmonitor | [click]() | [click](https://github.com/CharlesPikachu/DecryptLogin/tree/master/examples/bilibiliupmonitor) |
@@ -0,0 +1,146 @@
'''
Function:
B站UP主监控
Author:
Charles
微信公众号:
Charles的皮卡丘
'''
import os
import time
import pickle
import argparse
from DecryptLogin import login
'''命令行参数解析'''
def parseArgs():
parser = argparse.ArgumentParser(description='B站UP主监控')
parser.add_argument('--ids', dest='ids', help='监控的UP主ID, 例如"406756145,406756146"', type=str, required=False, default='406756145')
parser.add_argument('--key', dest='key', help='Server酱的Key', type=str, default=None)
args = parser.parse_args()
return args
'''B站UP主监控'''
class BilibiliUPMonitor():
def __init__(self, up_ids=['406756145'], time_interval=1800, server_key=None, **kwargs):
self.up_ids = up_ids
self.time_interval = time_interval
self.server_key = server_key
if os.path.exists('session.pkl'):
self.infos_return, self.session = {}, pickle.load(open('session.pkl', 'rb'))
else:
self.infos_return, self.session = self.login()
pickle.dump(self.session, open('session.pkl', 'wb'))
'''运行'''
def run(self):
# 批量关注
ups_recorder = {}
for up_id in self.up_ids:
code, message = self.follow(up_id)
ups_recorder[up_id] = self.getuserinfo(up_id)
if code == 0:
self.logging(f'关注UP主{ups_recorder[up_id]["username"]}成功, 正在进行监控...')
else:
self.logging(f'关注UP主{ups_recorder[up_id]["username"]}失败, 原因为{message}...')
# 获得UP主当前的视频信息
for up_id in self.up_ids:
ups_recorder[up_id]['vids'] = self.getupvids(up_id)
# 自动监控
while True:
# --sleep一段时间再检查
time.sleep(self.time_interval)
# --检查更新
for up_id in self.up_ids:
self.logging(f'正在检查UP主{ups_recorder[up_id]["username"]}是否更新了视频...')
vids = self.getupvids(up_id)
ups_recorder[up_id]['updated_vids'] = []
for vid in vids:
if vid in ups_recorder[up_id]['vids']: continue
ups_recorder[up_id]['updated_vids'].append(vid)
ups_recorder[up_id]['vids'].append(vid)
# --发送提示并下载
for up_id in self.up_ids:
if len(ups_recorder[up_id]['updated_vids']) > 0:
msg = f'你关注的UP主{ups_recorder[up_id]["username"]}更新啦...'
self.pushwechat(msg)
self.logging(msg)
for vid in ups_recorder[up_id]['updated_vids']:
os.system(f'videodl -i {vid} -s {ups_recorder[up_id]["username"]}')
else:
msg = f'你关注的UP主{ups_recorder[up_id]["username"]}暂时没有更新...'
self.logging(msg)
'''logging'''
def logging(self, msg, tip='INFO'):
print(f'[{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())} {tip}]: {msg}')
'''获得用户信息'''
def getuserinfo(self, up_id):
params = {'mid': up_id, 'jsonp': 'jsonp'}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'}
response = self.session.get('https://api.bilibili.com/x/space/acc/info', params=params, headers=headers)
response_json = response.json()
user_info = {
'username': response_json['data']['name'],
'gender': response_json['data']['sex'],
'sign': response_json['data']['sign'],
'level': response_json['data']['level'],
'birthday': response_json['data']['birthday']
}
return user_info
'''获得UP主首页所有视频信息'''
def getupvids(self, up_id):
up_vids, aids = [], []
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'}
params = {'keyword': '', 'mid': up_id, 'ps': 30, 'tid': 0, 'pn': 1, 'order': 'pubdate'}
response = self.session.get('https://api.bilibili.com/x/space/arc/search', headers=headers, params=params)
response_json = response.json()
for item in response_json['data']['list']['vlist']:
aids.append(item['aid'])
for aid in aids:
params = {'aid': aid}
response = self.session.get('https://api.bilibili.com/x/web-interface/view', headers=headers, params=params)
response_json = response.json()
up_vids.append('https://www.bilibili.com/video/' + response_json['data']['bvid'])
return up_vids
'''发送Server酱提示'''
def pushwechat(self, desp='你关注的UP主更新啦'):
if self.server_key is None: return
server_url = f'https://sc.ftqq.com/{self.server_key}.send'
params = {
'text': 'UP主更新提示',
'desp': desp,
}
response = requests.get(server_url, params=params)
return
'''关注某个UP主'''
def follow(self, up_id):
url = 'https://api.bilibili.com/x/relation/modify'
data = {
'fid': up_id,
'act': 1,
're_src': 11,
'jsonp': 'jsonp',
'csrf': self.session.cookies.get_dict(domain='.bilibili.com').get('bili_jct', ''),
}
headers = {
'Host': 'api.bilibili.com',
'Origin': 'https://space.bilibili.com',
'Referer': f'https://space.bilibili.com/{up_id}/',
}
response = self.session.post(url, data=data, headers=headers)
response_json = response.json()
return response_json['code'], response_json.get('message', '')
'''模拟登录'''
def login(self):
lg = login.Login()
infos_return, session = lg.bilibili(mode='scanqr')
return infos_return, session
'''run'''
if __name__ == '__main__':
args = parseArgs()
up_ids = [id_.strip() for id_ in args.ids.split(',')]
client = BilibiliUPMonitor(up_ids=up_ids, server_key=args.key)
client.run()