Skip to content

Commit

Permalink
Misc: Logging changes, dependency update
Browse files Browse the repository at this point in the history
  • Loading branch information
fearnlj01 committed Nov 12, 2023
1 parent bcac83e commit 040b6ff
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
8 changes: 6 additions & 2 deletions Helpers/DiscordHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public async Task<string> SendWebhook(IVideoFile file, string dumpResult, AniDBS
{
Webhook webhook = GetUnmatchedWebhook(file, dumpResult, searchResult);

_logger.Info(CultureInfo.InvariantCulture, "Sending Discord webhook (fileId={fileId})", file.VideoFileID);

HttpResponseMessage response = await _httpClient.PostAsJsonAsync($"{BaseUrl}?wait=true", webhook, _options);
_ = response.EnsureSuccessStatusCode();

Expand All @@ -57,13 +59,15 @@ public async Task<string> SendWebhook(IVideoFile file, string dumpResult, AniDBS
catch (Exception ex)
{
// TODO: More logging
_logger.Warn("Exception: {ex}", ex);
_logger.Debug("Exception: {ex}", ex);
return null;
}
}

public async Task PatchWebhook(IVideoFile file, IAnime anime, IEpisode episode, MemoryStream imageStream, string messageId)
{
_logger.Info(CultureInfo.InvariantCulture, "Attempting to update Discord message (fileId={fileId}, messageId={messageId})", file.VideoFileID, messageId);

try
{
MultipartFormDataContent form = new();
Expand All @@ -89,7 +93,7 @@ public async Task PatchWebhook(IVideoFile file, IAnime anime, IEpisode episode,
catch (Exception ex)
{
// TODO: More logging
_logger.Warn("Exception: {ex}", ex);
_logger.Debug("Exception: {ex}", ex);
}
}

Expand Down
35 changes: 20 additions & 15 deletions Helpers/ShokoHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
Expand Down Expand Up @@ -40,19 +41,19 @@ public async Task DumpFile(int fileId)
{
try
{
var requestObject = new
_logger.Info(CultureInfo.InvariantCulture, "Plugin triggering automatic AVDump (fileId={fileId})", fileId);

HttpResponseMessage response = await _httpClient.PostAsJsonAsync("AVDump/DumpFiles", new
{
FileIDs = new[] { fileId },
Priority = false
};
var json = new StringContent(JsonSerializer.Serialize(requestObject), Encoding.UTF8, "application/json");

HttpResponseMessage response = await _httpClient.PostAsync("AVDump/DumpFiles", json);
});
_ = response.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
_logger.Warn("Exception: {ex}", ex);
_logger.Warn(CultureInfo.InvariantCulture, "Failed to process AVDump request (fileId={fileId})", fileId);
_logger.Debug("Exception: {ex}", ex);
}
}

Expand All @@ -72,8 +73,8 @@ public async Task<AniDBSearchResult> MatchTitle(string filename)
ex is HttpRequestException or JsonException or ArgumentNullException or InvalidOperationException
)
{
_logger.Warn($"Unable to retrieve information about the file ('{filename}') from AniDB");
_logger.Warn("Exception: {ex}", ex);
_logger.Warn(CultureInfo.InvariantCulture, "Unable to retrieve title information for a file (fileName='{filename}') from AniDB", filename);
_logger.Debug("Exception: {ex}", ex);
return null;
}
}
Expand All @@ -84,6 +85,8 @@ public async Task ScanFile(IVideoFile file, int autoMatchAttempts = 1)
{
await Task.Delay(TimeSpan.FromMinutes(autoMatchAttempts * 5));

_logger.Info(CultureInfo.InvariantCulture, "Requesting file rescan (fileId={fileID}, matchAttempts={matchAttempts})", file.VideoFileID, autoMatchAttempts);

HttpResponseMessage response = await _httpClient.PostAsync($"File/{file.VideoFileID}/Rescan", null);
_ = response.EnsureSuccessStatusCode();
}
Expand All @@ -98,12 +101,14 @@ public async Task ScanFileById(int fileId)
{
try
{
_logger.Info(CultureInfo.InvariantCulture, "Requesting file rescan (fileId={fileID})", fileId);

HttpResponseMessage response = await _httpClient.PostAsync($"File/{fileId}/Rescan", null);
_ = response.EnsureSuccessStatusCode();
}
catch (HttpRequestException ex)
{
_logger.Warn($"Unable to scan file by ID ('{fileId}')");
_logger.Warn(CultureInfo.InvariantCulture, "Unable to scan file by ID ('{fileId}')", fileId);
_logger.Warn("Exception: ", ex);
}
}
Expand All @@ -117,14 +122,14 @@ public async Task<AniDBPoster> GetSeriesPoster(IAnime anime)

using Stream responseStream = await response.Content.ReadAsStreamAsync();
using JsonDocument jsonDoc = await JsonDocument.ParseAsync(responseStream);

string image = jsonDoc.RootElement.GetProperty("Images").GetProperty("Posters")[0].GetRawText();
return JsonSerializer.Deserialize<AniDBPoster>(image);
}
catch (HttpRequestException ex)
{
_logger.Warn($"Poster could not be downloaded for series ID: {anime.AnimeID}");
_logger.Warn("Exception: {ex}", ex);
_logger.Warn(CultureInfo.InvariantCulture, "Poster could not be downloaded for series ID: {animeId}", anime.AnimeID);
_logger.Debug("Exception: {ex}", ex);
return null;
}
}
Expand All @@ -146,8 +151,8 @@ public async Task<MemoryStream> GetImageStream(AniDBPoster poster)
}
catch (HttpRequestException ex)
{
_logger.Warn($"Could not retreieve image for the primary {poster.Source} {poster.Type} of {poster.ID}");
_logger.Warn("Exception: {ex}", ex);
_logger.Warn(CultureInfo.InvariantCulture, "Could not retreieve image for the primary {poster.Source} {poster.Type} of {poster.ID}", poster.Source, poster.Type, poster.ID);
_logger.Debug("Exception: {ex}", ex);
return null;
}
}
Expand Down
4 changes: 2 additions & 2 deletions WebhookDump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void OnFileNotMatched(object sender, FileNotMatchedEventArgs fileNotMatc
}
catch (Exception ex)
{
_logger.Warn("Exception: {ex}", ex);
_logger.Debug("Exception: {ex}", ex);
}
}
}
Expand Down Expand Up @@ -125,7 +125,7 @@ private async void OnFileMatched(object sender, FileMatchedEventArgs fileMatched
}
catch (Exception ex)
{
_logger.Warn("Exception: {ex}", ex);
_logger.Debug("Exception: {ex}", ex);
}
}

Expand Down
2 changes: 1 addition & 1 deletion WebhookDump.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<PackageReference Include="NLog" Version="5.1.2">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Shoko.Plugin.Abstractions" Version="2.5.0-alpha1">
<PackageReference Include="Shoko.Plugin.Abstractions" Version="2.5.0-alpha3">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
Expand Down

0 comments on commit 040b6ff

Please sign in to comment.