Skip to content
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

Skip subtitle directories #34

Merged
merged 3 commits into from
May 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 36 additions & 35 deletions TrailerDownloader/SignalRHubs/MovieHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,24 @@ public async Task GetAllMoviesInfo()

foreach (string movieDirectory in _movieDirectories)
{
Movie movie = GetMovieFromDirectory(movieDirectory);
if (movie == null)
foreach (string movieDirectory1 in Directory.GetDirectories(movieDirectory))
{
_logger.LogInformation($"No movie found in directory: '{movieDirectory}'");
continue;
}
Movie movie = GetMovieFromDirectory(movieDirectory1);
if (movie == null)
{
_logger.LogInformation($"No movie found in directory: '{movieDirectory1}'");
continue;
}

if (_movieDictionary.TryGetValue(movie.Title, out Movie dictionaryMovie))
{
dictionaryMovie.TrailerExists = movie.TrailerExists;
await _hubContext.Clients.All.SendAsync("getAllMoviesInfo", dictionaryMovie).ConfigureAwait(false);
}
else
{
taskList.Add(GetMovieInfoAsync(movie));
if (_movieDictionary.TryGetValue(movie.Title, out Movie dictionaryMovie))
{
dictionaryMovie.TrailerExists = movie.TrailerExists;
await _hubContext.Clients.All.SendAsync("getAllMoviesInfo", dictionaryMovie).ConfigureAwait(false);
}
else
{
taskList.Add(GetMovieInfoAsync(movie));
}
}
}

Expand All @@ -93,17 +96,25 @@ private void GetMovieDirectories(string directoryPath)
try
{
_movieDirectories.Clear();

var movieFiles = Directory.EnumerateFiles(directoryPath, "*", SearchOption.AllDirectories)
.Where(file => (File.GetAttributes(file) & FileAttributes.Hidden) == 0);

foreach (string movieFile in movieFiles)
// Enumerate all subdirectories
var subDirectories = Directory.EnumerateDirectories(directoryPath, "*", SearchOption.AllDirectories);

// Add the movie directories to the collection
var hasSubdirectories = false;
foreach (var subDirectory in subDirectories)
{
string movieDir = Path.GetDirectoryName(movieFile);
if (!_movieDirectories.Contains(movieDir))
{
_movieDirectories.Add(movieDir);
}
if (Directory.GetDirectories(subDirectory).Length <= 0) continue;
if (!subDirectory.Contains("Subs")) continue;
if (!subDirectory.Contains("Subtitles")) continue;
_movieDirectories.Add(subDirectory);
hasSubdirectories = true;
}

// If no subdirectories were added, add the main directoryPath
if (!hasSubdirectories)
{
_movieDirectories.Add(directoryPath);
}
}
catch (Exception ex)
Expand All @@ -112,6 +123,7 @@ private void GetMovieDirectories(string directoryPath)
}
}


private Movie GetMovieFromDirectory(string movieDirectory)
{
if (Directory.GetFiles(movieDirectory).Length == 0)
Expand All @@ -120,18 +132,7 @@ private Movie GetMovieFromDirectory(string movieDirectory)
}

bool trailerExists = Directory.GetFiles(movieDirectory).Where(name => name.Contains("-trailer")).Count() > 0;

DirectoryInfo directory = new DirectoryInfo(movieDirectory);
FileInfo[] files = directory.GetFiles();

var filteredFiles = files.Where(f => !_excludedFileExtensions.Any(f.Name.EndsWith)
&& !f.Name.Contains("-trailer")
&& !f.Attributes.HasFlag(FileAttributes.Hidden));

string filePath = filteredFiles.FirstOrDefault()?.FullName;


// string filePath = Directory.GetFiles(movieDirectory).FirstOrDefault(file => !_excludedFileExtensions.Any(file.EndsWith) && !file.Contains("-trailer"));
string filePath = Directory.GetFiles(movieDirectory).FirstOrDefault(file => !_excludedFileExtensions.Any(x => file.EndsWith(x)) && !file.Contains("-trailer"));
string title = Regex.Replace(Path.GetFileNameWithoutExtension(filePath), @"\(.*", string.Empty).Trim().Replace("-trailer", string.Empty);
string year = Regex.Match(Path.GetFileNameWithoutExtension(filePath), @"\(\d*").Captures.FirstOrDefault()?.Value.Replace("(", string.Empty);

Expand Down Expand Up @@ -280,4 +281,4 @@ private async Task<string> GetTrailerURL(int? id)

return string.Empty;
}
}
}