public List <Page> GetPages(string location) { CoreEventHub.OnProcessingStart(this, null); CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, "Buscando Páginas")); var pages = new List <Page>(); if (!string.IsNullOrEmpty(location)) { var dir = new DirectoryInfo(location); var images = dir.GetFiles(); foreach (var image in images) { var resultString = Regex.Match(image.Name, @"\d+").Value; var number = Int32.Parse(resultString); CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Adicionando página {number}")); var pg = new Page { Number = number, Source = image.FullName }; pages.Add(pg); } } CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, "Tudo Pronto!")); CoreEventHub.OnProcessingEnd(this, null); return(pages); }
public T CacheManagement <T>(string url, Func <String, T> method, double timeCache) { lock (_lockThis2) { var model = default(T); if (!string.IsNullOrEmpty(url)) { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Buscando em DB")); if (_libraryContext.Cache.Find().Where(x => x.Link == url).Execute().FirstOrDefault() is Cache cache) { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Encontrado!")); if (!InternetChecker.IsConnectedToInternet()) { model = cache.ModelsCache.ToObject <T>(); } else { if ((DateTime.Now - cache.Date).TotalMinutes < timeCache) { model = cache.ModelsCache.ToObject <T>(); } else { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Cache Vencido")); CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Atualizando...")); model = method.Invoke(url); if (model != null) { _libraryContext.Cache.Update(x => new { x.ModelsCache, x.Date }, model.ToBytes(), DateTime.Now) .Where(x => x.Link == url).Execute(); } else { model = cache.ModelsCache.ToObject <T>(); } } } } else { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Não encontrado!")); if (InternetChecker.IsConnectedToInternet()) { model = method.Invoke(url); if (model != null) { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Criando Cache")); var updt = new Cache { Link = url, Date = DateTime.Now, ModelsCache = model.ToBytes() }; _libraryContext.Cache.Save(updt); } } } } return(model); } }
public List <HqDownloadInfo> GetHqsDownloadInfo() { lock (_lock3) { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Processando arquivos...")); var list = new List <HqDownloadInfo>(); foreach (var loc in DownloadConfiguration.DownloadLocations) { var drInfo = new DirectoryInfo(loc); if (drInfo != null) { var dirs = drInfo.GetDirectories(); if (dirs != null && dirs.Count() > 0) { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"{dirs.Count()} diretoórios encontrados")); foreach (var dir in dirs) { var downloadInfo = new HqDownloadInfo(); CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Buscando informações de {dir.Name}")); if (File.Exists($"{dir.FullName}\\DownloadInfo.Json")) { using (StreamReader file = File.OpenText($"{dir.FullName}\\DownloadInfo.Json")) { JsonSerializer serializer = new JsonSerializer(); downloadInfo = (HqDownloadInfo)serializer.Deserialize(file, typeof(HqDownloadInfo)); } if (!string.IsNullOrEmpty(downloadInfo.Hq.Link)) { if (_downloadContext.Hq.Find().Where(x => x.Link == downloadInfo.Hq.Link).Execute().FirstOrDefault() is Hq hq) { downloadInfo.Hq = hq; _downloadContext.HqDownloadInfo.SaveOrReplace(downloadInfo); } } } else { var hq = new Hq { Title = dir.Name }; downloadInfo = new HqDownloadInfo { Hq = hq, Path = dir.FullName }; } if (downloadInfo != null && downloadInfo.Hq != null) { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"{downloadInfo.Hq.Title} adicionado!")); list.Add(downloadInfo); } } } } } return(list); } }
public HqDownloadInfo GetChaptersFromDownloadInfo(HqDownloadInfo downloadInfo) { lock (_lock4) { downloadInfo.Hq.Chapters = new List <Chapter>(); var subDirInfo = new DirectoryInfo(downloadInfo.Path); if (subDirInfo != null) { var subDirs = subDirInfo.GetDirectories(); var index = 1; if (subDirs != null && subDirs.Count() > 0) { foreach (var chapDir in subDirs) { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Adiconando capitulo {chapDir.Name}...")); var count = 0; var chapName = Regex.Replace(chapDir.Name, @"\d+", n => { var value = n.Value; var replacement = value.PadLeft(4, '0'); count++; if (count <= 1) { return(replacement); } return(value); }); var chap = new Chapter { Id = index, Title = chapName, Pages = new List <Page>() }; var pageFiles = chapDir.GetFiles(); if (pageFiles != null && pageFiles.Count() > 0) { foreach (var file in pageFiles) { var num = int.Parse(Path.GetFileNameWithoutExtension(file.Name)); CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Adiconando página {num}...")); chap.Pages.Add(new Page { Id = num, Number = num, Source = file.FullName }); } } index++; downloadInfo.Hq.Chapters.Add(chap); } downloadInfo.Hq.Chapters = downloadInfo.Hq.Chapters.OrderBy(x => x.Title).ToList(); downloadInfo.LastDownloadedChapter = downloadInfo.Hq.Chapters.Last(); } } return(downloadInfo); } }
public List <DownloadedHq> GetDownloads() { //_downloadContext.DownloadLocation.Save(new DownloadLocation { Location = "D:\\Mangas" }); CoreEventHub.OnProcessingStart(this, null); var list = new List <DownloadedHq>(); CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, "Buscando Locais")); var locais = _downloadContext.DownloadLocation.FindAll(); if (locais != null && locais.Count() > 0) { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, "Buscando Hqs")); foreach (var local in locais) { var directory = new DirectoryInfo(local.Location); var mangas = directory.GetDirectories(); foreach (var manga in mangas) { DownloadedHq downloadedHq = new DownloadedHq(); var json = manga.GetFiles("*.json").FirstOrDefault(); if (json != null) { using (StreamReader file = json.OpenText()) { JsonSerializer serializer = new JsonSerializer(); downloadedHq = serializer.Deserialize(file, typeof(DownloadedHq)) as DownloadedHq; } } if (downloadedHq != null) { list.Add(downloadedHq); } } } } CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, "Tudo Pronto!")); CoreEventHub.OnProcessingEnd(this, null); return(list); }
public List <DownloadedChapter> GetChapters(DownloadedHq downloadedHq) { CoreEventHub.OnProcessingStart(this, null); var chapters = new List <DownloadedChapter>(); var dir = new DirectoryInfo(downloadedHq.Location); var paths = dir.GetDirectories(); CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Buscando Capitulos de {downloadedHq.Hq.Title}")); foreach (var path in paths) { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Adicioandn capitulo {path.Name}")); var title = path.Name; var fisrt = false; title = Regex.Replace(title, @"\b(\d+)", new MatchEvaluator((match) => { var m = match.Value; if (!fisrt) { m = m.PadLeft(3, '0'); fisrt = true; } return(m); })); var chap = new Chapter { Title = title }; var downlodeadChapter = new DownloadedChapter { Date = path.CreationTime, Location = path.FullName, Chapter = chap }; chapters.Add(downlodeadChapter); } chapters = chapters.OrderBy(x => x.Chapter.Title).ToList <DownloadedChapter>(); CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, "Tudo Pronto!")); CoreEventHub.OnProcessingEnd(this, null); return(chapters); }
public T ModelCache <T>(string url, Func <String, T> method, double timeCache, bool withoutCache) where T : ModelBase { lock (_lockThis) { var model = default(T); if (!string.IsNullOrEmpty(url)) { if (typeof(T).IsAssignableFrom(typeof(Hq))) { if (withoutCache) { model = SaveHqInfo <T>(url, method); } else { if (_libraryContext.Hq.Find().Where(x => x.Link == url).Execute().FirstOrDefault() is Hq hqModel) { hqModel.Chapters = _libraryContext.Chapter.Find().Where(x => x.Hq == hqModel).Execute(); model = hqModel as T; if (InternetChecker.IsConnectedToInternet()) { if (!hqModel.IsDetailedInformation) { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Atualizando Informações")); model = SaveHqInfo <T>(url, method); } else { if ((DateTime.Now - hqModel.TimeInCache).TotalMinutes > timeCache) { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Cache Vencido")); CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Atualizando...")); model = SaveHqInfo <T>(url, method); } } } } else { model = SaveHqInfo <T>(url, method); } } } if (typeof(T).IsAssignableFrom(typeof(Chapter))) { if (withoutCache) { model = method.Invoke(url) as T; } else { if (_libraryContext.Chapter.Find().Where(x => x.Link == url).Execute().FirstOrDefault() is Chapter chapterModel) { chapterModel.Pages = _libraryContext.Page.Find().Where(x => x.Chapter == chapterModel).Execute(); if (chapterModel.Pages == null || chapterModel.Pages.Count == 0) { if (method.Invoke(url) is Chapter chapterFromSite) { chapterModel.Pages = chapterFromSite.Pages; SavePagesInDb(chapterModel); } } model = chapterModel as T; } else { model = method.Invoke(url) as T; SavePagesInDb(model as Chapter); } } } } return(model); } }
public List <Update> UpdatesCache(string url, Func <String, List <Update> > method, double timeCache) { lock (_lockThis6) { var updates = new List <Update>(); CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Buscando em DB")); var time = DateTime.Now; if (_libraryContext.Update.Find().Where(x => x.Source == url && x.TimeCache > time).Execute() is List <Update> updt && updt.Count > 0) { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Encontrado!")); foreach (var up in updt) { var hq = up.Hq; if (hq != null) { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Adicionando {up.Hq.Title}")); up.Chapters = _libraryContext.Chapter.Find().Where(x => x.Hq == hq && x.IsUpdate == true && x.Date > time).Execute(); updates.Add(up); } } } else { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Não encontrado!")); _libraryContext.Chapter.Update(x => x.IsUpdate, false).Where(x => x.IsUpdate == true && x.Date < time).Execute(); if (InternetChecker.IsConnectedToInternet()) { updates = method.Invoke(url); if (updates != null) { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Criando Cache")); foreach (var upd in updates) { upd.Source = url; upd.TimeCache = DateTime.Now.AddMinutes(timeCache); var link = upd.Hq.Link; if (_libraryContext.Hq.Find().Where(x => x.Link == link).Execute().FirstOrDefault() is Hq hq) { upd.Hq = hq; foreach (var chap in upd.Chapters) { chap.Hq = upd.Hq; chap.IsUpdate = true; chap.Date = DateTime.Now.AddDays(3); if (_libraryContext.Chapter.Find(x => x.Id).Where(x => x.Link == chap.Link).Execute().FirstOrDefault() is Chapter chapDb) { chap.Id = chapDb.Id; } else { chap.Id = Convert.ToInt32(_libraryContext.Chapter.Save(chap)); } } } else { CoreEventHub.OnProcessingProgress(this, new ProcessingEventArgs(DateTime.Now, $"Criando Cache para {upd.Hq.Title}")); upd.Hq.CoverSource = _coveCacheHelper.CreateCache(upd.Hq); upd.Hq.Id = Convert.ToInt32(_libraryContext.Hq.Save(upd.Hq)); foreach (var chap in upd.Chapters) { chap.Hq = upd.Hq; chap.IsUpdate = true; chap.Date = DateTime.Now.AddDays(3); if (_libraryContext.Chapter.Find(x => x.Id).Where(x => x.Link == chap.Link).Execute().FirstOrDefault() is Chapter chapDb) { chap.Id = chapDb.Id; } else { chap.Id = Convert.ToInt32(_libraryContext.Chapter.Save(chap)); } } } _libraryContext.Update.Save(upd); } } } } GC.Collect(); GC.WaitForPendingFinalizers(); return(updates); } }
protected void OnProcessingProgress(ProcessingEventArgs e) => CoreEventHub.OnProcessingProgress(this, e);