From 3c5c58b3b0313a5ef2941d55b85863b8cf519ca3 Mon Sep 17 00:00:00 2001 From: CSiCoAlm Date: Wed, 5 Jan 2022 10:01:11 +0100 Subject: [PATCH] - Autocommit - 2022/01/05 10:01:11 --- Src/A69WD/A69WD.csproj | 8 ++ Src/A69WD/Net/ATLink.cs | 27 ++++- Src/A69WD/Net/ATLinkState.cs | 71 +++++++++++++ Src/A69WD/Net/DownloadClient.cs | 180 +++++++++++++++++++++++++++++++- Src/A69WD/Net/cA69Engine2.cs | 109 +++++++++++++++++++ 5 files changed, 393 insertions(+), 2 deletions(-) create mode 100644 Src/A69WD/Net/ATLinkState.cs create mode 100644 Src/A69WD/Net/cA69Engine2.cs diff --git a/Src/A69WD/A69WD.csproj b/Src/A69WD/A69WD.csproj index 2f3d3e8..db73f10 100644 --- a/Src/A69WD/A69WD.csproj +++ b/Src/A69WD/A69WD.csproj @@ -35,6 +35,10 @@ Tools.ico + + False + ..\..\..\..\_Library\HtmlAgilityPack 1.11.37\Net40\HtmlAgilityPack.dll + @@ -53,7 +57,11 @@ + + + + Form diff --git a/Src/A69WD/Net/ATLink.cs b/Src/A69WD/Net/ATLink.cs index a5efc9f..eb7015f 100644 --- a/Src/A69WD/Net/ATLink.cs +++ b/Src/A69WD/Net/ATLink.cs @@ -4,6 +4,31 @@ using System.Linq; using System.Text; namespace A69WD.Net { - class ATLink { + + + public class ATLink { + + public string URL { get { return _url; } } + private string _url; + + public string Dst { get { return _dst; } } + private string _dst; + + public string Testo { get; set; } + + public ATLinkState Status { get; set; } + + + public ATLink(string url, string fileName) { Initialize(url, fileName, ATLinkState.ToCheck); } + + private void Initialize(string url, string fileName, ATLinkState status) { + this._url = url; + this._dst = fileName; + this.Testo = ""; + this.Status = status; + } + + + } } diff --git a/Src/A69WD/Net/ATLinkState.cs b/Src/A69WD/Net/ATLinkState.cs new file mode 100644 index 0000000..05a2f13 --- /dev/null +++ b/Src/A69WD/Net/ATLinkState.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace A69WD.Net { + + /// + /// Enumerazione di tutti i bossibili stati di un link: + /// + public enum ATLinkState { + /// + /// Indica un link che deve venir testato per capire se è o meno da downloadare + /// + ToCheck = 1, + /// + /// Indica un link che deve venir downloadato + /// + ToDownload = 2, + /// + /// Indica un ATLink che è in download + /// + OnDownload = 3, + /// + /// Indica un ATLink correttamente downloadato + /// + Donwloaded = 4, + /// + /// Indica un link che deve venir downloadato + /// + ToAnalize = 5, + /// + /// Indica un ATLink che è in analizzazione + /// + OnAnalize = 6, + /// + /// Indica un ATLink correttamente downloadato + /// + Analized = 7, + /// + /// Indica un ATLink in cui si è concluso il lavoro. + /// + End = 8, + /// + /// Indica un ATLink il cui download ha avuto un errore + /// + ErrorDownload = 9, + /// + /// Indica un ATLink la cui analisi ha avuto un errore + /// + ErrorAnalize = 10, + /// + /// Indica un ATLink il cui download è stato abortito + /// + AbortDownload = 11, + /// + /// Indica un ATLink la cui analisi è stata abortita + /// + AbortAnalize = 12, + /// + /// Indica un ATLink il cui download è stato cancellato + /// + CancelDownload = 13, + /// + /// Indica un ATLink la cui analisi è stata cancellata + /// + CancelAnalize = 14, + } // enum ATLinkState + + +} diff --git a/Src/A69WD/Net/DownloadClient.cs b/Src/A69WD/Net/DownloadClient.cs index 59dbf0e..93211bb 100644 --- a/Src/A69WD/Net/DownloadClient.cs +++ b/Src/A69WD/Net/DownloadClient.cs @@ -3,7 +3,185 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.ComponentModel; +using System.Net; +using System.IO; + + namespace A69WD.Net { - class DownloadClient { + + public class DownloadClient { + + #region Fields: _backgroundWorker, Link + + private BackgroundWorker _backgroundWorker = null; + + public ATLink Link { get; set; } + + #endregion + + + #region Eventi: + + public event EventHandler Starting; + public event EventHandler Complete; + public event EventHandler Cancelled; + public event EventHandler Error; + public event EventHandler ProgressChanged; + + + #region Gestione emissione degli eventi per le classi che utilizzano il DownloadClient + + /// + /// Attivo evento Starting + /// + private void RaiseEvent_Starting() { + EventHandler tmp = Starting; + if (null != tmp) + tmp(this, new RunWorkerCompletedEventArgs(0F, null, false)); + } + + /// + /// Attivo evento Complete + /// + private void RaiseEvent_Complete() { + //if ((this.Link.Status != ATLinkState.ErrorDownload) && (this.Link.Status != ATLinkState.CancelDownload)) { + // this.Link.Status = ATLinkState.Donwloaded; + EventHandler tmp = Complete; + if (null != tmp) + tmp(this, new RunWorkerCompletedEventArgs(100F, null, false)); + //} + } + + /// + /// Attivo l'evento Cancelled + /// + private void RaiseEvent_Cancelled() { + //this.Link.Status = ATLinkState.CancelDownload; + Logger.Error(string.Format("Error Code: 00281 - Cancellazione del download del link: {0}", this.Link.ToString())); + var copy = Cancelled; + if (null != copy) copy(this, new RunWorkerCompletedEventArgs(0F, null, true)); + } + + /// + /// Attivo l'evento Error + /// + private void RaiseEvent_Error(Exception error) { + //this.Link.Status = ATLinkState.ErrorDownload; + Logger.Error(string.Format("Error Code: 00282 - Errore durante il download del link: {0}", this.Link.ToString())); + var copy = Error; + if (null != copy) copy(this, new RunWorkerCompletedEventArgs(-1F, error, false)); + } + + /// + /// Attivo l'evento ProgressChanged + /// + private void RaiseEvent_ProgressChanged(Int32 progress) { + EventHandler copy = ProgressChanged; + if (null != copy) + copy(this, new System.ComponentModel.ProgressChangedEventArgs(progress, this)); + } + + + #endregion + + #endregion + + public DownloadClient(ATLink link) { this.Link = link; } + + + /// + /// Avvia il download su un thread in background + /// + public void Start() { + //if (this.Link.Status == ATLinkState.ToDownload) { + // Preparo gli argomenti da passare al thread in background: + List arguments = new List(); + arguments.Add(this.Link); + // Creo il thread + _backgroundWorker = CreateWorker(arguments); + //} + } + + + + #region Gestione del thread in background: + + /// + /// Creo il thread in background e lo faccio partire. + /// + private BackgroundWorker CreateWorker(List Arguments) { + // Creo un background worker: + BackgroundWorker bg = new BackgroundWorker(); + bg.WorkerReportsProgress = true; + bg.WorkerSupportsCancellation = true; + // Assegno le risposte agli eventi del thread in background: + bg.ProgressChanged += new ProgressChangedEventHandler(BackgroundThread_ProgressChanged); + bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundThread_RunWorkerCompleted); + bg.DoWork += new DoWorkEventHandler(BackgroundThread_DoWork); + // Assegno gli argomenti previsti: + bg.RunWorkerAsync(Arguments); + return bg; + } + + + #region Gestione eventi del thread in background: + + /// + /// Codice eseguito al completamento del thread in background + /// + /// + /// + void BackgroundThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { + if (e.Error != null) { + RaiseEvent_Error(e.Error); + } else { + if (e.Cancelled) + RaiseEvent_Cancelled(); + else RaiseEvent_Complete(); + } + } + + /// + /// Codice eseguito ad ogni progresso nel thread in background + /// + void BackgroundThread_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) { + RaiseEvent_ProgressChanged(e.ProgressPercentage); + } + + /// + /// Codice eseguito dal trhead in background: + /// cioè esegue il dowload asincrono del file indicato da web: + /// + void BackgroundThread_DoWork(object sender, DoWorkEventArgs e) { + try { + RaiseEvent_Starting(); + + HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb(); + HtmlAgilityPack.HtmlDocument doc = web.Load(this.Link.URL); + this.Link.Testo = doc.Text; + RaiseEvent_ProgressChanged(50); + System.IO.StreamWriter sw = new System.IO.StreamWriter(this.Link.Dst); + sw.Write(doc.Text); + sw.Flush(); + sw.Close(); + RaiseEvent_Complete(); + } catch (Exception ex) { + Console.Write(ex.Message); + RaiseEvent_Error(ex); + } finally { + ; + } + } + + #endregion + + #endregion + + + + + + } } diff --git a/Src/A69WD/Net/cA69Engine2.cs b/Src/A69WD/Net/cA69Engine2.cs new file mode 100644 index 0000000..0864639 --- /dev/null +++ b/Src/A69WD/Net/cA69Engine2.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using A69WD.Net; + + +namespace A69WD { + + public class cA69Engine2 { + + #region Property: State, NumDownload, Tipologie, BASE_URL, BASE_LINK_IDX, BASE_LINK_RAC + + public EngineStatusEnum State { get { return state; } } + + public int NumDownload { get { return downloaded.Count; } } + + static string[] Tipologie = { "altro", "bdsm", "gay", "incesto", "lesbo", "orge", "prime", "scambio", "tradimenti", "trans", "trio" }; + + const string BASE_URL = "https://www.annunci69.it/racconti-erotici/{Tip}.html"; + const string BASE_LINK_IDX = "https://www.annunci69.it/racconti-erotici/racconti_lista.php"; + const string BASE_LINK_RAC = "https://www.annunci69.it/racconti-erotici/"; + + #endregion + + + #region Fields: allLinks, toDownload, downloaded, racLinks, dtStartTime, downRac, state, wb + + List allLinks = null; + List toDownload = null; + List downloaded = null; + List racLinks = null; + + DateTime dtStartTime; + + bool downRac = false; + + EngineStatusEnum state = EngineStatusEnum.NotReady; + + #endregion + + + #region Costruttore e distruttore + + public cA69Engine2() { + try { + // Inizializzo gli archivi (dai file in memoria) + allLinks = new List(); + toDownload = new List(); + downloaded = new List(); + racLinks = new List(); + int res = App.DB.LoadData(allLinks, toDownload, downloaded, racLinks); + RaiseEvent_Updating("Ricaricati " + res.ToString("000") + " link di racconti da scaricare"); + wb = WB; + wb.DocumentCompleted += Wb_DocumentCompleted; + // Per ora non serializzo gli archivi e quindi carico le pagine iniziali delle 11 tipologie: + foreach (string tipologia in Tipologie) { + toDownload.Add(BASE_URL.Replace("{Tip}", tipologia)); + allLinks.Add(BASE_URL.Replace("{Tip}", tipologia)); + } + } catch (Exception ex) { + Logger.Log(new object[] { "Error Code: ER02220", "Errore in cA69Engine()", ex }); + throw ex; + } + state = EngineStatusEnum.StandBy; + } + + #endregion + + + + + + + #region Eventi: + + /// + /// Evento che indica un cambiamento di stato nell'AnalizeManager + /// + public event EventHandler Updating; + + + #region Generazione degli eventi dell'engine verso le classi chiamanti: + + /// + /// Attiva l'evento che segnala un cambiamento di stato nel engine (per esempio, terminato il download di un singolo file) + /// + void RaiseEvent_Updating(string msg, int tot, int toDown, int down) { + EventHandler tmp = Updating; + if (null != tmp) tmp(this, new EngineEventArgs(msg, tot, toDown, down)); + } + + /// + /// Attiva l'evento che segnala un cambiamento di stato nel engine (per esempio, terminato il download di un singolo file) + /// + void RaiseEvent_Updating(string msg) { + EventHandler tmp = Updating; + if (null != tmp) tmp(this, new EngineEventArgs(msg, allLinks.Count, toDownload.Count, downloaded.Count)); + } + + #endregion + + + #endregion + + + } +}