Skip to content

Commit

Permalink
Removed log4net and hardcoded list of extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
EricStG committed Dec 21, 2020
1 parent 1339e22 commit 6f0cae8
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 110 deletions.
25 changes: 16 additions & 9 deletions Listatateur/Crawler/Crawler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using log4net;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -7,17 +7,24 @@ namespace Crawler
{
public class Crawler
{
private static ILog logger = LogManager.GetLogger(typeof(Crawler));
public Crawler(ILogger<Crawler> logger)
{
_logger = logger;
}

private readonly ILogger<Crawler> _logger;

private static readonly HashSet<string> _extensions = new HashSet<string>(new[] { ".mp3", ".aac", ".ogg" }, StringComparer.OrdinalIgnoreCase); // TODO: make this configurable?

private static MediaFile ProcessFile(string filename)
private MediaFile ProcessFile(string filename)
{
try
{
if (Properties.Settings.Default.Extensions.Contains(Path.GetExtension(filename)))
if (_extensions.Contains(Path.GetExtension(filename)))
{
using (var file = TagLib.File.Create(filename, TagLib.ReadStyle.Average))
{
return new MediaFile()
return new MediaFile
{
Filename = filename,
Title = file.Tag.Title,
Expand All @@ -34,18 +41,18 @@ private static MediaFile ProcessFile(string filename)
}
else
{
logger.DebugFormat("File '{0}' does not match any of the configured extensions. Skipping.", filename);
_logger.LogDebug("File '{Filename}' does not match any of the configured extensions. Skipping.", filename);
return null;
}
}
catch (Exception ex)
{
logger.WarnFormat("Could not read tag for file '{0}': {1}", filename, ex);
_logger.LogWarning(ex, "Could not read tag for file '{Filename}'", filename);
return null;
}
}

private static IEnumerable<MediaFile> ProcessFolder(string folder, bool recursive)
private IEnumerable<MediaFile> ProcessFolder(string folder, bool recursive)
{
SearchOption options;
if (recursive)
Expand All @@ -66,7 +73,7 @@ private static IEnumerable<MediaFile> ProcessFolder(string folder, bool recursiv
}
}

public static IEnumerable<MediaFile> ProcessPaths(IEnumerable<string> paths, bool recursive)
public IEnumerable<MediaFile> ProcessPaths(IEnumerable<string> paths, bool recursive)
{
foreach(var path in paths)
{
Expand Down
17 changes: 1 addition & 16 deletions Listatateur/Crawler/Crawler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="log4net" Version="2.0.12" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="taglib-sharp-netstandard2.0" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
<Compile Update="Settings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<None Update="Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>

</Project>
38 changes: 0 additions & 38 deletions Listatateur/Crawler/Settings.Designer.cs

This file was deleted.

14 changes: 0 additions & 14 deletions Listatateur/Crawler/Settings.settings

This file was deleted.

28 changes: 0 additions & 28 deletions Listatateur/Crawler/app.config

This file was deleted.

9 changes: 9 additions & 0 deletions Listatateur/Listatateur-Console/Listatateur-Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<OutputType>Exe</OutputType>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishTrimmed>true</PublishTrimmed>
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Crawler\Crawler.csproj" />
</ItemGroup>
Expand Down
27 changes: 22 additions & 5 deletions Listatateur/Listatateur-Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;

Expand All @@ -10,17 +11,33 @@ class Program

static void Main(string[] args)
{
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.SetMinimumLevel(LogLevel.Trace)
.AddConsole();

});

var files = CrawlFiles(args, loggerFactory);
ExportFiles(files);
}

private static IEnumerable<Crawler.MediaFile> CrawlFiles(string[] args, ILoggerFactory loggerFactory)
{
var crawler = new Crawler.Crawler(loggerFactory.CreateLogger<Crawler.Crawler>());

IEnumerable<Crawler.MediaFile> files;
if (args.Length > 0)
{
files = Crawler.Crawler.ProcessPaths(args, true);
{
files = crawler.ProcessPaths(args, true);
}
else
{
files = Crawler.Crawler.ProcessPaths(new string[] { Environment.CurrentDirectory }, true);
files = crawler.ProcessPaths(new string[] { Environment.CurrentDirectory }, true);
}

ExportFiles(files);
return files;
}

static void ExportFiles(IEnumerable<Crawler.MediaFile> files)
Expand Down

0 comments on commit 6f0cae8

Please sign in to comment.