Skip to content

Commit

Permalink
refactor: update used release group query
Browse files Browse the repository at this point in the history
and add v3 release group endpoint to get used, unused, or all anidb
release groups.
  • Loading branch information
revam committed Nov 20, 2023
1 parent 5083c0b commit 2dd60c4
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static string ReplaceCaseInsensitive(string input, string search, string
private static string RemoveSubgroups(string value)
{
var originalLength = value.Length;
var releaseGroups = RepoFactory.AniDB_ReleaseGroup.GetAllReleaseGroups();
var releaseGroups = RepoFactory.AniDB_ReleaseGroup.GetUsedReleaseGroups().Select(r => r.GroupName);
foreach (var releaseGroup in releaseGroups)
{
value = ReplaceCaseInsensitive(value, releaseGroup, string.Empty);
Expand Down Expand Up @@ -154,7 +154,7 @@ public List<CL_AniDB_Anime> SearchAnimeWithFilename(int uid, [FromForm]string qu
[HttpGet("ReleaseGroups")]
public List<string> GetAllReleaseGroups()
{
return RepoFactory.AniDB_ReleaseGroup.GetAllReleaseGroups().ToList();
return RepoFactory.AniDB_ReleaseGroup.GetUsedReleaseGroups().Select(r => r.GroupName).ToList();
}

[HttpGet("File/DeleteMultipleFilesWithPreferences/{userID}")]
Expand Down
47 changes: 47 additions & 0 deletions Shoko.Server/API/v3/Controllers/AniDBController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Shoko.Server.API.Annotations;
using Shoko.Server.API.v3.Helpers;
using Shoko.Server.API.v3.Models.Common;
using Shoko.Server.API.v3.Models.Shoko;
using Shoko.Server.Repositories;
using Shoko.Server.Settings;

namespace Shoko.Server.API.v3.Controllers;

[ApiController]
[Route("/api/v{version:apiVersion}/[controller]")]
[ApiV3]
[Authorize]
public class AniDBController : BaseController
{
/// <summary>
/// Get the known anidb release groups stored in shoko.
/// </summary>
/// <param name="pageSize">The page size. Set to <code>0</code> to disable pagination.</param>
/// <param name="page">The page index.</param>
/// <param name="includeMissing">Include missing release groups.</param>
/// <returns></returns>
[HttpGet("ReleaseGroup")]
public ActionResult<ListResult<ReleaseGroup>> GetReleaseGroups(
[FromQuery, Range(0, 1000)] int pageSize = 20,
[FromQuery, Range(1, int.MaxValue)] int page = 1,
[FromQuery] IncludeOnlyFilter includeMissing = IncludeOnlyFilter.False)
{
return includeMissing switch
{
IncludeOnlyFilter.False => RepoFactory.AniDB_ReleaseGroup.GetUsedReleaseGroups()
.ToListResult(g => new ReleaseGroup(g), page, pageSize),
IncludeOnlyFilter.Only => RepoFactory.AniDB_ReleaseGroup.GetUnusedReleaseGroups()
.ToListResult(g => new ReleaseGroup(g), page, pageSize),
_ => RepoFactory.AniDB_ReleaseGroup.GetAll()
.ToListResult(g => new ReleaseGroup(g), page, pageSize),
};
}

public AniDBController(ISettingsProvider settingsProvider) : base(settingsProvider)
{
}
}
24 changes: 2 additions & 22 deletions Shoko.Server/API/v3/Models/Shoko/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,7 @@ public AniDB(SVR_AniDB_File anidb)
{
ID = anidb.FileID;
Source = ParseFileSource(anidb.File_Source);
ReleaseGroup = new AniDBReleaseGroup
{
ID = anidb.GroupID, Name = anidb.Anime_GroupName, ShortName = anidb.Anime_GroupNameShort
};
ReleaseGroup = new ReleaseGroup(anidb.ReleaseGroup);
ReleaseDate = anidb.File_ReleaseDate == 0
? null
: Commons.Utils.AniDB.GetAniDBDateAsDate(anidb.File_ReleaseDate);
Expand Down Expand Up @@ -262,7 +259,7 @@ public AniDB(SVR_AniDB_File anidb)
/// <summary>
/// The Release Group. This is usually set, but sometimes is set as "raw/unknown"
/// </summary>
public AniDBReleaseGroup ReleaseGroup { get; set; }
public ReleaseGroup ReleaseGroup { get; set; }

/// <summary>
/// The file's release date. This is probably not filled in
Expand Down Expand Up @@ -321,23 +318,6 @@ public AniDB(SVR_AniDB_File anidb)
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTime Updated { get; set; }

public class AniDBReleaseGroup
{
/// <summary>
/// The Release Group's Name (Unlimited Translation Works)
/// </summary>
public string Name { get; set; }

/// <summary>
/// The Release Group's Name (UTW)
/// </summary>
public string ShortName { get; set; }

/// <summary>
/// AniDB ID
/// </summary>
public int ID { get; set; }
}
}

public class CrossReferenceIDs
Expand Down
37 changes: 37 additions & 0 deletions Shoko.Server/API/v3/Models/Shoko/ReleaseGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

using Shoko.Models.Server;

namespace Shoko.Server.API.v3.Models.Shoko;


public class ReleaseGroup
{
/// <summary>
/// AniDB release group ID (69)
/// /// </summary>
public int ID { get; set; }

/// <summary>
/// The Release Group's Name (Unlimited Translation Works)
/// </summary>
public string Name { get; set; }

/// <summary>
/// The Release Group's Name (UTW)
/// </summary>
public string ShortName { get; set; }

/// <summary>
/// Source. Anidb, User, etc.
/// </summary>
/// <value></value>
public string Source { get; set; }

public ReleaseGroup(AniDB_ReleaseGroup group)
{
ID = group.GroupID;
Name = group.GroupName;
ShortName = group.ShortName;
Source = "AniDB";
}
}
12 changes: 10 additions & 2 deletions Shoko.Server/Models/SVR_AniDB_File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@ public class SVR_AniDB_File : AniDB_File, IAniDBFile
[XmlIgnore]
public List<CrossRef_File_Episode> EpisodeCrossRefs => RepoFactory.CrossRef_File_Episode.GetByHash(Hash);

public string Anime_GroupName => RepoFactory.AniDB_ReleaseGroup.GetByGroupID(GroupID)?.Name;
public string Anime_GroupNameShort => RepoFactory.AniDB_ReleaseGroup.GetByGroupID(GroupID)?.ShortName;
// NOTE: I want to cache it, but i won't for now. not until the anidb files and release groups are stored in a non-cached repo.
public AniDB_ReleaseGroup ReleaseGroup =>
RepoFactory.AniDB_ReleaseGroup.GetByGroupID(GroupID) ?? new()
{
GroupID = GroupID,
GroupName = "",
GroupNameShort = "",
};

public string Anime_GroupName => ReleaseGroup?.Name;
public string Anime_GroupNameShort => ReleaseGroup?.ShortName;

public string SubtitlesRAW
{
Expand Down
53 changes: 36 additions & 17 deletions Shoko.Server/Repositories/Cached/AniDB_ReleaseGroupRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using NutzCode.InMemoryIndex;
using Shoko.Models.Server;
using Shoko.Server.Databases;
Expand All @@ -14,29 +15,47 @@ public AniDB_ReleaseGroup GetByGroupID(int id)
return ReadLock(() => GroupIDs.GetOne(id));
}

public IList<string> GetAllReleaseGroups()
const string UsedReleaseGroupsQuery = @"SELECT {g.*}
FROM AniDB_File f
INNER JOIN AniDB_ReleaseGroup g ON f.GroupID = g.GroupID
INNER JOIN CrossRef_File_Episode x ON x.Hash = f.Hash
GROUP BY g.GroupID
ORDER BY g.GroupName ASC";

public IReadOnlyList<AniDB_ReleaseGroup> GetUsedReleaseGroups()
{
var query =
@"SELECT g.GroupName
FROM AniDB_File a
INNER JOIN AniDB_ReleaseGroup g ON a.GroupID = g.GroupID
INNER JOIN CrossRef_File_Episode xref1 ON xref1.Hash = a.Hash
GROUP BY g.GroupName
ORDER BY count(DISTINCT xref1.AnimeID) DESC, g.GroupName ASC";

var result = Lock(() =>
var results = Lock(() =>
{
using var session = DatabaseFactory.SessionFactory.OpenSession();
return session.CreateSQLQuery(query).List<string>();
return session.CreateSQLQuery(UsedReleaseGroupsQuery).AddEntity("g", typeof(AniDB_ReleaseGroup))
.List<object>();
});
return results
.Select(result => (AniDB_ReleaseGroup)result)
.Where(result => !string.Equals(result.GroupName, "raw/unknown", System.StringComparison.InvariantCultureIgnoreCase))
.ToList();
}

if (result.Contains("raw/unknown"))
{
result.Remove("raw/unknown");
}
const string UnusedReleaseGroupsQuery = @"SELECT {g.*}
FROM AniDB_ReleaseGroup g
LEFT JOIN AniDB_File f ON f.GroupID = g.GroupID
WHERE f.GroupID IS NULL
GROUP BY g.GroupID
ORDER BY g.GroupName ASC";

return result;
}
public IReadOnlyList<AniDB_ReleaseGroup> GetUnusedReleaseGroups()
{
var results = Lock(() =>
{
using var session = DatabaseFactory.SessionFactory.OpenSession();
return session.CreateSQLQuery(UnusedReleaseGroupsQuery).AddEntity("g", typeof(AniDB_ReleaseGroup))
.List<object>();
});
return results
.Select(result => (AniDB_ReleaseGroup)result)
.Where(result => !string.Equals(result.GroupName, "raw/unknown", System.StringComparison.InvariantCultureIgnoreCase))
.ToList();
}

public override void PopulateIndexes()
{
Expand Down

0 comments on commit 2dd60c4

Please sign in to comment.