Skip to content

Commit

Permalink
feat: bililive url
Browse files Browse the repository at this point in the history
  • Loading branch information
Misaka-L committed Jul 27, 2024
1 parent 95d0602 commit a43bfc1
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
36 changes: 36 additions & 0 deletions MisakaBiliApi/Controllers/BiliLiveController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.AspNetCore.Mvc;
using MisakaBiliApi.Models.ApiResponse;
using MisakaBiliCore.Models;
using MisakaBiliCore.Models.BiliApi;
using MisakaBiliCore.Services.BiliApi;

namespace MisakaBiliApi.Controllers;

[ApiController]
[Route("live")]
public class BiliLiveController(IBiliLiveApiService biliLiveApiService) : ControllerBase
{
[HttpGet("url")]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType<MisakaVideoUrlResponse>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status302Found)]
[Produces("application/json")]
public async Task<IActionResult> GetLiveUrl(string cid, LiveStreamType streamType = LiveStreamType.M3U8,
bool redirect = false)
{
var liveUrlResponse =
await biliLiveApiService.GetLiveUrlByRoomId(cid, streamType == LiveStreamType.Flv ? "web" : "h5");

var liveUrl = liveUrlResponse.Data.Durl.First();

if (redirect)
return Redirect(liveUrl.Url.ToString());

return Ok(new MisakaVideoUrlResponse(
Url: liveUrl.Url.ToString(),
Format: streamType.ToString(),
TimeLength: liveUrl.Length,
Quality: BiliVideoQuality.R1080P
));
}
}
23 changes: 23 additions & 0 deletions MisakaBiliApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@

builder.Services.AddHostedService<BiliApiCredentialRefreshHostService>();

#region HttpClient & Refit

#region HttpClient

builder.Services.AddHttpClient("biliapi", client =>
{
foreach (var (headerName, value) in defaultRequestHeader)
Expand Down Expand Up @@ -106,9 +110,26 @@
CookieContainer = services.GetRequiredService<BiliApiSecretStorageService>().CookieContainer
});

#endregion

builder.Services.AddRefitClient<IBiliApiServices>(null, httpClientName: "biliapi")
.AddHttpMessageHandler<WbiRequestHandler>();

builder.Services.AddRefitClient<IBiliLiveApiService>()
.ConfigureHttpClient(client =>
{
foreach (var (headerName, value) in defaultRequestHeader)
{
client.DefaultRequestHeaders.Add(headerName, value);
}

client.BaseAddress = new Uri("https://api.live.bilibili.com");
}).ConfigurePrimaryHttpMessageHandler(services => new SocketsHttpHandler
{
AutomaticDecompression = DecompressionMethods.All,
CookieContainer = services.GetRequiredService<BiliApiSecretStorageService>().CookieContainer
});

builder.Services.AddRefitClient<IBiliPassportApiService>()
.ConfigureHttpClient(client =>
{
Expand All @@ -124,6 +145,8 @@
CookieContainer = services.GetRequiredService<BiliApiSecretStorageService>().CookieContainer
});

#endregion

builder.Services.AddControllers();

builder.Host.UseSerilog();
Expand Down
8 changes: 8 additions & 0 deletions MisakaBiliCore/Models/BiliApi/BiliLiveQuality.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MisakaBiliCore.Models.BiliApi;

public enum BiliLiveQuality
{
Smooth = 2,
High = 3,
Original = 4
}
37 changes: 37 additions & 0 deletions MisakaBiliCore/Models/BiliApi/BiliLiveUrlResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Text.Json.Serialization;

namespace MisakaBiliCore.Models.BiliApi;

public class BiliLiveUrlResponse
{
[JsonPropertyName("current_quality")] public long CurrentQuality { get; set; }

[JsonPropertyName("accept_quality")] public string[] AcceptQuality { get; set; }

[JsonPropertyName("current_qn")] public long CurrentQn { get; set; }

[JsonPropertyName("quality_description")]
public QualityDescription[] QualityDescription { get; set; }

[JsonPropertyName("durl")] public Durl[] Durl { get; set; }
}

public class Durl
{
[JsonPropertyName("url")] public Uri Url { get; set; }

[JsonPropertyName("length")] public long Length { get; set; }

[JsonPropertyName("order")] public long Order { get; set; }

[JsonPropertyName("stream_type")] public long StreamType { get; set; }

[JsonPropertyName("p2p_type")] public long P2PType { get; set; }
}

public class QualityDescription
{
[JsonPropertyName("qn")] public long Qn { get; set; }

[JsonPropertyName("desc")] public string Desc { get; set; }
}
7 changes: 7 additions & 0 deletions MisakaBiliCore/Models/LiveStreamType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace MisakaBiliCore.Models;

public enum LiveStreamType
{
M3U8,
Flv
}
13 changes: 13 additions & 0 deletions MisakaBiliCore/Services/BiliApi/IBiliLiveApiService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using MisakaBiliCore.Models.BiliApi;
using Refit;

namespace MisakaBiliCore.Services.BiliApi;

public interface IBiliLiveApiService
{
[Get("/room/v1/Room/playUrl")]
public Task<BiliApiResponse<BiliLiveUrlResponse>> GetLiveUrlByRoomId(
[AliasAs("cid")] string cid,
[AliasAs("platform")] string platform = "web",
[AliasAs("quality")] int quality = (int)BiliLiveQuality.Original);
}

0 comments on commit a43bfc1

Please sign in to comment.