using System.Collections.Generic;
using System.Configuration;
using System.Threading.Tasks;
using OzonTest.CommandLine;
using OzonTest.ImagePatternsSection;
private static readonly ICollection<string> _patterns = new List<string>();
private static ILogger _logger;
static async Task Main(string[] args)
Log.Logger = new LoggerConfiguration().ReadFrom.AppSettings().CreateLogger();
_logger = Log.ForContext<Program>();
_logger.Information("Work started.");
await Parser.Default.ParseArguments<Options>(args)
_logger.Error("An error occurred while parsing the command line parameters.");
.WithParsedAsync(ProcessCommandLineParametersAsync);
_logger.Error(ex, ex.ToString());
_logger.Information("Work finished.");
private static async Task ProcessCommandLineParametersAsync(Options opt)
_logger.Information($"Page source is {opt.Uri}.");
using (var container = ConfigureComponents(opt))
var manager = container.Resolve<IImageManager>();
await manager.LoadAsync(new Uri(opt.Uri));
await manager.SaveAsync(opt.CleanUp);
private static void ReadImagePatterns()
ImagesPatternsSection section = (ImagesPatternsSection)ConfigurationManager.GetSection("ImagesPatterns");
foreach (ImagePatternElement item in section.ImagesPatternItems)
_patterns.Add(item.PatternValue);
private static IContainer ConfigureComponents(Options options)
var builder = new ContainerBuilder();
builder.RegisterType<WebImageLoader>().As<IImageLoader>().
WithParameter("imagePatterns", _patterns.ToArray());
var saveToDb = options.SaveToDb ?? bool.Parse(ConfigurationManager.AppSettings["saveToDb"]);
builder.RegisterType<PostgresImageStorage>().As<IStorage>();
builder.RegisterType<FileSystemImageStorage>().As<IStorage>()
.WithParameter("targetFolder", GetTargetFolder(options));
builder.RegisterType<ImageManager>().As<IImageManager>();
private static string GetTargetFolder(Options options)
var generalTargetFolder = !string.IsNullOrEmpty(options.TargetFolder) ? options.TargetFolder :
ConfigurationManager.AppSettings["targetFolder"];
return Path.Combine(generalTargetFolder, new Uri(options.Uri).Host);
private static void ShowErrors(IEnumerable<Error> errors)
foreach (var error in errors)
_logger.Error(error.Tag.ToString());
public interface IImageLoader
ICollection<Image> Load(Uri dataSourcUri);
Task<ICollection<Image>> LoadAsync(Uri dataSourcUri);
public interface IStorage : IDisposable
void Save(IEnumerable<Image> images);
Task SaveAsync(IEnumerable<Image> images);
void CleanUp(string site);
public interface IImageManager
Task SaveAsync(bool cleanUp);