Skip to content

fix: add DB migration & endpoint to download missing TMDB People #1203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
42 changes: 42 additions & 0 deletions Shoko.Server/API/v3/Controllers/TmdbController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,48 @@ public async Task<ActionResult> DownloadImagesForTmdbShowByShowID(
return NoContent();
}

/// <summary>
/// Download any missing TMDB Persons.
/// </summary>
/// <param name="removeErrors">Removes all references to the bad TMDB Person if unable to successfully download the person</param>
/// <returns> <see cref="OkResult"/> with a summary of found/updated/skipped/deleted actions. </returns>
[HttpGet("Person/DownloadMissing")]
public async Task<ActionResult> RepairMissingTmdbPersons([FromQuery] bool removeErrors = false)
{
var personIds = new HashSet<int>();
var (errorCount, removedCount) = (0, 0);

var people = RepoFactory.TMDB_Person.GetAll().Select(person => person.TmdbPersonID).ToList();

RepoFactory.TMDB_Episode_Cast.GetAll().AsParallel().Where(p =>
!people.Contains(p.TmdbPersonID)).ForEach(p => personIds.Add(p.TmdbPersonID));
RepoFactory.TMDB_Episode_Crew.GetAll().AsParallel().Where(p =>
!people.Contains(p.TmdbPersonID)).ForEach(p => personIds.Add(p.TmdbPersonID));

RepoFactory.TMDB_Movie_Cast.GetAll().AsParallel().Where(p =>
!people.Contains(p.TmdbPersonID)).ForEach(p => personIds.Add(p.TmdbPersonID));
RepoFactory.TMDB_Movie_Crew.GetAll().AsParallel().Where(p =>
!people.Contains(p.TmdbPersonID)).ForEach(p => personIds.Add(p.TmdbPersonID));

foreach (var id in personIds)
try
{
await _tmdbMetadataService.UpdatePerson(id, forceRefresh: true);
}
catch (NullReferenceException)
{
_logger.LogInformation("Unable to find TMDB Person record {@Id} on TMDB", id);
errorCount++;
if (removeErrors && await _tmdbMetadataService.PurgePerson(id, forceRemoval: true))
removedCount++;
}

var updateCount = personIds.Count - errorCount;
var skippedCount = errorCount - removedCount;

return Ok($"(Found/Updated/Skipped/Deleted) ({personIds.Count}/{updateCount}/{skippedCount}/{removedCount}) missing TMDB person(s).");
}

#endregion

#region Online (Search / Bulk / Single)
Expand Down
4 changes: 2 additions & 2 deletions Shoko.Server/Providers/TMDB/TmdbMetadataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2072,11 +2072,11 @@ private async Task DownloadPersonImages(int personId, ProfileImages images, bool
await _imageService.DownloadImagesByType(images.Profiles, ImageEntityType.Person, ForeignEntityType.Person, personId, settings.TMDB.MaxAutoStaffImages, [], forceDownload);
}

public async Task<bool> PurgePerson(int personId, bool removeImageFiles = true)
public async Task<bool> PurgePerson(int personId, bool removeImageFiles = true, bool forceRemoval = false)
{
using (await GetLockForEntity(ForeignEntityType.Person, personId, "metadata & images", "Purge"))
{
if (IsPersonLinkedToOtherEntities(personId))
if (!forceRemoval && IsPersonLinkedToOtherEntities(personId))
return false;

var person = _tmdbPeople.GetByTmdbPersonID(personId);
Expand Down