From fd2da1a3afff8491f84e98664730ff79ac928d74 Mon Sep 17 00:00:00 2001 From: CSiCoAlm Date: Wed, 27 Oct 2021 13:01:11 +0200 Subject: [PATCH] - Autocommit - 2021/10/27 13:01:11 --- Src/A69WD/Data/DBTools.cs | 98 ++++++++++++++++++ Src/A69WD/Data/eLinkState.cs | 4 +- Src/A69WD/Net/cA69Engine.cs | 130 +++++++++++++++++++----- Src/A69WD/Net/frmDownloader.Designer.cs | 28 ----- Src/A69WD/Net/frmDownloader.cs | 25 +++++ 5 files changed, 229 insertions(+), 56 deletions(-) diff --git a/Src/A69WD/Data/DBTools.cs b/Src/A69WD/Data/DBTools.cs index 56c46c9..8bbea22 100644 --- a/Src/A69WD/Data/DBTools.cs +++ b/Src/A69WD/Data/DBTools.cs @@ -140,6 +140,104 @@ namespace A69WD { #endregion + + public int LoadData(List allLinks, List toDownload, List downloaded, List racLinks) { + object obj = new Object(); + lock (obj) { + int Res = 0; + try { + + System.Data.SQLite.SQLiteCommand cmd = new SQLiteCommand("SELECT [URL], [Tipo], [Status], [LastUp] FROM [LinksLog] WHERE [Tipo]='Rac' AND [Status]<>4", cn); // Donwloaded = 4 + SQLiteDataReader rdr = cmd.ExecuteReader(); + while (rdr.Read()) { + string url = rdr["URL"] as string; + allLinks.Add(url); + toDownload.Add(url); + racLinks.Add(url); + Res++; + } + } catch (Exception ex) { + Logger.Log(new object[] { "Error Code: DB07003", "Errore in Data.DBTools.LoadData()", ex }); + if (Res > 0) Res = -1 * Res; + } + return Res; + } + } + + public int SaveData(List allLinks, List toDownload, List downloaded, List racLinks) { + object obj = new Object(); + lock (obj) { + int Res = 0; + try { + foreach (string link in racLinks) { LinkLogUpdtate(link, cA69Engine.LinkType(link), eLinkState.ToDownload); } + foreach (string link in toDownload) { LinkLogUpdtate(link, cA69Engine.LinkType(link), eLinkState.ToDownload); } + foreach (string link in downloaded) { LinkLogUpdtate(link, cA69Engine.LinkType(link), eLinkState.Donwloaded); } + } catch (Exception ex) { + Logger.Log(new object[] { "Error Code: DB07003", "Errore in Data.DBTools.LoadData()", ex }); + if (Res > 0) Res = -1 * Res; + } + return Res; + } + } + private bool LinkLogUpdtate(string url, string tip, eLinkState st) { + // object obj = new Object(); + // lock (obj) { + // Cancello riga (se non presente, non fa nulla) + // Inserisco riga + bool Res = false; + try { + // Cancello il link anche se non presente + System.Data.SQLite.SQLiteCommand cmd = new SQLiteCommand(string.Format("DELETE FROM [LinksLog] WHERE [URL]='{0}'", url), cn); + int res = cmd.ExecuteNonQuery(); + cmd = null; + // Inserisco il link come se fosse nuovo + cmd = new System.Data.SQLite.SQLiteCommand(); + cmd.Connection = cn; + cmd.CommandText = "INSERT INTO LinksLog (URL,Tipo,Status,LastUp)VALUES (@URL,@Tipo,@Status,@LastUp)"; + cmd.CommandType = System.Data.CommandType.Text; + System.Data.SQLite.SQLiteParameter param = new System.Data.SQLite.SQLiteParameter(); + param.ParameterName = "@URL"; + param.DbType = global::System.Data.DbType.String; + param.SourceColumn = "URL"; + cmd.Parameters.Add(param); + param = new System.Data.SQLite.SQLiteParameter(); + param.ParameterName = "@Tipo"; + param.DbType = global::System.Data.DbType.String; + param.SourceColumn = "Tipo"; + cmd.Parameters.Add(param); + param = new System.Data.SQLite.SQLiteParameter(); + param.ParameterName = "@Status"; + param.DbType = global::System.Data.DbType.Int32; + param.SourceColumn = "Status"; + cmd.Parameters.Add(param); + param = new System.Data.SQLite.SQLiteParameter(); + param.ParameterName = "@LastUp"; + param.DbType = global::System.Data.DbType.DateTime; + param.SourceColumn = "LastUp"; + cmd.Parameters.Add(param); + cmd.Parameters[0].Value = url; + cmd.Parameters[1].Value = tip; + cmd.Parameters[2].Value = st; + //if (InsertDate) { cmd.Parameters[3].Value = DateTime.Now; } else { cmd.Parameters[3].Value = DateTime.MaxValue; } + cmd.Parameters[3].Value = DateTime.Now; + res = cmd.ExecuteNonQuery(); + if (res != 1) { + Logger.Log(new object[] { "Error Code: DB06010", string.Format("DBTools.LinkLogUpdtate({0})={1}", url, res) }); + Res = false; + } else { + Res = true; + } + } catch (Exception ex) { + Logger.Log(new object[] { "Error Code: DB06001", string.Format("DBTools.LinkLogUpdtate({0})", url), ex }); + return false; + } finally { + //if ((cn != null) && (cn.State != System.Data.ConnectionState.Closed)) cn.Close(); + } + return Res; + // } // lock (obj) + } + + ///// ///// Ricava dal DB tutti i dati ///// diff --git a/Src/A69WD/Data/eLinkState.cs b/Src/A69WD/Data/eLinkState.cs index dfe2006..c2ce43a 100644 --- a/Src/A69WD/Data/eLinkState.cs +++ b/Src/A69WD/Data/eLinkState.cs @@ -8,7 +8,7 @@ namespace A69WD { /// /// Enumerazione di tutti i bossibili stati di un link: /// - public enum ATLinkState { + public enum eLinkState { /// /// Indica un link che deve venir testato per capire se è o meno da downloadare /// @@ -65,6 +65,6 @@ namespace A69WD { ///// Indica un ATLink la cui analisi è stata cancellata ///// //CancelAnalize = 14, - } // enum ATLinkState + } // enum eLinkState }//namespace A69WD diff --git a/Src/A69WD/Net/cA69Engine.cs b/Src/A69WD/Net/cA69Engine.cs index c220cd0..0756e94 100644 --- a/Src/A69WD/Net/cA69Engine.cs +++ b/Src/A69WD/Net/cA69Engine.cs @@ -11,13 +11,13 @@ namespace A69WD { public class cA69Engine { - #region Property: State, Tipologie, BASE_URL, BASE_LINK_IDX, BASE_LINK_RAC + #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; } } - string[] Tipologie = { "altro", "bdsm", "gay", "incesto", "lesbo", "orge", "prime", "scambio", "tradimenti", "trans", "trio" }; + 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"; @@ -26,7 +26,7 @@ namespace A69WD { #endregion - #region Fields: allLinks, toDownload, downloaded, racLinks, dtStartTime, aborting, downRac, state, wb + #region Fields: allLinks, toDownload, downloaded, racLinks, dtStartTime, downRac, state, wb List allLinks = null; List toDownload = null; @@ -35,7 +35,6 @@ namespace A69WD { DateTime dtStartTime; - bool aborting = false; bool downRac = false; EngineStatusEnum state = EngineStatusEnum.NotReady; @@ -48,17 +47,24 @@ namespace A69WD { #region Costruttore e distruttore public cA69Engine(WebBrowser WB) { - // Inizializzo gli archivi (dai file in memoria) - allLinks = new List(); - toDownload = new List(); - downloaded = new List(); - racLinks = new List(); - 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)); + 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; } @@ -74,7 +80,7 @@ namespace A69WD { dtStartTime = DateTime.Now; downRac = DownRac; - UpdateST("Inizio lo scrap dei link dei racconti"); + RaiseEvent_Updating("Inizio lo scrap dei link dei racconti"); state = EngineStatusEnum.Starting; @@ -89,12 +95,45 @@ namespace A69WD { } public void Stop() { - + state = EngineStatusEnum.Stopping; } #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 + + + private void Wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { object obj = new Object(); @@ -107,7 +146,7 @@ namespace A69WD { string link = wb.Url.ToString(); // aggiorno UI - UpdateST(wb.Name + " - Donwloaded: " + link); + RaiseEvent_Updating(wb.Name + " - Donwloaded: " + link); // Se pagina è un racconto, lo salvo if (IsRacLink(link)) { @@ -162,6 +201,8 @@ namespace A69WD { private void SaveState() { + int res = App.DB.SaveData(allLinks, toDownload, downloaded, racLinks); + StringBuilder sb; // Finito download, salvo gli archivi: @@ -192,22 +233,23 @@ namespace A69WD { cTools.WriteTextFile(Path.Combine(Cfg.Settings.PathAnalizadedIndexFile, string.Format("_A69_RacLinks_{0}.tes", dt)), sb); // avviso utente: - UpdateST(string.Format("Finito il download in {0}", DateTime.Now - dtStartTime)); + RaiseEvent_Updating(string.Format("Finito il download in {0}", DateTime.Now - dtStartTime)); state = EngineStatusEnum.Stopped; } - - - private void UpdateST(string Msg) { - //lblST.Text = string.Format("{0} - {1} - {2}", DateTime.Now.ToString("HH:mm:ss"), cnt.ToString("0000"), Msg); - } private string GetFileName(string url) { return url.Replace("https://www.annunci69.it/racconti-erotici/", "A69_").Replace("/", "_").Replace(":", "_").Replace(@"\", "_"); } - private bool IsIdxLink(string link) { return link.StartsWith(BASE_LINK_IDX); } - private bool IsRacLink(string link) { foreach (string tip in Tipologie) { if (link.StartsWith(BASE_LINK_RAC + tip + "/")) return true; } return false; } + static private bool IsIdxLink(string link) { return link.StartsWith(BASE_LINK_IDX); } + static private bool IsRacLink(string link) { foreach (string tip in Tipologie) { if (link.StartsWith(BASE_LINK_RAC + tip + "/")) return true; } return false; } + static public string LinkType(string link) { + if (IsIdxLink(link)) return "Idx"; + if (IsRacLink(link)) return "Rac"; + //return "Unknown"; + return "Unk"; + } }//class cA69Engine @@ -230,4 +272,40 @@ namespace A69WD { Stopped = 5, } // enum EngineStatusEnum + + public class EngineEventArgs : EventArgs { + + + #region Property and Fields: + + + /// + /// Numero di file da analizzare, in totale + /// + public int NTot { get { return nTot; } } + private int nTot; + + public int NtoDown { get { return ntoDown; } } + private int ntoDown; + + public int NDown { get { return nDown; } } + private int nDown; + + public string Message { get { return message; } } + private string message; + + #endregion + + + public EngineEventArgs(string Message, int NTot, int NtoDown, int NDown) { + this.nTot = NTot; + this.ntoDown = NtoDown; + this.nDown = NDown; + this.message = Message; + } + + } // class EngineEventArgs + + + }//namespace A69WD diff --git a/Src/A69WD/Net/frmDownloader.Designer.cs b/Src/A69WD/Net/frmDownloader.Designer.cs index c908dcd..02a622d 100644 --- a/Src/A69WD/Net/frmDownloader.Designer.cs +++ b/Src/A69WD/Net/frmDownloader.Designer.cs @@ -23,8 +23,6 @@ /// the contents of this method with the code editor. /// private void InitializeComponent() { - this.btbPath = new System.Windows.Forms.Button(); - this.tbPath = new System.Windows.Forms.TextBox(); this.btnClose = new System.Windows.Forms.Button(); this.btnHide = new System.Windows.Forms.Button(); this.btnDownloadAll = new System.Windows.Forms.Button(); @@ -33,26 +31,6 @@ this.btnDownloadLink = new System.Windows.Forms.Button(); this.SuspendLayout(); // - // btbPath - // - this.btbPath.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.btbPath.Location = new System.Drawing.Point(-39, 53); - this.btbPath.Name = "btbPath"; - this.btbPath.Size = new System.Drawing.Size(20, 20); - this.btbPath.TabIndex = 17; - this.btbPath.Text = "."; - this.btbPath.UseVisualStyleBackColor = true; - // - // tbPath - // - this.tbPath.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.tbPath.Location = new System.Drawing.Point(132, 54); - this.tbPath.Name = "tbPath"; - this.tbPath.Size = new System.Drawing.Size(0, 20); - this.tbPath.TabIndex = 16; - this.tbPath.Text = "C:\\Temporary\\A69WD\\Dst\\"; - // // btnClose // this.btnClose.Location = new System.Drawing.Point(381, 12); @@ -121,8 +99,6 @@ this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(504, 74); - this.Controls.Add(this.btbPath); - this.Controls.Add(this.tbPath); this.Controls.Add(this.btnClose); this.Controls.Add(this.btnHide); this.Controls.Add(this.btnDownloadAll); @@ -133,14 +109,10 @@ this.Name = "frmDownloader"; this.Text = "frmDownloader"; this.ResumeLayout(false); - this.PerformLayout(); } #endregion - - private System.Windows.Forms.Button btbPath; - private System.Windows.Forms.TextBox tbPath; private System.Windows.Forms.Button btnClose; private System.Windows.Forms.Button btnHide; private System.Windows.Forms.Button btnDownloadAll; diff --git a/Src/A69WD/Net/frmDownloader.cs b/Src/A69WD/Net/frmDownloader.cs index f3b8d7c..9756da2 100644 --- a/Src/A69WD/Net/frmDownloader.cs +++ b/Src/A69WD/Net/frmDownloader.cs @@ -15,6 +15,7 @@ namespace A69WD { public frmDownloader() { InitializeComponent(); engine = new cA69Engine(this.wb1); + engine.Updating += Engine_Updating; } cA69Engine engine = null; @@ -40,6 +41,30 @@ namespace A69WD { } } + #region Gestione eventi dal engine + + private void Engine_Updating(object sender, EngineEventArgs e) { EngineUpdatingStatus(e.Message, e.NTot, e.NtoDown, e.NDown); } + + delegate void EngineUpdatingStatusCallback(string Message, int NTot, int NtoDown, int NDown); + private void EngineUpdatingStatus(string Message, int NTot, int NtoDown, int NDown) { + try { + // InvokeRequired required compares the thread ID of the + // calling thread to the thread ID of the creating thread. + // If these threads are different, it returns true. + if (lblST.InvokeRequired) { + EngineUpdatingStatusCallback callBack = new EngineUpdatingStatusCallback(EngineUpdatingStatus); + this.Invoke(callBack, new object[] { Message, NTot, NtoDown, NDown }); + } else { + lblST.Text = string.Format("{4} - {3} | {2} | {1} - {0}", Message, NTot, NtoDown, NDown, DateTime.Now.ToString("HH:mm:ss")); + + } + } catch (Exception ex) { + Logger.Log(new object[] { "Error Code: ER05553", "Errore in EngineUpdatingStatus():", ex }); + } + } + + #endregion + }//class frmDownloader