Merge branch 'lubo'
# Conflicts: # Common.py # Demo/Xigua.proto # Demo/XiguaGift.proto # Demo/XiguaUser.proto # README.md # WebMain.py # WinMain.py # api.py # bilibili.py # liveDownloader.py # templates/head.html
This commit is contained in:
commit
0723817bc8
5
.gitignore
vendored
5
.gitignore
vendored
@ -178,4 +178,7 @@ fabric.properties
|
||||
pyvenv.cfg
|
||||
.venv
|
||||
pip-selfcheck.json
|
||||
|
||||
*.mp4
|
||||
*.flv
|
||||
config*
|
||||
.*
|
||||
|
431
Common.py
Normal file
431
Common.py
Normal file
@ -0,0 +1,431 @@
|
||||
import os
|
||||
import queue
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import psutil
|
||||
from api import XiGuaLiveApi
|
||||
import json
|
||||
import threading
|
||||
from bilibili import *
|
||||
|
||||
# 默认设置
|
||||
config = {
|
||||
# 录像的主播名
|
||||
"l_u": "永恒de草薙",
|
||||
"b_u": "自己的B站账号",
|
||||
"b_p": "自己的B站密码",
|
||||
# 标题及预留时间位置
|
||||
"t_t": "【永恒de草薙直播录播】直播于 {}",
|
||||
# 标签
|
||||
"tag": ["永恒de草薙", "三国", "三国战记", "直播录像", "录播", "怀旧", "街机"],
|
||||
# 描述
|
||||
"des": "西瓜直播 https://live.ixigua.com/userlive/97621754276 \n自动投递\n原主播:永恒de草薙\n直播时间:晚上6点多到凌晨4点左右",
|
||||
# 来源, 空则为自制
|
||||
"src": "",
|
||||
# Log条数
|
||||
"l_c": 5,
|
||||
# 错误Log条数
|
||||
"elc": 10,
|
||||
# 每一chunk大小
|
||||
"c_s": 16 * 1024,
|
||||
# 每一块视频大小
|
||||
"p_s": 2141000000,
|
||||
# 忽略的大小
|
||||
"i_s": 2048000,
|
||||
"max": 75,
|
||||
"exp": 1,
|
||||
"dow": "echo 'clean'",
|
||||
# 仅下载
|
||||
"dlO": True,
|
||||
# 下播延迟投稿
|
||||
"dly": 30,
|
||||
# 短的时间的格式
|
||||
"sdf": "%Y%m%d",
|
||||
"enc": "ffmpeg -i {f} -c:v copy -c:a copy -f mp4 {t} -y"
|
||||
}
|
||||
doCleanTime = datetime.fromtimestamp(0)
|
||||
loginTime = datetime.fromtimestamp(0)
|
||||
_clean_flag = None
|
||||
delay = datetime.fromtimestamp(0)
|
||||
b = Bilibili()
|
||||
|
||||
network = [{
|
||||
"currentTime": datetime.now(),
|
||||
"out": {
|
||||
"currentByte": psutil.net_io_counters().bytes_sent,
|
||||
},
|
||||
"in": {
|
||||
"currentByte": psutil.net_io_counters().bytes_recv,
|
||||
}
|
||||
}, {
|
||||
"currentTime": datetime.now(),
|
||||
"out": {
|
||||
"currentByte": psutil.net_io_counters().bytes_sent,
|
||||
},
|
||||
"in": {
|
||||
"currentByte": psutil.net_io_counters().bytes_recv,
|
||||
}
|
||||
}]
|
||||
|
||||
|
||||
def reloadConfig():
|
||||
global config
|
||||
if os.path.exists('config.json'):
|
||||
_config_fp = open("config.json", "r", encoding="utf8")
|
||||
_config = json.load(_config_fp)
|
||||
config.update(_config)
|
||||
_config_fp.close()
|
||||
|
||||
|
||||
def resetDelay():
|
||||
global delay
|
||||
delay = datetime.now() + timedelta(minutes=int(config['dly']))
|
||||
|
||||
|
||||
def doDelay():
|
||||
global delay
|
||||
if -60 < getTimeDelta(datetime.now(), delay) < 60:
|
||||
delay = datetime.fromtimestamp(0)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def updateNetwork():
|
||||
global network
|
||||
network.append({
|
||||
"currentTime": datetime.now(),
|
||||
"out": {
|
||||
"currentByte": psutil.net_io_counters().bytes_sent,
|
||||
},
|
||||
"in": {
|
||||
"currentByte": psutil.net_io_counters().bytes_recv,
|
||||
}
|
||||
})
|
||||
network = network[-3:]
|
||||
|
||||
|
||||
def getTimeDelta(a, b):
|
||||
return (a - b).total_seconds()
|
||||
|
||||
|
||||
def _doClean(_force=False):
|
||||
global doCleanTime, _clean_flag
|
||||
_disk = psutil.disk_usage(".")
|
||||
if _disk.percent > config["max"] or getTimeDelta(datetime.now(), doCleanTime) > config["exp"] * 86400 or _force:
|
||||
_clean_flag = True
|
||||
doCleanTime = datetime.now()
|
||||
appendOperation("执行配置的清理命令")
|
||||
os.system(config["dow"])
|
||||
appendOperation("执行配置的清理命令完毕")
|
||||
doCleanTime = datetime.now()
|
||||
_clean_flag = False
|
||||
|
||||
|
||||
def doClean(_force=False):
|
||||
if _clean_flag:
|
||||
return
|
||||
p = threading.Thread(target=_doClean, args=(_force,))
|
||||
p.setDaemon(True)
|
||||
p.start()
|
||||
|
||||
|
||||
def getCurrentStatus():
|
||||
_disk = psutil.disk_usage(".")
|
||||
_mem = psutil.virtual_memory()
|
||||
_net = psutil.net_io_counters()
|
||||
_delta = getTimeDelta(network[-1]["currentTime"], network[-2]["currentTime"])
|
||||
if 60 > _delta > 1:
|
||||
_inSpeed = (network[-1]["in"]["currentByte"] - network[-2]["in"]["currentByte"]) / _delta
|
||||
_outSpeed = (network[-1]["out"]["currentByte"] - network[-2]["out"]["currentByte"]) / _delta
|
||||
else:
|
||||
_outSpeed = (network[-1]["in"]["currentByte"] - network[-2]["in"]["currentByte"])
|
||||
_inSpeed = (network[-1]["out"]["currentByte"] - network[-2]["out"]["currentByte"])
|
||||
updateNetwork()
|
||||
return {
|
||||
"memTotal": parseSize(_mem.total),
|
||||
"memUsed": parseSize(_mem.used),
|
||||
"memUsage": _mem.percent,
|
||||
"diskTotal": parseSize(_disk.total),
|
||||
"diskUsed": parseSize(_disk.used),
|
||||
"diskUsage": _disk.percent,
|
||||
"cpu": psutil.cpu_percent(),
|
||||
"outSpeed": parseSize(_outSpeed),
|
||||
"inSpeed": parseSize(_inSpeed),
|
||||
"doCleanTime": datetime.strftime(doCleanTime, dt_format),
|
||||
"fileExpire": config["exp"],
|
||||
}
|
||||
|
||||
|
||||
dt_format = "%Y/%m/%d %H:%M:%S"
|
||||
reloadConfig()
|
||||
broadcaster = ""
|
||||
streamUrl = ""
|
||||
|
||||
forceNotDownload = False
|
||||
forceNotBroadcasting = False
|
||||
forceNotUpload = False
|
||||
forceNotEncode = False
|
||||
if config["dlO"] is True:
|
||||
forceNotUpload = True
|
||||
forceNotEncode = True
|
||||
forceStartEncodeThread = False
|
||||
forceStartUploadThread = False
|
||||
|
||||
uploadQueue = queue.Queue()
|
||||
encodeQueue = queue.Queue()
|
||||
|
||||
uploadStatus = []
|
||||
downloadStatus = []
|
||||
encodeStatus = []
|
||||
errors = []
|
||||
operations = []
|
||||
|
||||
|
||||
def appendOperation(obj):
|
||||
global operations
|
||||
if isinstance(obj, dict):
|
||||
if "datetime" not in obj:
|
||||
obj["datetime"] = datetime.strftime(datetime.now(), dt_format)
|
||||
operations.append(obj)
|
||||
else:
|
||||
operations.append({
|
||||
"datetime": datetime.strftime(datetime.now(), dt_format),
|
||||
"message": str(obj)
|
||||
})
|
||||
operations = operations[-config["elc"]:]
|
||||
|
||||
|
||||
def parseSize(size):
|
||||
K = size / 1024.0
|
||||
if K > 1000:
|
||||
M = K / 1024.0
|
||||
if M > 1000:
|
||||
return "{:.2f}GB".format(M / 1024.0)
|
||||
else:
|
||||
return "{:.2f}MB".format(M)
|
||||
else:
|
||||
return "{:.2f}KB".format(K)
|
||||
|
||||
|
||||
def appendUploadStatus(obj):
|
||||
global uploadStatus
|
||||
if isinstance(obj, dict):
|
||||
if "datetime" not in obj:
|
||||
obj["datetime"] = datetime.strftime(datetime.now(), dt_format)
|
||||
uploadStatus.append(obj)
|
||||
else:
|
||||
uploadStatus.append({
|
||||
"datetime": datetime.strftime(datetime.now(), dt_format),
|
||||
"message": str(obj)
|
||||
})
|
||||
uploadStatus = uploadStatus[-config["l_c"]:]
|
||||
|
||||
|
||||
def modifyLastUploadStatus(obj):
|
||||
global uploadStatus
|
||||
if isinstance(obj, dict):
|
||||
if "datetime" not in obj:
|
||||
obj["datetime"] = datetime.strftime(datetime.now(), dt_format)
|
||||
uploadStatus[-1] = obj
|
||||
else:
|
||||
uploadStatus[-1]["message"] = str(obj)
|
||||
uploadStatus[-1]["datetime"] = datetime.strftime(datetime.now(), dt_format)
|
||||
|
||||
|
||||
def appendEncodeStatus(obj):
|
||||
global encodeStatus
|
||||
if isinstance(obj, dict):
|
||||
if "datetime" not in obj:
|
||||
obj["datetime"] = datetime.strftime(datetime.now(), dt_format)
|
||||
encodeStatus.append(obj)
|
||||
else:
|
||||
encodeStatus.append({
|
||||
"datetime": datetime.strftime(datetime.now(), dt_format),
|
||||
"message": str(obj)
|
||||
})
|
||||
encodeStatus = encodeStatus[-config["l_c"]:]
|
||||
|
||||
|
||||
def modifyLastEncodeStatus(obj):
|
||||
global encodeStatus
|
||||
if isinstance(obj, dict):
|
||||
if "datetime" not in obj:
|
||||
obj["datetime"] = datetime.strftime(datetime.now(), dt_format)
|
||||
encodeStatus[-1] = obj
|
||||
else:
|
||||
encodeStatus[-1]["message"] = str(obj)
|
||||
encodeStatus[-1]["datetime"] = datetime.strftime(datetime.now(), dt_format)
|
||||
|
||||
|
||||
def appendDownloadStatus(obj):
|
||||
global downloadStatus
|
||||
if isinstance(obj, dict):
|
||||
if "datetime" not in obj:
|
||||
obj["datetime"] = datetime.strftime(datetime.now(), dt_format)
|
||||
downloadStatus.append(obj)
|
||||
else:
|
||||
downloadStatus.append({
|
||||
"datetime": datetime.strftime(datetime.now(), dt_format),
|
||||
"message": str(obj)
|
||||
})
|
||||
downloadStatus = downloadStatus[-config["l_c"]:]
|
||||
|
||||
|
||||
def modifyLastDownloadStatus(obj):
|
||||
global downloadStatus
|
||||
if isinstance(obj, dict):
|
||||
if "datetime" not in obj:
|
||||
obj["datetime"] = datetime.strftime(datetime.now(), dt_format)
|
||||
downloadStatus[-1] = obj
|
||||
else:
|
||||
downloadStatus[-1]["message"] = str(obj)
|
||||
downloadStatus[-1]["datetime"] = datetime.strftime(datetime.now(), dt_format)
|
||||
|
||||
|
||||
def appendError(obj):
|
||||
global errors
|
||||
if isinstance(obj, dict):
|
||||
if "datetime" not in obj:
|
||||
obj["datetime"] = datetime.strftime(datetime.now(), dt_format)
|
||||
errors.append(obj)
|
||||
else:
|
||||
errors.append({
|
||||
"datetime": datetime.strftime(datetime.now(), dt_format),
|
||||
"message": str(obj)
|
||||
})
|
||||
errors = errors[-config["elc"]:]
|
||||
|
||||
|
||||
def loginBilibili(force=False):
|
||||
if config["dlO"] is False or forceNotUpload is False:
|
||||
global loginTime
|
||||
global b
|
||||
if getTimeDelta(datetime.now(), loginTime) < 86400 * 10 and not force:
|
||||
return False
|
||||
if os.path.exists('cookie'):
|
||||
try:
|
||||
with open('cookie', 'r', encoding='utf8') as f:
|
||||
_cookie = f.readline().strip()
|
||||
b = Bilibili(_cookie)
|
||||
loginTime = datetime.now()
|
||||
appendOperation("Cookie 登录")
|
||||
return True
|
||||
except Exception as e:
|
||||
appendError(e)
|
||||
appendOperation("Cookie 登录失败")
|
||||
return False
|
||||
else:
|
||||
appendOperation("设置了不上传,所以不会登陆")
|
||||
|
||||
|
||||
class downloader(XiGuaLiveApi):
|
||||
playlist = None
|
||||
|
||||
def _checkUsernameIsMatched(self, compare=None):
|
||||
return True
|
||||
|
||||
def updRoomInfo(self, force=False):
|
||||
global broadcaster
|
||||
_prev_status = self.isLive
|
||||
doClean()
|
||||
_result = super(downloader, self).updRoomInfo(force)
|
||||
if _prev_status != self.isLive and not self.isLive:
|
||||
resetDelay()
|
||||
broadcaster = self.broadcaster
|
||||
if _result:
|
||||
if self.isLive:
|
||||
self.updPlayList()
|
||||
else:
|
||||
self.playlist = False
|
||||
return _result
|
||||
|
||||
def updPlayList(self):
|
||||
global streamUrl
|
||||
if self.isLive and "stream_url" in self._rawRoomInfo:
|
||||
self.playlist = self._rawRoomInfo["stream_url"]["flv_pull_url"]
|
||||
if type(self.playlist) is dict:
|
||||
for _ in self.playlist.values():
|
||||
self.playlist = _
|
||||
break
|
||||
self.playlist = self.playlist.replace("_uhd", "").replace("_sd", "").replace("_ld", "")
|
||||
streamUrl = self.playlist
|
||||
else:
|
||||
streamUrl = None
|
||||
self.playlist = None
|
||||
|
||||
|
||||
api = downloader(config["l_u"])
|
||||
|
||||
|
||||
def refreshDownloader():
|
||||
global api
|
||||
api = downloader(config["l_u"])
|
||||
|
||||
|
||||
def uploadVideo(name):
|
||||
if not os.path.exists(name):
|
||||
appendError("Upload File Not Exist {}".format(name))
|
||||
return
|
||||
loginBilibili()
|
||||
doClean()
|
||||
if forceNotUpload is False:
|
||||
b.preUpload(VideoPart(name, os.path.basename(name)))
|
||||
else:
|
||||
appendUploadStatus("设置了不上传,所以[{}]不会上传了".format(name))
|
||||
if not forceNotEncode:
|
||||
os.remove(name)
|
||||
|
||||
|
||||
def publishVideo(date):
|
||||
if forceNotUpload is False:
|
||||
b.finishUpload(config["t_t"].format(date), 17, config["tag"], config["des"],
|
||||
source=config["src"], no_reprint=0)
|
||||
b.clear()
|
||||
else:
|
||||
appendUploadStatus("设置了不上传,所以[{}]的录播不会投了".format(date))
|
||||
|
||||
|
||||
def encodeVideo(name):
|
||||
if forceNotEncode:
|
||||
appendEncodeStatus("设置了不编码,所以[{}]不会编码".format(name))
|
||||
return False
|
||||
if not os.path.exists(name):
|
||||
appendEncodeStatus("文件[{}]不存在".format(name))
|
||||
return False
|
||||
if os.path.getsize(name) < 8 * 1024 * 1024:
|
||||
appendEncodeStatus("Encoded File >{}< is too small, will ignore it".format(name))
|
||||
return False
|
||||
appendEncodeStatus("Encoding >{}< Start".format(name))
|
||||
_new_name = os.path.splitext(name)[0] + ".mp4"
|
||||
_code = os.system(config["enc"].format(f=name, t=_new_name))
|
||||
if _code != 0:
|
||||
Common.appendError("Encode {} with Non-Zero Return.".format(name))
|
||||
return False
|
||||
Common.modifyLastEncodeStatus("Encode >{}< Finished".format(name))
|
||||
uploadQueue.put(_new_name)
|
||||
|
||||
|
||||
def collectInfomation():
|
||||
return {
|
||||
"download": downloadStatus,
|
||||
"encode": encodeStatus,
|
||||
"encodeQueueSize": encodeQueue.qsize(),
|
||||
"upload": uploadStatus,
|
||||
"uploadQueueSize": uploadQueue.qsize(),
|
||||
"error": errors,
|
||||
"operation": operations,
|
||||
"broadcast": {
|
||||
"broadcaster": broadcaster.__str__(),
|
||||
"isBroadcasting": api.isLive,
|
||||
"streamUrl": streamUrl,
|
||||
"updateTime": api.updateAt.strftime(dt_format),
|
||||
"delayTime": delay.strftime(dt_format)
|
||||
},
|
||||
"config": {
|
||||
"forceNotBroadcasting": forceNotBroadcasting,
|
||||
"forceNotDownload": forceNotDownload,
|
||||
"forceNotUpload": forceNotUpload,
|
||||
"forceNotEncode": forceNotEncode,
|
||||
"downloadOnly": config['dlO'],
|
||||
},
|
||||
}
|
File diff suppressed because one or more lines are too long
BIN
Demo/324_.txt
BIN
Demo/324_.txt
Binary file not shown.
1104
Demo/result.txt
1104
Demo/result.txt
File diff suppressed because it is too large
Load Diff
4406
Demo/result2.txt
4406
Demo/result2.txt
File diff suppressed because it is too large
Load Diff
1434
Demo/result3.txt
1434
Demo/result3.txt
File diff suppressed because it is too large
Load Diff
2576
Demo/result4.json
2576
Demo/result4.json
File diff suppressed because it is too large
Load Diff
6007
Demo/result4.txt
6007
Demo/result4.txt
File diff suppressed because it is too large
Load Diff
11
README.md
11
README.md
@ -13,10 +13,19 @@
|
||||
|
||||
### 西瓜直播弹幕接口```api.py```
|
||||
|
||||
> - 基于安卓9.4.2(94214)
|
||||
> - 基于安卓9.6.6(96615)
|
||||
|
||||
### 西瓜直播弹幕助手--礼物端```WinMain.py```
|
||||
|
||||
### 西瓜直播弹幕助手--录播端```WebMain.py```
|
||||
|
||||
> - 能够自动进行ffmpeg转码
|
||||
> - 转码后自动上传至B站
|
||||
> - 顺便还能自己清理录播的文件(移动到一个位置,执行shell命令,上传百度云)
|
||||
> - 把录像文件分一定大小保存(B站有限制,但是不知道是多少)
|
||||
> - 少部分错误包容机制
|
||||
> - 有一个简单的WEB页面,及简单的控制接口
|
||||
|
||||
### ~~计划更新~~
|
||||
|
||||
### 随缘更新
|
||||
|
193
WebMain.py
Normal file
193
WebMain.py
Normal file
@ -0,0 +1,193 @@
|
||||
import os
|
||||
from glob import glob
|
||||
from time import sleep
|
||||
from flask_cors import CORS
|
||||
from flask import Flask, jsonify, request, render_template, Response, send_file
|
||||
import Common
|
||||
import threading
|
||||
from liveDownloader import run as RUN
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['JSON_AS_ASCII'] = False
|
||||
CORS(app, supports_credentials=True)
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
return render_template("index.html")
|
||||
|
||||
|
||||
@app.route("/config", methods=["GET"])
|
||||
def readConfig():
|
||||
config = Common.config.copy()
|
||||
config.pop("b_p")
|
||||
config.pop("mv")
|
||||
return jsonify(config)
|
||||
|
||||
|
||||
@app.route("/config", methods=["POST"])
|
||||
def writeConfig():
|
||||
# TODO : 完善
|
||||
Common.appendOperation("更新配置")
|
||||
Common.reloadConfig()
|
||||
return jsonify({"message": "ok", "code": 200, "status": 0, "data": request.form})
|
||||
|
||||
|
||||
@app.route("/force/not/upload", methods=["POST"])
|
||||
def toggleForceNotUpload():
|
||||
Common.forceNotUpload = not Common.forceNotUpload
|
||||
Common.appendOperation("将强制不上传的值改为:{}".format(Common.forceNotUpload))
|
||||
return jsonify({"message": "ok", "code": 200, "status": 0, "data": {
|
||||
"forceNotUpload": Common.forceNotUpload,
|
||||
}})
|
||||
|
||||
|
||||
@app.route("/force/not/encode", methods=["POST"])
|
||||
def toggleForceNotEncode():
|
||||
Common.forceNotEncode = not Common.forceNotEncode
|
||||
Common.appendOperation("将强制不编码的值改为:{}".format(Common.forceNotEncode))
|
||||
return jsonify({"message": "ok", "code": 200, "status": 0, "data": {
|
||||
"forceNotEncode": Common.forceNotEncode,
|
||||
}})
|
||||
|
||||
|
||||
@app.route("/force/not/download", methods=["POST"])
|
||||
def toggleForceNotDownload():
|
||||
Common.forceNotDownload = not Common.forceNotDownload
|
||||
Common.appendOperation("将强制不下载的值改为:{}".format(Common.forceNotDownload))
|
||||
return jsonify({"message": "ok", "code": 200, "status": 0, "data": {
|
||||
"forceNotDownload": Common.forceNotDownload,
|
||||
}})
|
||||
|
||||
|
||||
@app.route("/force/not/broadcast", methods=["POST"])
|
||||
def toggleForceNotBroadcast():
|
||||
Common.forceNotBroadcasting = not Common.forceNotBroadcasting
|
||||
return jsonify({"message": "ok", "code": 200, "status": 0, "data": {
|
||||
"forceNotBroadcasting": Common.forceNotBroadcasting,
|
||||
}})
|
||||
|
||||
|
||||
@app.route("/force/start/encode", methods=["POST"])
|
||||
def toggleForceStartEncodeThread():
|
||||
Common.forceStartEncodeThread = True
|
||||
Common.appendOperation("强制运行编码线程")
|
||||
return jsonify({"message": "ok", "code": 200, "status": 0, "data": {
|
||||
}})
|
||||
|
||||
|
||||
@app.route("/force/start/upload", methods=["POST"])
|
||||
def toggleForceStartUploadThread():
|
||||
Common.forceStartUploadThread = True
|
||||
Common.appendOperation("强制运行上传线程")
|
||||
return jsonify({"message": "ok", "code": 200, "status": 0, "data": {
|
||||
}})
|
||||
|
||||
|
||||
@app.route("/force/start/clean", methods=["POST"])
|
||||
def startForceCleanDisk():
|
||||
Common.doClean(True)
|
||||
Common.appendOperation("强制执行清理程序")
|
||||
return jsonify({"message": "ok", "code": 200, "status": 0, "data": {
|
||||
}})
|
||||
|
||||
|
||||
@app.route("/encode/insert", methods=["POST"])
|
||||
def insertEncode():
|
||||
if "filename" in request.form and os.path.exists(request.form["filename"]):
|
||||
Common.appendOperation("添加编码文件:{}".format(request.form["filename"]))
|
||||
Common.encodeQueue.put(request.form["filename"])
|
||||
return jsonify({"message": "ok", "code": 200, "status": 0})
|
||||
else:
|
||||
return jsonify({"message": "no filename specific", "code": 400, "status": 1})
|
||||
|
||||
|
||||
@app.route("/upload/insert", methods=["POST"])
|
||||
def insertUpload():
|
||||
if "filename" in request.form and os.path.exists(request.form["filename"]):
|
||||
Common.appendOperation("添加上传文件:{}".format(request.form["filename"]))
|
||||
Common.uploadQueue.put(request.form["filename"])
|
||||
return jsonify({"message": "ok", "code": 200, "status": 0})
|
||||
else:
|
||||
return jsonify({"message": "no filename specific", "code": 400, "status": 1})
|
||||
|
||||
|
||||
@app.route("/upload/finish", methods=["POST"])
|
||||
def finishUpload():
|
||||
Common.appendOperation("设置当前已完成上传")
|
||||
Common.uploadQueue.put(True)
|
||||
return jsonify({"message": "ok", "code": 200, "status": 0})
|
||||
|
||||
|
||||
@app.route("/stats", methods=["GET"])
|
||||
def getAllStats():
|
||||
return jsonify({"message": "ok", "code": 200, "status": 0, "data": Common.collectInfomation()})
|
||||
|
||||
|
||||
@app.route("/stats/device", methods=["GET"])
|
||||
def getDeviceStatus():
|
||||
return jsonify({"message": "ok", "code": 200, "status": 0, "data": {
|
||||
"status": Common.getCurrentStatus(),
|
||||
}})
|
||||
|
||||
|
||||
@app.route("/stats/config", methods=["GET"])
|
||||
def getConfigStats():
|
||||
return jsonify({"message": "ok", "code": 200, "status": 0, "data": {
|
||||
"config": {
|
||||
"forceNotBroadcasting": Common.forceNotBroadcasting,
|
||||
"forceNotDownload": Common.forceNotDownload,
|
||||
"forceNotUpload": Common.forceNotUpload,
|
||||
"forceNotEncode": Common.forceNotEncode,
|
||||
"downloadOnly": Common.config['dlO'],
|
||||
}
|
||||
}})
|
||||
|
||||
|
||||
@app.route("/account/reLogin", methods=["POST"])
|
||||
def accountRelogin():
|
||||
res = Common.loginBilibili(True)
|
||||
return jsonify({"message": "ok", "code": 200, "status": 0, "data": {"result": res}})
|
||||
|
||||
|
||||
@app.route("/files/", methods=["GET"])
|
||||
def fileIndex():
|
||||
a = []
|
||||
for i in (glob("*.mp4") + glob("*.flv")):
|
||||
a.append({
|
||||
"name": i,
|
||||
"size": Common.parseSize(os.path.getsize(i))
|
||||
})
|
||||
return render_template("files.html", files=a)
|
||||
|
||||
|
||||
@app.route("/files/download/<path>", methods=["GET"])
|
||||
def fileDownload(path):
|
||||
if not (".mp4" in path or ".flv" in path):
|
||||
return Response(status=404)
|
||||
if os.path.exists(path):
|
||||
return send_file(path, as_attachment=True)
|
||||
else:
|
||||
return Response(status=404)
|
||||
|
||||
|
||||
def SubThread():
|
||||
t = threading.Thread(target=RUN, args=())
|
||||
t.setDaemon(True)
|
||||
t.start()
|
||||
while True:
|
||||
if t.is_alive():
|
||||
sleep(240)
|
||||
else:
|
||||
t = threading.Thread(target=RUN, args=())
|
||||
t.setDaemon(True)
|
||||
t.start()
|
||||
|
||||
|
||||
if not app.debug:
|
||||
p = threading.Thread(target=SubThread)
|
||||
p.setDaemon(True)
|
||||
p.start()
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run()
|
53
api.py
53
api.py
@ -15,27 +15,27 @@ DEBUG = False
|
||||
# 自己抓的自己设备的参数,建议开发者自己抓一个长期使用
|
||||
# 如果有大佬破解初次激活设备时的数据也行,可以自己生成一堆用
|
||||
CUSTOM_INFO = {
|
||||
'iid': "96159232732",
|
||||
'device_id': "55714661189",
|
||||
'cdid': "ed4295e8-5d9a-4cb9-b2a2-04009a3baa2d",
|
||||
'openudid': "70d6668d41512c39",
|
||||
'iid': "3993882704224472",
|
||||
'device_id': "71008241150",
|
||||
'cdid': "c927a88b-ca4c-427a-9c8b-43da247b9860",
|
||||
'openudid': "630fd57b61c64c4c",
|
||||
# 'aid': "32", # 是一个不变的值
|
||||
'channel': "xiaomi",
|
||||
'device_brand': "Xiaomi",
|
||||
'device_type': "MI+8+SE",
|
||||
'os_api': "28",
|
||||
'os_version': "9",
|
||||
'rom_version': "miui_V12_V12.0.2.0.QEBCNXM",
|
||||
'device_type': "MI 9",
|
||||
'os_api': "29",
|
||||
'os_version': "10",
|
||||
'rom_version': "miui_V12_V12.0.6.0.QFACNXM",
|
||||
}
|
||||
VERSION_INFO = {
|
||||
'app_name': "video_article",
|
||||
'version_code': "942",
|
||||
'version_code_full': "94214",
|
||||
'version_name': "9.4.2",
|
||||
'ab_version': "668852,668853,668858,668851,668859,668856,668855,2358970,"
|
||||
"668854,2393607,1477978,994679,2408463,2412359",
|
||||
'manifest_version_code': "542",
|
||||
'tma_jssdk_version': "1830001",
|
||||
'version_code': "966",
|
||||
'version_code_full': "96615",
|
||||
'version_name': "9.6.6",
|
||||
'ab_version': "668851,2678488,668858,2678385,668859,2678471,668856,2678470,668855,2678439,668854,994679,"
|
||||
"2678460,2713007,2738381,668853,2678466,668852,2678435,2625016",
|
||||
'manifest_version_code': "566",
|
||||
'tma_jssdk_version': "2010000",
|
||||
'oaid': "693ea85657ef38ca",
|
||||
}
|
||||
COMMON_GET_PARAM = (
|
||||
@ -53,7 +53,7 @@ SEARCH_USER_API = (
|
||||
'&_s_page_sub_route=/&_s_ec={{"filterDataType":[],"reserveFilterBar":true}}&__use_xigua_native_bridge_fetch__=1'
|
||||
'&ab_param={{"is_show_filter_feature": 1, "is_hit_new_ui": 1}}'
|
||||
"&search_start_time={TIMESTAMP:.0f}&from=live&en_qc=1&pd=xigua_live&ssmix=a{COMMON}&keyword={keyword}")
|
||||
USER_INFO_API = "https://api100-quic-c-hl.ixigua.com/video/app/user/home/v7/?to_user_id={userId}{COMMON}"
|
||||
USER_INFO_API = "https://ib-hl.snssdk.com/video/app/user/userhome/v8/?to_user_id={userId}{COMMON}"
|
||||
ROOM_INFO_API = "https://webcast3-normal-c-hl.ixigua.com/webcast/room/enter/?room_id={roomId}&pack_level=4{COMMON}"
|
||||
DANMAKU_GET_API = "https://webcast3-normal-c-hl.ixigua.com/webcast/im/fetch/?{WEBCAST}{COMMON}"
|
||||
GIFT_DATA_API = ("https://webcast3-normal-c-hl.ixigua.com/webcast/gift/list/?room_id={roomId}&to_room_id={roomId}&"
|
||||
@ -64,8 +64,8 @@ COMMON_HEADERS = {
|
||||
"passport-sdk-version": "21",
|
||||
"X-SS-DP": "32",
|
||||
"x-vc-bdturing-sdk-version": "2.0.1",
|
||||
"User-Agent": "Dalvik/2.1.0 (Linux; U; Android 10) VideoArticle/9.2.6 cronet/TTNetVersion:828f6f3c 2020-09-06 "
|
||||
"QuicVersion:7aee791b 2020-06-05",
|
||||
"User-Agent": "Dalvik/2.1.0 (Linux; U; Android 10; MI 9 MIUI/V12.0.6.0.QFACNXM) VideoArticle/9.6.6 "
|
||||
"cronet/TTNetVersion:4b936afe 2021-01-13 QuicVersion:47946d2a 2020-10-14",
|
||||
"Accept-Encoding": "gzip, deflate"
|
||||
}
|
||||
|
||||
@ -321,22 +321,21 @@ class XiGuaLiveApi:
|
||||
print("获取用户信息失败")
|
||||
return False
|
||||
self.isValidUser = d["status"] == 0
|
||||
if "user_info" not in d and d["user_info"] is None:
|
||||
_d = d.get('data', {})
|
||||
if "user_home_info" not in _d and _d['user_home_info']['user_info'] is None:
|
||||
self.apiChangedError("Api发生改变,请及时联系我", d)
|
||||
return False
|
||||
self._updRoomAt = datetime.now()
|
||||
self.broadcaster = User(d)
|
||||
self.broadcaster = User(_d['user_home_info'])
|
||||
if not self._checkUsernameIsMatched():
|
||||
self.isLive = False
|
||||
return False
|
||||
self.isLive = d["user_info"]["is_living"]
|
||||
if d["user_info"]['live_info'] is None:
|
||||
if d["live_data"] is None:
|
||||
self.isLive = False
|
||||
else:
|
||||
self._rawRoomInfo = d["live_data"]['live_info']
|
||||
self.isLive = 'user_live_info_list' in _d
|
||||
if self.isLive and len(_d['user_live_info_list']) != 0:
|
||||
# 既然有长度,默认个0应该没事
|
||||
self._rawRoomInfo = _d['user_live_info_list'][0]['live_info']
|
||||
else:
|
||||
self._rawRoomInfo = d["user_info"]['live_info']
|
||||
self.isLive = False
|
||||
if self.isLive:
|
||||
self.roomID = self._rawRoomInfo['room_id']
|
||||
return self._getRoomInfo(True)
|
||||
|
540
bilibili.py
Normal file
540
bilibili.py
Normal file
@ -0,0 +1,540 @@
|
||||
# coding=utf-8
|
||||
|
||||
import os
|
||||
import re
|
||||
import json as JSON
|
||||
import Common
|
||||
import rsa
|
||||
import math
|
||||
import base64
|
||||
import hashlib
|
||||
import requests
|
||||
from urllib import parse
|
||||
from requests.adapters import HTTPAdapter
|
||||
from urllib3 import Retry
|
||||
|
||||
|
||||
class VideoPart:
|
||||
def __init__(self, path, title='', desc=''):
|
||||
self.path = path
|
||||
self.title = title
|
||||
self.desc = desc
|
||||
|
||||
|
||||
class Bilibili:
|
||||
def __init__(self, cookie=None):
|
||||
self.files = []
|
||||
self.videos = []
|
||||
self.session = requests.session()
|
||||
self.session.keep_alive = False
|
||||
if cookie:
|
||||
self.session.headers["cookie"] = cookie
|
||||
self.csrf = re.search('bili_jct=(.*?);', cookie).group(1)
|
||||
self.mid = re.search('DedeUserID=(.*?);', cookie).group(1)
|
||||
self.session.headers['Accept'] = 'application/json, text/javascript, */*; q=0.01'
|
||||
self.session.headers['Referer'] = 'https://space.bilibili.com/{mid}/#!/'.format(mid=self.mid)
|
||||
# session.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
|
||||
# session.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
|
||||
|
||||
def login(self, user, pwd):
|
||||
"""
|
||||
|
||||
:param user: username
|
||||
:type user: str
|
||||
:param pwd: password
|
||||
:type pwd: str
|
||||
:return: if success return True
|
||||
else raise Exception
|
||||
"""
|
||||
APPKEY = '4409e2ce8ffd12b8'
|
||||
ACTIONKEY = 'appkey'
|
||||
BUILD = 101800
|
||||
DEVICE = 'android_tv_yst'
|
||||
MOBI_APP = 'android_tv_yst'
|
||||
PLATFORM = 'android'
|
||||
APPSECRET = '59b43e04ad6965f34319062b478f83dd'
|
||||
|
||||
def md5(s):
|
||||
h = hashlib.md5()
|
||||
h.update(s.encode('utf-8'))
|
||||
return h.hexdigest()
|
||||
|
||||
def sign(s):
|
||||
"""
|
||||
|
||||
:return: return sign
|
||||
"""
|
||||
return md5(s + APPSECRET)
|
||||
|
||||
def signed_body(body):
|
||||
"""
|
||||
|
||||
:return: body which be added sign
|
||||
"""
|
||||
if isinstance(body, str):
|
||||
return body + '&sign=' + sign(body)
|
||||
elif isinstance(body, dict):
|
||||
ls = []
|
||||
for k, v in body.items():
|
||||
ls.append(k + '=' + v)
|
||||
body['sign'] = sign('&'.join(ls))
|
||||
return body
|
||||
|
||||
def getkey():
|
||||
"""
|
||||
|
||||
:return: hash, key
|
||||
"""
|
||||
r = self.session.post(
|
||||
'https://passport.bilibili.com/api/oauth2/getKey',
|
||||
signed_body({'appkey': APPKEY}),
|
||||
)
|
||||
# {"ts":1544152439,"code":0,"data":{"hash":"99c7573759582e0b","key":"-----BEGIN PUBLIC----- -----END PUBLIC KEY-----\n"}}
|
||||
json = r.json()
|
||||
data = json['data']
|
||||
return data['hash'], data['key']
|
||||
|
||||
def access_token_2_cookie(access_token):
|
||||
r = self.session.get(
|
||||
'https://passport.bilibili.com/api/login/sso?' + \
|
||||
signed_body(
|
||||
'access_key={access_token}&appkey={appkey}&gourl=https%3A%2F%2Faccount.bilibili.com%2Faccount%2Fhome'
|
||||
.format(access_token=access_token, appkey=APPKEY),
|
||||
),
|
||||
allow_redirects=False,
|
||||
)
|
||||
return r.cookies.get_dict(domain=".bilibili.com")
|
||||
|
||||
self.session.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
|
||||
h, k = getkey()
|
||||
pwd = base64.b64encode(
|
||||
rsa.encrypt(
|
||||
(h + pwd).encode('utf-8'),
|
||||
rsa.PublicKey.load_pkcs1_openssl_pem(k.encode())
|
||||
)
|
||||
)
|
||||
user = parse.quote_plus(user)
|
||||
pwd = parse.quote_plus(pwd)
|
||||
|
||||
r = self.session.post(
|
||||
'https://passport.snm0516.aisee.tv/api/tv/login',
|
||||
signed_body(
|
||||
'appkey={appkey}&build={build}&captcha=&channel=master&'
|
||||
'guid=XYEBAA3E54D502E37BD606F0589A356902FCF&mobi_app={mobi_app}&'
|
||||
'password={password}&platform={platform}&token=5598158bcd8511e2&ts=0&username={username}'
|
||||
.format(appkey=APPKEY, build=BUILD, platform=PLATFORM, mobi_app=MOBI_APP, username=user, password=pwd)),
|
||||
)
|
||||
json = r.json()
|
||||
|
||||
if json['code'] == -105:
|
||||
# need captcha
|
||||
raise Exception('TODO: login with captcha')
|
||||
|
||||
if json['code'] != 0:
|
||||
raise Exception(r.text)
|
||||
|
||||
access_token = json['data']['token_info']['access_token']
|
||||
cookie_dict = access_token_2_cookie(access_token)
|
||||
cookie = '; '.join(
|
||||
'%s=%s' % (k, v)
|
||||
for k, v in cookie_dict.items()
|
||||
)
|
||||
self.session.headers["cookie"] = cookie
|
||||
self.csrf = re.search('bili_jct=(.*?)(;|$)', cookie).group(1)
|
||||
self.mid = re.search('DedeUserID=(.*?)(;|$)', cookie).group(1)
|
||||
self.session.headers['Accept'] = 'application/json, text/javascript, */*; q=0.01'
|
||||
self.session.headers['Referer'] = 'https://space.bilibili.com/{mid}/#!/'.format(mid=self.mid)
|
||||
|
||||
return True
|
||||
|
||||
def upload(self,
|
||||
parts,
|
||||
title,
|
||||
tid,
|
||||
tag,
|
||||
desc,
|
||||
source='',
|
||||
cover='',
|
||||
no_reprint=1,
|
||||
):
|
||||
"""
|
||||
|
||||
:param parts: e.g. VideoPart('part path', 'part title', 'part desc'), or [VideoPart(...), VideoPart(...)]
|
||||
:type parts: VideoPart or list<VideoPart>
|
||||
:param title: video's title
|
||||
:type title: str
|
||||
:param tid: video type, see: https://member.bilibili.com/x/web/archive/pre
|
||||
or https://github.com/uupers/BiliSpider/wiki/%E8%A7%86%E9%A2%91%E5%88%86%E5%8C%BA%E5%AF%B9%E5%BA%94%E8%A1%A8
|
||||
:type tid: int
|
||||
:param tag: video's tag
|
||||
:type tag: list<str>
|
||||
:param desc: video's description
|
||||
:type desc: str
|
||||
:param source: (optional) 转载地址
|
||||
:type source: str
|
||||
:param cover: (optional) cover's URL, use method *cover_up* to get
|
||||
:type cover: str
|
||||
:param no_reprint: (optional) 0=可以转载, 1=禁止转载(default)
|
||||
:type no_reprint: int
|
||||
"""
|
||||
self.preUpload(parts)
|
||||
self.finishUpload(title, tid, tag, desc, source, cover, no_reprint)
|
||||
self.clear()
|
||||
|
||||
def preUpload(self, parts, max_retry=5):
|
||||
"""
|
||||
:param max_retry:
|
||||
:param parts: e.g. VideoPart('part path', 'part title', 'part desc'), or [VideoPart(...), VideoPart(...)]
|
||||
:type parts: VideoPart or list<VideoPart>
|
||||
"""
|
||||
self.session.headers['Content-Type'] = 'application/json; charset=utf-8'
|
||||
if not isinstance(parts, list):
|
||||
parts = [parts]
|
||||
|
||||
# retry by status
|
||||
retries = Retry(
|
||||
total=max_retry,
|
||||
backoff_factor=1,
|
||||
status_forcelist=(504, ),
|
||||
)
|
||||
self.session.mount('https://', HTTPAdapter(max_retries=retries))
|
||||
self.session.mount('http://', HTTPAdapter(max_retries=retries))
|
||||
#
|
||||
|
||||
for part in parts:
|
||||
filepath = part.path
|
||||
filename = os.path.basename(filepath)
|
||||
filesize = os.path.getsize(filepath)
|
||||
Common.appendUploadStatus("Upload >{}< Started".format(filepath))
|
||||
self.files.append(part)
|
||||
r = self.session.get('https://member.bilibili.com/preupload?'
|
||||
'os=upos&upcdn=ws&name={name}&size={size}&r=upos&profile=ugcupos%2Fyb&ssl=0'
|
||||
.format(name=parse.quote_plus(filename), size=filesize))
|
||||
"""return example
|
||||
{
|
||||
"upos_uri": "upos://ugc/i181012ws18x52mti3gg0h33chn3tyhp.mp4",
|
||||
"biz_id": 58993125,
|
||||
"endpoint": "//upos-hz-upcdnws.acgvideo.com",
|
||||
"endpoints": [
|
||||
"//upos-hz-upcdnws.acgvideo.com",
|
||||
"//upos-hz-upcdntx.acgvideo.com"
|
||||
],
|
||||
"chunk_retry_delay": 3,
|
||||
"chunk_retry": 200,
|
||||
"chunk_size": 4194304,
|
||||
"threads": 2,
|
||||
"timeout": 900,
|
||||
"auth": "os=upos&cdn=upcdnws&uid=&net_state=4&device=&build=&os_version=&ak=×tamp=&sign=",
|
||||
"OK": 1
|
||||
}
|
||||
"""
|
||||
json = r.json()
|
||||
upos_uri = json['upos_uri']
|
||||
endpoint = json['endpoint']
|
||||
auth = json['auth']
|
||||
biz_id = json['biz_id']
|
||||
chunk_size = json['chunk_size']
|
||||
self.session.headers['X-Upos-Auth'] = auth # add auth header
|
||||
r = self.session.post('https:{}/{}?uploads&output=json'.format(endpoint, upos_uri.replace('upos://', '')))
|
||||
# {"upload_id":"72eb747b9650b8c7995fdb0efbdc2bb6","key":"\/i181012ws2wg1tb7tjzswk2voxrwlk1u.mp4","OK":1,"bucket":"ugc"}
|
||||
json = r.json()
|
||||
upload_id = json['upload_id']
|
||||
with open(filepath, 'rb') as f:
|
||||
chunks_num = math.ceil(filesize / chunk_size)
|
||||
chunks_index = 0
|
||||
chunks_data = f.read(chunk_size)
|
||||
Common.modifyLastUploadStatus(
|
||||
"Uploading >{}< @ {:.2f}%".format(filepath, 100.0 * chunks_index / chunks_num))
|
||||
while True:
|
||||
if not chunks_data:
|
||||
break
|
||||
|
||||
def upload_chunk():
|
||||
r = self.session.put('https:{endpoint}/{upos_uri}?'
|
||||
'partNumber={part_number}&uploadId={upload_id}&chunk={chunk}&chunks={chunks}&size={size}&start={start}&end={end}&total={total}'
|
||||
.format(endpoint=endpoint,
|
||||
upos_uri=upos_uri.replace('upos://', ''),
|
||||
part_number=chunks_index + 1, # starts with 1
|
||||
upload_id=upload_id,
|
||||
chunk=chunks_index,
|
||||
chunks=chunks_num,
|
||||
size=len(chunks_data),
|
||||
start=chunks_index * chunk_size,
|
||||
end=chunks_index * chunk_size + len(chunks_data),
|
||||
total=filesize,
|
||||
),
|
||||
chunks_data,
|
||||
)
|
||||
return r
|
||||
|
||||
def retry_upload_chunk():
|
||||
"""return :class:`Response` if upload success, else return None."""
|
||||
for i in range(max_retry):
|
||||
r = upload_chunk()
|
||||
if r.status_code == 200:
|
||||
return r
|
||||
Common.modifyLastUploadStatus(
|
||||
"Uploading >{}< @ {:.2f}% RETRY[{}]".format(filepath, 100.0 * chunks_index / chunks_num, max_retry))
|
||||
return None
|
||||
|
||||
r = retry_upload_chunk()
|
||||
if r:
|
||||
Common.modifyLastUploadStatus(
|
||||
"Uploading >{}< @ {:.2f}%".format(filepath, 100.0 * chunks_index / chunks_num))
|
||||
else:
|
||||
Common.modifyLastUploadStatus(
|
||||
"Uploading >{}< FAILED @ {:.2f}%".format(filepath, 100.0 * chunks_index / chunks_num))
|
||||
continue
|
||||
chunks_data = f.read(chunk_size)
|
||||
chunks_index += 1 # start with 0
|
||||
|
||||
# NOT DELETE! Refer to https://github.com/comwrg/bilibiliupload/issues/15#issuecomment-424379769
|
||||
self.session.post('https:{endpoint}/{upos_uri}?'
|
||||
'output=json&name={name}&profile=ugcupos%2Fyb&uploadId={upload_id}&biz_id={biz_id}'
|
||||
.format(endpoint=endpoint,
|
||||
upos_uri=upos_uri.replace('upos://', ''),
|
||||
name=filename,
|
||||
upload_id=upload_id,
|
||||
biz_id=biz_id,
|
||||
),
|
||||
{"parts": [{"partNumber": i, "eTag": "etag"} for i in range(1, chunks_num + 1)]},
|
||||
)
|
||||
self.videos.append({'filename': upos_uri.replace('upos://ugc/', '').split('.')[0],
|
||||
'title': part.title,
|
||||
'desc': part.desc})
|
||||
Common.modifyLastUploadStatus("Upload >{}< Finished".format(filepath))
|
||||
__f = open("uploaded.json", "w")
|
||||
JSON.dump(self.videos, __f)
|
||||
__f.close()
|
||||
|
||||
def finishUpload(self,
|
||||
title,
|
||||
tid,
|
||||
tag,
|
||||
desc,
|
||||
source='',
|
||||
cover='',
|
||||
no_reprint=1,
|
||||
):
|
||||
"""
|
||||
:param title: video's title
|
||||
:type title: str
|
||||
:param tid: video type, see: https://member.bilibili.com/x/web/archive/pre
|
||||
or https://github.com/uupers/BiliSpider/wiki/%E8%A7%86%E9%A2%91%E5%88%86%E5%8C%BA%E5%AF%B9%E5%BA%94%E8%A1%A8
|
||||
:type tid: int
|
||||
:param tag: video's tag
|
||||
:type tag: list<str>
|
||||
:param desc: video's description
|
||||
:type desc: str
|
||||
:param source: (optional) 转载地址
|
||||
:type source: str
|
||||
:param cover: (optional) cover's URL, use method *cover_up* to get
|
||||
:type cover: str
|
||||
:param no_reprint: (optional) 0=可以转载, 1=禁止转载(default)
|
||||
:type no_reprint: int
|
||||
"""
|
||||
if len(self.videos) == 0:
|
||||
return
|
||||
Common.appendUploadStatus("[{}]投稿中,请稍后".format(title))
|
||||
self.session.headers['Content-Type'] = 'application/json; charset=utf-8'
|
||||
copyright = 2 if source else 1
|
||||
r = self.session.post('https://member.bilibili.com/x/vu/web/add?csrf=' + self.csrf,
|
||||
json={
|
||||
"copyright": copyright,
|
||||
"source": source,
|
||||
"title": title,
|
||||
"tid": tid,
|
||||
"tag": ','.join(tag),
|
||||
"no_reprint": no_reprint,
|
||||
"desc": desc,
|
||||
"cover": cover,
|
||||
"mission_id": 0,
|
||||
"order_id": 0,
|
||||
"videos": self.videos}
|
||||
)
|
||||
Common.modifyLastUploadStatus("[{}] Published | Result : {}".format(title, r.text))
|
||||
|
||||
def reloadFromPrevious(self):
|
||||
if os.path.exists("uploaded.json"):
|
||||
__f = open("uploaded.json", "r")
|
||||
try:
|
||||
self.videos = JSON.load(__f)
|
||||
Common.appendUploadStatus("RELOAD SUCCESS")
|
||||
except:
|
||||
Common.appendUploadStatus("RELOAD Failed")
|
||||
self.videos = []
|
||||
__f.close()
|
||||
os.remove("uploaded.json")
|
||||
else:
|
||||
Common.appendUploadStatus("RELOAD Failed")
|
||||
self.videos = []
|
||||
|
||||
def clear(self):
|
||||
self.files.clear()
|
||||
self.videos.clear()
|
||||
if (os.path.exists("uploaded.json")):
|
||||
os.remove("uploaded.json")
|
||||
|
||||
def appendUpload(self,
|
||||
aid,
|
||||
parts,
|
||||
title="",
|
||||
tid="",
|
||||
tag="",
|
||||
desc="",
|
||||
source='',
|
||||
cover='',
|
||||
no_reprint=1,
|
||||
):
|
||||
"""
|
||||
:param aid: just aid
|
||||
:type aid: int
|
||||
:param parts: e.g. VideoPart('part path', 'part title', 'part desc'), or [VideoPart(...), VideoPart(...)]
|
||||
:type parts: VideoPart or list<VideoPart>
|
||||
:param title: video's title
|
||||
:type title: str
|
||||
:param tid: video type, see: https://member.bilibili.com/x/web/archive/pre
|
||||
or https://github.com/uupers/BiliSpider/wiki/%E8%A7%86%E9%A2%91%E5%88%86%E5%8C%BA%E5%AF%B9%E5%BA%94%E8%A1%A8
|
||||
:type tid: int
|
||||
:param tag: video's tag
|
||||
:type tag: list<str>
|
||||
:param desc: video's description
|
||||
:type desc: str
|
||||
:param source: (optional) 转载地址
|
||||
:type source: str
|
||||
:param cover: (optional) cover's URL, use method *cover_up* to get
|
||||
:type cover: str
|
||||
:param no_reprint: (optional) 0=可以转载, 1=禁止转载(default)
|
||||
:type no_reprint: int
|
||||
"""
|
||||
self.session.headers['Content-Type'] = 'application/json; charset=utf-8'
|
||||
p = self.session.get("https://member.bilibili.com/x/web/archive/view?aid={}&history=".format(aid))
|
||||
j = p.json()
|
||||
if len(self.videos) == 0:
|
||||
for i in j['data']['videos']:
|
||||
self.videos.append({'filename': i['filename'],
|
||||
'title': i["title"],
|
||||
'desc': i["desc"]})
|
||||
if (title == ""): title = j["data"]["archive"]['title']
|
||||
if (tag == ""): tag = j["data"]["archive"]['tag']
|
||||
if (no_reprint == ""): no_reprint = j["data"]["archive"]['no_reprint']
|
||||
if (desc == ""): desc = j["data"]["archive"]['desc']
|
||||
if (source == ""): source = j["data"]["archive"]['source']
|
||||
if (tid == ""): tid = j["data"]["archive"]['tid']
|
||||
self.preUpload(parts)
|
||||
self.editUpload(aid, title, tid, tag, desc, source, cover, no_reprint)
|
||||
|
||||
def editUpload(self,
|
||||
aid,
|
||||
title,
|
||||
tid,
|
||||
tag,
|
||||
desc,
|
||||
source='',
|
||||
cover='',
|
||||
no_reprint=1,
|
||||
):
|
||||
"""
|
||||
:param aid: just aid
|
||||
:type aid: int
|
||||
:param parts: e.g. VideoPart('part path', 'part title', 'part desc'), or [VideoPart(...), VideoPart(...)]
|
||||
:type parts: VideoPart or list<VideoPart>
|
||||
:param title: video's title
|
||||
:type title: str
|
||||
:param tid: video type, see: https://member.bilibili.com/x/web/archive/pre
|
||||
or https://github.com/uupers/BiliSpider/wiki/%E8%A7%86%E9%A2%91%E5%88%86%E5%8C%BA%E5%AF%B9%E5%BA%94%E8%A1%A8
|
||||
:type tid: int
|
||||
:param tag: video's tag
|
||||
:type tag: list<str>
|
||||
:param desc: video's description
|
||||
:type desc: str
|
||||
:param source: (optional) 转载地址
|
||||
:type source: str
|
||||
:param cover: (optional) cover's URL, use method *cover_up* to get
|
||||
:type cover: str
|
||||
:param no_reprint: (optional) 0=可以转载, 1=禁止转载(default)
|
||||
:type no_reprint: int
|
||||
"""
|
||||
copyright = 2 if source else 1
|
||||
r = self.session.post('https://member.bilibili.com/x/vu/web/edit?csrf=' + self.csrf,
|
||||
json={
|
||||
"aid": aid,
|
||||
"copyright": copyright,
|
||||
"source": source,
|
||||
"title": title,
|
||||
"tid": tid,
|
||||
"tag": ','.join(tag),
|
||||
"no_reprint": no_reprint,
|
||||
"desc": desc,
|
||||
"cover": cover,
|
||||
"mission_id": 0,
|
||||
"order_id": 0,
|
||||
"videos": self.videos}
|
||||
)
|
||||
print(r.text)
|
||||
|
||||
def addChannel(self, name, intro=''):
|
||||
"""
|
||||
|
||||
:param name: channel's name
|
||||
:type name: str
|
||||
:param intro: channel's introduction
|
||||
:type intro: str
|
||||
"""
|
||||
r = self.session.post(
|
||||
url='https://space.bilibili.com/ajax/channel/addChannel',
|
||||
data={
|
||||
'name': name,
|
||||
'intro': intro,
|
||||
'aids': '',
|
||||
'csrf': self.csrf,
|
||||
},
|
||||
# name=123&intro=123&aids=&csrf=565d7ed17cef2cc8ad054210c4e64324&_=1497077610768
|
||||
|
||||
)
|
||||
# return
|
||||
# {"status":true,"data":{"cid":"15812"}}
|
||||
print(r.json())
|
||||
|
||||
def channel_addVideo(self, cid, aids):
|
||||
"""
|
||||
|
||||
:param cid: channel's id
|
||||
:type cid: int
|
||||
:param aids: videos' id
|
||||
:type aids: list<int>
|
||||
"""
|
||||
|
||||
r = self.session.post(
|
||||
url='https://space.bilibili.com/ajax/channel/addVideo',
|
||||
data={
|
||||
'aids': '%2C'.join(aids),
|
||||
'cid': cid,
|
||||
'csrf': self.csrf
|
||||
}
|
||||
# aids=9953555%2C9872953&cid=15814&csrf=565d7ed17cef2cc8ad054210c4e64324&_=1497079332679
|
||||
)
|
||||
print(r.json())
|
||||
|
||||
def cover_up(self, img):
|
||||
"""
|
||||
|
||||
:param img: img path or stream
|
||||
:type img: str or BufferedReader
|
||||
:return: img URL
|
||||
"""
|
||||
|
||||
if isinstance(img, str):
|
||||
f = open(img, 'rb')
|
||||
else:
|
||||
f = img
|
||||
r = self.session.post(
|
||||
url='https://member.bilibili.com/x/vu/web/cover/up',
|
||||
data={
|
||||
'cover': b'data:image/jpeg;base64,' + (base64.b64encode(f.read())),
|
||||
'csrf': self.csrf,
|
||||
}
|
||||
)
|
||||
# print(r.text)
|
||||
# {"code":0,"data":{"url":"http://i0.hdslb.com/bfs/archive/67db4a6eae398c309244e74f6e85ae8d813bd7c9.jpg"},"message":"","ttl":1}
|
||||
return r.json()['data']['url']
|
148
liveDownloader.py
Normal file
148
liveDownloader.py
Normal file
@ -0,0 +1,148 @@
|
||||
import time
|
||||
from datetime import datetime
|
||||
import threading
|
||||
import Common
|
||||
import os
|
||||
import requests
|
||||
|
||||
session = requests.session()
|
||||
|
||||
|
||||
def download():
|
||||
while Common.api.isLive and not Common.forceNotDownload:
|
||||
if not Common.streamUrl:
|
||||
Common.appendError("Download with No StreamUrl Specific")
|
||||
break
|
||||
path = datetime.strftime(datetime.now(), "%Y%m%d_%H%M.flv")
|
||||
try:
|
||||
p = session.get(Common.streamUrl, stream=True, timeout=3)
|
||||
p.raise_for_status()
|
||||
except Exception as e:
|
||||
Common.appendError("Download >{}< with Exception [{}]".format(path,e.__str__()))
|
||||
break
|
||||
Common.appendDownloadStatus("Download >{}< Start".format(path))
|
||||
f = open(path, "wb")
|
||||
_size = 0
|
||||
try:
|
||||
for T in p.iter_content(chunk_size=Common.config["c_s"]):
|
||||
if Common.forceNotDownload:
|
||||
Common.modifyLastDownloadStatus("Force Stop Download".format(path))
|
||||
return
|
||||
f.write(T)
|
||||
_size += len(T)
|
||||
Common.modifyLastDownloadStatus(
|
||||
"Downloading >{}< @ {:.2f}%".format(path, 100.0 * _size / Common.config["p_s"]))
|
||||
if _size > Common.config["p_s"] and not Common.config["dlO"]:
|
||||
Common.modifyLastDownloadStatus("Download >{}< Exceed MaxSize".format(path))
|
||||
break
|
||||
Common.modifyLastDownloadStatus("Download >{}< Finished".format(path))
|
||||
except Exception as e:
|
||||
Common.appendError("Download >{}< With Exception {}".format(path, e.__str__()))
|
||||
Common.api.updRoomInfo(True)
|
||||
finally:
|
||||
f.close()
|
||||
if os.path.getsize(path) < Common.config["i_s"]:
|
||||
Common.modifyLastDownloadStatus("Downloaded File >{}< is too small, will ignore it".format(path))
|
||||
else:
|
||||
Common.encodeQueue.put(path)
|
||||
Common.api.updRoomInfo(True)
|
||||
|
||||
|
||||
def encode():
|
||||
Common.appendEncodeStatus("Encode Daemon Starting")
|
||||
while True:
|
||||
i = Common.encodeQueue.get()
|
||||
Common.encodeVideo(i)
|
||||
|
||||
|
||||
def upload():
|
||||
date = datetime.strftime(datetime.now(), Common.config["sdf"])
|
||||
Common.appendUploadStatus("Upload Daemon Starting")
|
||||
i = Common.uploadQueue.get()
|
||||
while True:
|
||||
if i is True:
|
||||
Common.publishVideo(date)
|
||||
break
|
||||
try:
|
||||
Common.uploadVideo(i)
|
||||
except Exception as e:
|
||||
Common.appendError(e.__str__())
|
||||
continue
|
||||
finally:
|
||||
time.sleep(90)
|
||||
i = Common.uploadQueue.get()
|
||||
Common.appendUploadStatus("Upload Daemon Quiting")
|
||||
|
||||
|
||||
t = threading.Thread(target=download, args=())
|
||||
ut = threading.Thread(target=upload, args=())
|
||||
et = threading.Thread(target=encode, args=())
|
||||
|
||||
|
||||
def awakeEncode():
|
||||
global et
|
||||
if et.is_alive():
|
||||
return True
|
||||
et = threading.Thread(target=encode, args=())
|
||||
et.setDaemon(True)
|
||||
et.start()
|
||||
return False
|
||||
|
||||
|
||||
def awakeDownload():
|
||||
global t
|
||||
if t.is_alive():
|
||||
return True
|
||||
t = threading.Thread(target=download, args=())
|
||||
t.setDaemon(True)
|
||||
t.start()
|
||||
Common.api.updRoomInfo()
|
||||
return False
|
||||
|
||||
|
||||
def awakeUpload():
|
||||
global ut
|
||||
if ut.is_alive():
|
||||
return True
|
||||
ut = threading.Thread(target=upload, args=())
|
||||
ut.setDaemon(True)
|
||||
ut.start()
|
||||
return False
|
||||
|
||||
|
||||
def run():
|
||||
Common.refreshDownloader()
|
||||
if not Common.api.isValidUser:
|
||||
Common.appendError("[{}]用户未找到".format(Common.api.name))
|
||||
return
|
||||
while True:
|
||||
if Common.api.isLive and not Common.forceNotBroadcasting:
|
||||
if not Common.forceNotDownload:
|
||||
awakeDownload()
|
||||
if not Common.forceNotUpload:
|
||||
awakeUpload()
|
||||
if not Common.forceNotEncode:
|
||||
awakeEncode()
|
||||
try:
|
||||
Common.api.updRoomInfo()
|
||||
except Exception as e:
|
||||
Common.appendError(e.__str__())
|
||||
finally:
|
||||
time.sleep(1)
|
||||
else:
|
||||
try:
|
||||
Common.api.updRoomInfo()
|
||||
except Exception as e:
|
||||
Common.appendError(e.__str__())
|
||||
Common.refreshDownloader()
|
||||
if not Common.api.broadcaster:
|
||||
Common.refreshDownloader()
|
||||
if Common.forceStartEncodeThread:
|
||||
awakeEncode()
|
||||
Common.forceStartEncodeThread = False
|
||||
if Common.forceStartUploadThread:
|
||||
awakeUpload()
|
||||
Common.forceStartUploadThread = False
|
||||
if Common.doDelay():
|
||||
Common.uploadQueue.put(True)
|
||||
time.sleep(5)
|
13
templates/head.html
Normal file
13
templates/head.html
Normal file
@ -0,0 +1,13 @@
|
||||
<meta charset="UTF-8">
|
||||
<script src="https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js"></script>
|
||||
<style>
|
||||
td{
|
||||
border: solid 1px lightgray;
|
||||
}
|
||||
.title{
|
||||
width: 6em;
|
||||
}
|
||||
.time{
|
||||
width: 10em;
|
||||
}
|
||||
</style>
|
Reference in New Issue
Block a user