using BBDown.Core.Protobuf;
using Google.Protobuf;
using System.Buffers.Binary;
using System.IO.Compression;
using System.Text.Json;
using System.Text.Json.Serialization;
using static BBDown.Core.Util.HTTPUtil;
using static BBDown.Core.Logger;
namespace BBDown.Core;
static class AppHelper
{
private static readonly string API = "https://grpc.biliapi.net/bilibili.app.playurl.v1.PlayURL/PlayView";
private static readonly string API2 = "https://app.bilibili.com/bilibili.pgc.gateway.player.v2.PlayURL/PlayView";
private static readonly string dalvikVer = "2.1.0";
private static readonly string osVer = "11";
private static readonly string brand = "M2012K11AC";
private static readonly string model = "Build/RKQ1.200826.002";
private static readonly string appVer = "7.32.0";
private static readonly int build = 7320200; // 新版才能抓到配音
private static readonly string channel = "xiaomi_cn_tv.danmaku.bili_zm20200902";
private static readonly Network.Types.TYPE networkType = Network.Types.TYPE.Wifi;
private static readonly string networkOid = "46007";
private static readonly string cronet = "1.36.1";
private static readonly string buvid = "";
private static readonly string mobiApp = "android";
private static readonly string appKey = "android64";
private static readonly string sessionId = "dedf8669";
private static readonly string platform = "android";
private static readonly string env = "prod";
private static readonly int appId = 1;
private static readonly string region = "CN";
private static readonly string language = "zh";
private static PlayViewReq.Types.CodeType GetVideoCodeType(string code)
{
return code switch
{
"AVC" => PlayViewReq.Types.CodeType.Code264,
"HEVC" => PlayViewReq.Types.CodeType.Code265,
"AV1" => PlayViewReq.Types.CodeType.Codeav1,
_ => PlayViewReq.Types.CodeType.Code265
};
}
///
/// 发起请求并返回响应报文(protobuf -> json)
///
///
///
///
///
///
public static async Task DoReqAsync(string aid, string cid, string epId, string qn, bool bangumi, string encoding, string appkey = "")
{
var headers = GetHeader(appkey);
LogDebug("App-Req-Headers: {0}", JsonSerializer.Serialize(headers, JsonContext.Default.DictionaryStringString));
byte[] data;
// 只有pgc接口才有配音和片头尾信息
if (bangumi)
{
if (!(string.IsNullOrEmpty(encoding) || encoding == "HEVC"))
LogWarn("APP的番剧不支持 HEVC 以外的编码");
var body = GetPayload(Convert.ToInt64(epId), Convert.ToInt64(cid), Convert.ToInt64(qn), PlayViewReq.Types.CodeType.Code265);
data = await GetPostResponseAsync(API2, body, headers);
}
else
{
var body = GetPayload(Convert.ToInt64(aid), Convert.ToInt64(cid), Convert.ToInt64(qn), GetVideoCodeType(encoding));
data = await GetPostResponseAsync(API, body, headers);
}
var resp = new MessageParser(() => new PlayViewReply()).ParseFrom(ReadMessage(data));
LogDebug("PlayViewReplyPlain: {0}", JsonSerializer.Serialize(resp, JsonContext.Default.PlayViewReply));
return ConvertToDashJson(resp);
}
///
/// 将protobuf转换成网页那种json 这样就不用修改之前的解析逻辑了
///
///
///
private static string ConvertToDashJson(object data)
{
var resp = (PlayViewReply)data;
var videos = new List