fix: fix dandan api authentication. #69

This commit is contained in:
cxfksword
2025-02-03 14:17:47 +08:00
parent 0b4a566fa9
commit 8d1dd6c379
3 changed files with 48 additions and 15 deletions

View File

@@ -16,6 +16,13 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Inject DANDNA Api ID
env:
DANDAN_API_ID: ${{ secrets.DANDAN_API_ID }}
DANDAN_API_SECRET: ${{ secrets.DANDAN_API_SECRET }}
run: |
sed -i "s/API_ID = \"\"/API_ID = \"$DANDAN_API_ID\"/g" Jellyfin.Plugin.Danmu/Scrapers/Dandan/DandanApi.cs
sed -i "s/API_SECRET = \"\"/API_SECRET = \"$DANDAN_API_SECRET\"/g" Jellyfin.Plugin.Danmu/Scrapers/Dandan/DandanApi.cs
- name: Get tags (For CHANGELOG)
run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- name: Setup dotnet

View File

@@ -17,6 +17,13 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Inject DANDNA Api ID
env:
DANDAN_API_ID: ${{ secrets.DANDAN_API_ID }}
DANDAN_API_SECRET: ${{ secrets.DANDAN_API_SECRET }}
run: |
sed -i "s/API_ID = \"\"/API_ID = \"$DANDAN_API_ID\"/g" Jellyfin.Plugin.Danmu/Scrapers/Dandan/DandanApi.cs
sed -i "s/API_SECRET = \"\"/API_SECRET = \"$DANDAN_API_SECRET\"/g" Jellyfin.Plugin.Danmu/Scrapers/Dandan/DandanApi.cs
- name: Get tags (For CHANGELOG)
run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- name: Setup dotnet

View File

@@ -3,20 +3,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Jellyfin.Extensions.Json;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller;
using Microsoft.Extensions.Logging;
using Jellyfin.Plugin.Danmu.Model;
using System.Threading;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Common.Net;
using System.Net.Http.Json;
using System.Security.Cryptography;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using System.Net;
using System.Web;
using Microsoft.Extensions.Caching.Memory;
using Jellyfin.Plugin.Danmu.Scrapers.Dandan.Entity;
@@ -27,6 +18,8 @@ namespace Jellyfin.Plugin.Danmu.Scrapers.Dandan;
public class DandanApi : AbstractApi
{
const string API_ID = "";
const string API_SECRET = "";
private static readonly object _lock = new object();
private DateTime lastRequestTime = DateTime.Now.AddDays(-1);
@@ -67,8 +60,7 @@ public class DandanApi : AbstractApi
keyword = HttpUtility.UrlEncode(keyword);
var url = $"https://api.dandanplay.net/api/v2/search/anime?keyword={keyword}";
var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var response = await this.Request(url, cancellationToken).ConfigureAwait(false);
var result = await response.Content.ReadFromJsonAsync<SearchResult>(_jsonOptions, cancellationToken).ConfigureAwait(false);
if (result != null && result.Success)
{
@@ -95,8 +87,7 @@ public class DandanApi : AbstractApi
}
var url = $"https://api.dandanplay.net/api/v2/bangumi/{animeId}";
var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var response = await this.Request(url, cancellationToken).ConfigureAwait(false);
var result = await response.Content.ReadFromJsonAsync<AnimeResult>(_jsonOptions, cancellationToken).ConfigureAwait(false);
if (result != null && result.Success && result.Bangumi != null)
@@ -125,8 +116,7 @@ public class DandanApi : AbstractApi
var withRelated = this.Config.WithRelatedDanmu ? "true" : "false";
var chConvert = this.Config.ChConvert;
var url = $"https://api.dandanplay.net/api/v2/comment/{epId}?withRelated={withRelated}&chConvert={chConvert}";
var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var response = await this.Request(url, cancellationToken).ConfigureAwait(false);
var result = await response.Content.ReadFromJsonAsync<CommentResult>(_jsonOptions, cancellationToken).ConfigureAwait(false);
if (result != null)
{
@@ -153,4 +143,33 @@ public class DandanApi : AbstractApi
}
}
protected async Task<HttpResponseMessage> Request(string url, CancellationToken cancellationToken)
{
var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
var signature = GenerateSignature(url, timestamp);
httpClient.DefaultRequestHeaders.Add("X-AppId", API_ID);
httpClient.DefaultRequestHeaders.Add("X-Signature", signature);
httpClient.DefaultRequestHeaders.Add("X-Timestamp", timestamp.ToString());
var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
return response;
}
protected string GenerateSignature(string url, long timestamp)
{
if (string.IsNullOrEmpty(API_ID) || string.IsNullOrEmpty(API_SECRET))
{
throw new Exception("弹弹接口缺少API_ID和API_SECRET");
}
var uri = new Uri(url);
var path = uri.AbsolutePath;
var str = $"{API_ID}{timestamp}{path}{API_SECRET}";
using (var sha256 = SHA256.Create())
{
var hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(str));
return Convert.ToBase64String(hashBytes);
}
}
}