Automatically create libraries

This commit is contained in:
Zoe Roux 2023-03-11 19:48:59 +09:00
parent fa6bb695f3
commit 95257771a5
5 changed files with 103 additions and 5 deletions

View File

@ -1,4 +1,4 @@
# Authors
Ordered by the date of the first commit.
* Zoe Roux ([@AnonymusRaccoon](http://github.com/AnonymusRaccoon))
* Zoe Roux ([@zoriya](http://github.com/zoriya))

View File

@ -84,6 +84,7 @@ namespace Kyoo.Core
builder.RegisterTask<RegisterEpisode>();
builder.RegisterTask<RegisterSubtitle>();
builder.RegisterTask<MetadataProviderLoader>();
builder.RegisterTask<LibraryCreator>();
static bool DatabaseIsPresent(IComponentRegistryBuilder x)
=> x.IsRegistered(new TypedService(typeof(DatabaseContext)));

View File

@ -0,0 +1,97 @@
// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Models.Attributes;
using Kyoo.Utils;
using Microsoft.Extensions.Logging;
namespace Kyoo.Core.Tasks
{
/// <summary>
/// A task to add new video files.
/// </summary>
[TaskMetadata("library-creator", "Create libraries", "Create libraries on the library root folder.",
RunOnStartup = true, Priority = 500)]
public class LibraryCreator : ITask
{
/// <summary>
/// The library manager used to get libraries and providers to use.
/// </summary>
private readonly ILibraryManager _libraryManager;
/// <summary>
/// A task manager used to create sub tasks for each episode to add to the database.
/// </summary>
private readonly ITaskManager _taskManager;
/// <summary>
/// The logger used to inform the current status to the console.
/// </summary>
private readonly ILogger<Crawler> _logger;
/// <summary>
/// Create a new <see cref="Crawler"/>.
/// </summary>
/// <param name="libraryManager">The library manager to retrieve existing episodes/library/tracks</param>
/// <param name="taskManager">The task manager used to start <see cref="RegisterEpisode"/>.</param>
/// <param name="logger">The logger used print messages.</param>
public LibraryCreator(ILibraryManager libraryManager,
ITaskManager taskManager,
ILogger<Crawler> logger)
{
_libraryManager = libraryManager;
_taskManager = taskManager;
_logger = logger;
}
/// <inheritdoc />
public TaskParameters GetParameters()
{
return new();
}
/// <inheritdoc />
public async Task Run(TaskParameters arguments, IProgress<float> progress, CancellationToken cancellationToken)
{
ICollection<Provider> providers = await _libraryManager.GetAll<Provider>();
ICollection<string> existings = (await _libraryManager.GetAll<Library>()).SelectMany(x => x.Paths).ToArray();
IEnumerable<Library> newLibraries = Directory.GetDirectories(Environment.GetEnvironmentVariable("LIBRARY_ROOT") ?? "/video")
.Where(x => !existings.Contains(x))
.Select(x => new Library
{
Slug = Utility.ToSlug(Path.GetFileName(x)),
Name = Path.GetFileName(x),
Paths = new string[] { x },
Providers = providers,
});
foreach (Library library in newLibraries)
{
await _libraryManager.Create(library);
}
}
}
}

View File

@ -2,7 +2,7 @@ version: "3.8"
services:
back:
build:
build:
context: ./back
dockerfile: Dockerfile.dev
ports:
@ -16,6 +16,7 @@ services:
- DATABASE__CONFIGURATIONS__POSTGRES__PASSWORD=${POSTGRES_PASSWORD}
- TVDB__APIKEY=${TVDB__APIKEY}
- THEMOVIEDB__APIKEY=${THEMOVIEDB__APIKEY}
- LIBRARY_ROOT=/video
depends_on:
- postgres
volumes:
@ -24,7 +25,7 @@ services:
- kyoo:/var/lib/kyoo
- ./video:/video
front:
build:
build:
context: ./front
dockerfile: Dockerfile.dev
volumes:
@ -67,4 +68,3 @@ services:
volumes:
kyoo:
db:

View File

@ -13,6 +13,7 @@ services:
- DATABASE__CONFIGURATIONS__POSTGRES__DATABASE=${POSTGRES_DB}
- TVDB__APIKEY=${TVDB__APIKEY}
- THEMOVIEDB__APIKEY=${THEMOVIEDB__APIKEY}
- LIBRARY_ROOT=/video
depends_on:
- postgres
volumes:
@ -52,4 +53,3 @@ services:
volumes:
kyoo:
db: