From ae8d3b0dcecc2f93f00508e8350326cf76b5a4f2 Mon Sep 17 00:00:00 2001 From: CSiCoAlm Date: Wed, 27 Oct 2021 11:01:12 +0200 Subject: [PATCH] - Autocommit - 2021/10/27 11:01:11 --- Src/A69WD/A69WD.csproj | 1 + Src/A69WD/Data/DBTools.cs | 68 +++---- Src/A69WD/General/Cfg.cs | 4 +- Src/A69WD/Net/cA69Engine.cs | 233 ++++++++++++++++++++++++ Src/A69WD/Net/frmDownloader.Designer.cs | 4 + Src/A69WD/Net/frmDownloader.cs | 34 +++- Src/A69WD/Settings.cfg | 5 +- 7 files changed, 309 insertions(+), 40 deletions(-) create mode 100644 Src/A69WD/Net/cA69Engine.cs diff --git a/Src/A69WD/A69WD.csproj b/Src/A69WD/A69WD.csproj index 9acc1eb..dbf1889 100644 --- a/Src/A69WD/A69WD.csproj +++ b/Src/A69WD/A69WD.csproj @@ -50,6 +50,7 @@ + Form diff --git a/Src/A69WD/Data/DBTools.cs b/Src/A69WD/Data/DBTools.cs index 3cdcc5c..56c46c9 100644 --- a/Src/A69WD/Data/DBTools.cs +++ b/Src/A69WD/Data/DBTools.cs @@ -140,43 +140,43 @@ namespace A69WD { #endregion - /// - /// Ricava dal DB tutti i dati - /// - /// Numero di link inseriti nell'engine. Un numero negativo se si è avuto un errore. - public long RetrieveLinks() { - // MRK: TODO: - // Può essere utile amplificare questa funzione in modo che si possa specificare quali link reinserire: - // Ad esempio dei filtri tipo: - // - solo link con Status pari a ErrorDownload o CancelDownload - // - solo link con Status pari a OnDownload - // - ecc... + ///// + ///// Ricava dal DB tutti i dati + ///// + ///// Numero di link inseriti nell'engine. Un numero negativo se si è avuto un errore. + //public long RetrieveLinks() { + // // MRK: TODO: + // // Può essere utile amplificare questa funzione in modo che si possa specificare quali link reinserire: + // // Ad esempio dei filtri tipo: + // // - solo link con Status pari a ErrorDownload o CancelDownload + // // - solo link con Status pari a OnDownload + // // - ecc... - //return 0; // MRK: BYPASSATO TUTTO PER EVITARE CHE ALL'INIZIO CARICHI I LINK IN SOSPESO. + // //return 0; // MRK: BYPASSATO TUTTO PER EVITARE CHE ALL'INIZIO CARICHI I LINK IN SOSPESO. - object obj = new Object(); - lock (obj) { - long Res = 0; - System.Data.DataTable table = new System.Data.DataTable("LinksLog"); - try { - System.Data.SQLite.SQLiteCommand cmd = new SQLiteCommand("SELECT [URL], [Tipo], [Status], [LastUp] FROM [LinksLog] WHERE [Tipo]='Testo' AND [Status]<>4", cn); // Donwloaded = 4 - SQLiteDataReader rdr = cmd.ExecuteReader(); - while (rdr.Read()) { - string url = rdr["URL"] as string; - ATLinkState status = ATLinkState.ToDownload; - try { status = ((ATLinkState)((long)rdr["Status"])); } catch (Exception ex) { throw new Exception("ERRORE AFFABULANTE \n" + ex.Message); } + // object obj = new Object(); + // lock (obj) { + // long Res = 0; + // System.Data.DataTable table = new System.Data.DataTable("LinksLog"); + // try { + // System.Data.SQLite.SQLiteCommand cmd = new SQLiteCommand("SELECT [URL], [Tipo], [Status], [LastUp] FROM [LinksLog] WHERE [Tipo]='Testo' AND [Status]<>4", cn); // Donwloaded = 4 + // SQLiteDataReader rdr = cmd.ExecuteReader(); + // while (rdr.Read()) { + // string url = rdr["URL"] as string; + // ATLinkState status = ATLinkState.ToDownload; + // try { status = ((ATLinkState)((long)rdr["Status"])); } catch (Exception ex) { throw new Exception("ERRORE AFFABULANTE \n" + ex.Message); } - ATLib.Net.ATLink lk = new ATLib.Net.ATLink(plugin, url, fileName, status); - ATLib.ATWDEngine.Instance.AddLink(lk); - Res++; - } - } catch (Exception ex) { - ATLib.Logger.Log(new object[] { "Error Code: DB07003", "Errore in ATLib.Data.DBTools.RetrieveLinks()", ex }); - if (Res > 0) Res = -1 * Res; - } - return Res; - } - } + // ATLib.Net.ATLink lk = new ATLib.Net.ATLink(plugin, url, fileName, status); + // ATLib.ATWDEngine.Instance.AddLink(lk); + // Res++; + // } + // } catch (Exception ex) { + // ATLib.Logger.Log(new object[] { "Error Code: DB07003", "Errore in ATLib.Data.DBTools.RetrieveLinks()", ex }); + // if (Res > 0) Res = -1 * Res; + // } + // return Res; + // } + //} diff --git a/Src/A69WD/General/Cfg.cs b/Src/A69WD/General/Cfg.cs index 3d4046d..a0e7dfb 100644 --- a/Src/A69WD/General/Cfg.cs +++ b/Src/A69WD/General/Cfg.cs @@ -161,7 +161,7 @@ namespace A69WD { public virtual string PathDownloadWorking { get { return Path.Combine(PathApplicationRoot, "Down"); } } /// - /// Path della directory su cui riporto i file di indice correttamente analizzati. + /// Path della directory su cui riporto i file di indice, correttamente analizzati. /// public virtual string PathAnalizadedIndexFile { get { @@ -177,7 +177,7 @@ namespace A69WD { /// - /// Path della directory su cui riporto i file di indice correttamente analizzati. + /// Path della directory su cui riporto i file dei racconti, correttamente analizzati. /// public virtual string PathAnalizadedRaccFile { get { diff --git a/Src/A69WD/Net/cA69Engine.cs b/Src/A69WD/Net/cA69Engine.cs new file mode 100644 index 0000000..c220cd0 --- /dev/null +++ b/Src/A69WD/Net/cA69Engine.cs @@ -0,0 +1,233 @@ +using System; +using System.IO; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows.Forms; + + +namespace A69WD { + + public class cA69Engine { + + + #region Property: State, 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" }; + + 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, aborting, downRac, state, wb + + List allLinks = null; + List toDownload = null; + List downloaded = null; + List racLinks = null; + + DateTime dtStartTime; + + bool aborting = false; + bool downRac = false; + + EngineStatusEnum state = EngineStatusEnum.NotReady; + + WebBrowser wb = null; + + #endregion + + + #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)); + } + state = EngineStatusEnum.StandBy; + } + + #endregion + + + #region Method: + + public bool Start(bool DownRac) { + if (state != EngineStatusEnum.StandBy) return false; + + dtStartTime = DateTime.Now; + downRac = DownRac; + + UpdateST("Inizio lo scrap dei link dei racconti"); + + state = EngineStatusEnum.Starting; + + if (toDownload.Count > 0) { + // proseguo con il download: + string lk = toDownload.First(); + toDownload.Remove(lk); + wb.Navigate(lk); + } + + return true; + } + + public void Stop() { + + } + + #endregion + + + + private void Wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { + object obj = new Object(); + lock (obj) { + + StringBuilder sb; + if (state == EngineStatusEnum.Starting) state = EngineStatusEnum.Started; + + WebBrowser wb = sender as WebBrowser; + string link = wb.Url.ToString(); + + // aggiorno UI + UpdateST(wb.Name + " - Donwloaded: " + link); + + // Se pagina è un racconto, lo salvo + if (IsRacLink(link)) { + sb = new StringBuilder(); + sb.Append(wb.DocumentText); + string fn = Path.Combine(Cfg.Settings.PathAnalizadedRaccFile, GetFileName(link)); + cTools.WriteTextFile(fn, sb); + } + + // Registro che ho eseguito il download del link + downloaded.Add(link); + + // Ciclo su tutti i link estratti dalla pagina: + foreach (HtmlElement item in wb.Document.Links) { + link = item.GetAttribute("href"); + // Se link nuovo: + if (!allLinks.Contains(link)) { + // Link nuovo, mi comporto di conseguenza alla tipologia: + if (IsRacLink(link)) { + // Link ad un racconto + allLinks.Add(link); + if (downRac) { toDownload.Add(link); } + racLinks.Add(link); + } else { + if (IsIdxLink(link)) { + // Link ad un indice + allLinks.Add(link); + toDownload.Add(link); + } else { + // Link non riconosciuto, lo scarto + ; + } + } + } + } // fine ciclo sui link estratti + + // Se ho raggiunto il massimo numero di link, o non vi sono link da downloadare, mi fermo: + if ((toDownload.Count > 0) && (state==EngineStatusEnum.Started)) { + // proseguo con il download: + string lk = toDownload.First(); + toDownload.Remove(lk); + wb.Navigate(lk); + } else { + // Finito download: + state = EngineStatusEnum.Stopping; + // salvo gli archivi: + SaveState(); + } + + }//lock + } + + + private void SaveState() { + StringBuilder sb; + + // Finito download, salvo gli archivi: + string dt = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); + + // Salvo archivio: AllLinks + sb = new StringBuilder(); + allLinks.Sort(); + foreach (string lnk in allLinks) { sb.AppendLine(lnk); } + cTools.WriteTextFile(Path.Combine(Cfg.Settings.PathAnalizadedIndexFile, string.Format("_A69_AllLinks_{0}.tes", dt)), sb); + + // Salvo archivio: toDownload + sb = new StringBuilder(); + toDownload.Sort(); + foreach (string lnk in toDownload) { sb.AppendLine(lnk); } + cTools.WriteTextFile(Path.Combine(Cfg.Settings.PathAnalizadedIndexFile, string.Format("_A69_toDownload_{0}.tes", dt)), sb); + + // Salvo archivio: Downloaded + sb = new StringBuilder(); + downloaded.Sort(); + foreach (string lnk in downloaded) { sb.AppendLine(lnk); } + cTools.WriteTextFile(Path.Combine(Cfg.Settings.PathAnalizadedIndexFile, string.Format("_A69_Downloaded_{0}.tes", dt)), sb); + + // Salvo archivio: RacLinks + sb = new StringBuilder(); + racLinks.Sort(); + foreach (string lnk in racLinks) { sb.AppendLine(lnk); } + 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)); + + 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; } + + + }//class cA69Engine + + + /// + /// Enumerazione di tutti i possibili stati dell'engine: + /// + public enum EngineStatusEnum { + /// + /// Engine da inizializzare + /// + NotReady = 1, + /// + /// Engine inizializzato e pronto a partire + /// + StandBy = 1, + Starting = 2, + Started = 3, + Stopping = 4, + Stopped = 5, + } // enum EngineStatusEnum + +}//namespace A69WD diff --git a/Src/A69WD/Net/frmDownloader.Designer.cs b/Src/A69WD/Net/frmDownloader.Designer.cs index 9b4fba6..c908dcd 100644 --- a/Src/A69WD/Net/frmDownloader.Designer.cs +++ b/Src/A69WD/Net/frmDownloader.Designer.cs @@ -61,6 +61,7 @@ this.btnClose.TabIndex = 14; this.btnClose.Text = "Close"; this.btnClose.UseVisualStyleBackColor = true; + this.btnClose.Click += new System.EventHandler(this.btnClose_Click); // // btnHide // @@ -70,6 +71,7 @@ this.btnHide.TabIndex = 13; this.btnHide.Text = "Hide"; this.btnHide.UseVisualStyleBackColor = true; + this.btnHide.Click += new System.EventHandler(this.btnHide_Click); // // btnDownloadAll // @@ -79,6 +81,7 @@ this.btnDownloadAll.TabIndex = 11; this.btnDownloadAll.Text = "Download All"; this.btnDownloadAll.UseVisualStyleBackColor = true; + this.btnDownloadAll.Click += new System.EventHandler(this.btnDownloadAll_Click); // // lblST // @@ -111,6 +114,7 @@ this.btnDownloadLink.TabIndex = 10; this.btnDownloadLink.Text = "Download only Link"; this.btnDownloadLink.UseVisualStyleBackColor = true; + this.btnDownloadLink.Click += new System.EventHandler(this.btnDownloadLink_Click); // // frmDownloader // diff --git a/Src/A69WD/Net/frmDownloader.cs b/Src/A69WD/Net/frmDownloader.cs index 167e918..f3b8d7c 100644 --- a/Src/A69WD/Net/frmDownloader.cs +++ b/Src/A69WD/Net/frmDownloader.cs @@ -8,9 +8,39 @@ using System.Text; using System.Windows.Forms; namespace A69WD { + public partial class frmDownloader : Form { + + public frmDownloader() { InitializeComponent(); + engine = new cA69Engine(this.wb1); } - } -} + + cA69Engine engine = null; + + + private void btnDownloadLink_Click(object sender, EventArgs e) { + engine.Start(false); + } + + private void btnDownloadAll_Click(object sender, EventArgs e) { + engine.Start(true); + } + + private void btnHide_Click(object sender, EventArgs e) { + this.Hide(); + } + + private void btnClose_Click(object sender, EventArgs e) { + if ((engine.State == EngineStatusEnum.Stopped) || (engine.State == EngineStatusEnum.StandBy)) { + this.Close(); + } else { + engine.Stop(); + } + } + + + + }//class frmDownloader +}//namespace A69WD diff --git a/Src/A69WD/Settings.cfg b/Src/A69WD/Settings.cfg index 510ee6d..3ac382f 100644 --- a/Src/A69WD/Settings.cfg +++ b/Src/A69WD/Settings.cfg @@ -3,8 +3,9 @@ - C:\Temporary\A69WD - + + D:\Project\Private\Prj\MyTools\A69WD\Test + A69DB.db