From dfe56ef6aebf50d0ebe2f7dd188239c026df97b7 Mon Sep 17 00:00:00 2001 From: CSiCoAlm Date: Thu, 1 Jan 2026 12:23:58 +0100 Subject: [PATCH] =?UTF-8?q?Test=20download=20con=20pi=C3=B9=20client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Src/A69WD/Site/cSite_A69.cs | 75 +++++++++++++++++++++++------- Src/A69WD/UI/frmReader.Designer.cs | 37 +++++++++++++-- Src/A69WD/UI/frmReader.cs | 71 +++++++++++++++++++++++++++- 3 files changed, 159 insertions(+), 24 deletions(-) diff --git a/Src/A69WD/Site/cSite_A69.cs b/Src/A69WD/Site/cSite_A69.cs index 87c5eaf..86d164e 100644 --- a/Src/A69WD/Site/cSite_A69.cs +++ b/Src/A69WD/Site/cSite_A69.cs @@ -112,21 +112,6 @@ namespace A69WD.Site { HtmlDocument tdoc = null; #region ToConfirm - //if (lk.Status == eLinkState.ToConfirm) { - // // Eseguo la verifica se link da downloadare - // eLinkState tstate = App.DB.GetStateByURL(lk.URL); - // if ( - // (tstate == eLinkState.ToConfirm) || - // (tstate == eLinkState.ToDownload) || - // (tstate == eLinkState.Error) - // ) { - // lk.Status = eLinkState.ToDownload; - // App.DB.LinkLog(lk); - // } else { - // lk.Status = eLinkState.End; - // App.DB.LinkLog(lk); - // } - //} if (lk.Status == eLinkState.ToConfirm) { // Eseguo la verifica se link da downloadare lk.Status = eLinkState.ToDownload; @@ -294,21 +279,75 @@ namespace A69WD.Site { return newlinks; } + public async Task> CrawlPageAsyncTest1(iLink lk) { + var newlinks = new List(); + try { + // Scarico il file + var resp = await client.GetAsync(lk.URL); + if (!resp.IsSuccessStatusCode) { + Console.WriteLine($"A69 - Errore HTTP {(int)resp.StatusCode} {resp.ReasonPhrase} per {lk.URL}"); + lk.Status = eLinkState.Error; + //App.DB.LinkLog(lk); + return newlinks; + } + // Scarico la pagina + var thtml = await client.GetStringAsync(lk.URL); + // Salvo il file downloadato + System.IO.StreamWriter tsw = new System.IO.StreamWriter(System.IO.Path.Combine(lk.FilePath, lk.FileName), false, Encoding.UTF8); + tsw.Write(thtml); + + Console.WriteLine("Link.Count=" + newlinks.Count.ToString("00")); + + } catch (Exception ex) { + Console.WriteLine($"Errore con {lk.URL}: {ex.Message}"); + lk.Status = eLinkState.Error; + //App.DB.LinkLog(lk); + } + return newlinks; + } + + public async Task> CrawlPageAsyncTest2(iLink lk) { + var newlinks = new List(); + try { + // Scarico il file + var resp = await client.GetAsync(lk.URL); + if (!resp.IsSuccessStatusCode) { + Console.WriteLine($"A69 - Errore HTTP {(int)resp.StatusCode} {resp.ReasonPhrase} per {lk.URL}"); + lk.Status = eLinkState.Error; + //App.DB.LinkLog(lk); + return newlinks; + } + // Scarico la pagina + HttpClient tmpClient = new HttpClient(_handler); + var thtml = await tmpClient.GetStringAsync(lk.URL); + // Salvo il file downloadato + System.IO.StreamWriter tsw = new System.IO.StreamWriter(System.IO.Path.Combine(lk.FilePath, lk.FileName), false, Encoding.UTF8); + tsw.Write(thtml); + + Console.WriteLine("Link.Count=" + newlinks.Count.ToString("00")); + + } catch (Exception ex) { + Console.WriteLine($"Errore con {lk.URL}: {ex.Message}"); + lk.Status = eLinkState.Error; + //App.DB.LinkLog(lk); + } + return newlinks; + } public cSite_A69() { - var handler = new HttpClientHandler { + _handler = new HttpClientHandler { CookieContainer = new CookieContainer(), UseCookies = true, AllowAutoRedirect = true, AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }; - client = new HttpClient(handler); + client = new HttpClient(_handler); client.Timeout = TimeSpan.FromSeconds(60); client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"); client.DefaultRequestHeaders.Referrer = new Uri(cLink_A69.LoginPageUrl); } - + HttpClientHandler _handler = null; diff --git a/Src/A69WD/UI/frmReader.Designer.cs b/Src/A69WD/UI/frmReader.Designer.cs index 7f835c5..0e4399b 100644 --- a/Src/A69WD/UI/frmReader.Designer.cs +++ b/Src/A69WD/UI/frmReader.Designer.cs @@ -28,18 +28,22 @@ this.sbMessage = new System.Windows.Forms.ToolStripStatusLabel(); this.sbProgressBar = new System.Windows.Forms.ToolStripProgressBar(); this.sbStatusLabel = new System.Windows.Forms.ToolStripStatusLabel(); + this.btnTest = new System.Windows.Forms.Button(); + this.btnTest2 = new System.Windows.Forms.Button(); this.sb.SuspendLayout(); this.SuspendLayout(); // // rtb // + this.rtb.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); this.rtb.BackColor = System.Drawing.Color.White; this.rtb.BorderStyle = System.Windows.Forms.BorderStyle.None; - this.rtb.Dock = System.Windows.Forms.DockStyle.Fill; this.rtb.Font = new System.Drawing.Font("Verdana", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.rtb.Location = new System.Drawing.Point(0, 0); + this.rtb.Location = new System.Drawing.Point(0, 54); this.rtb.Name = "rtb"; - this.rtb.Size = new System.Drawing.Size(700, 420); + this.rtb.Size = new System.Drawing.Size(700, 366); this.rtb.TabIndex = 3; this.rtb.Text = ""; this.rtb.KeyDown += new System.Windows.Forms.KeyEventHandler(this.KeyDownAnalisi); @@ -64,7 +68,7 @@ | System.Windows.Forms.ToolStripStatusLabelBorderSides.Bottom))); this.sbMessage.BorderStyle = System.Windows.Forms.Border3DStyle.SunkenInner; this.sbMessage.Name = "sbMessage"; - this.sbMessage.Size = new System.Drawing.Size(452, 19); + this.sbMessage.Size = new System.Drawing.Size(483, 19); this.sbMessage.Spring = true; this.sbMessage.Tag = "Messaggi"; this.sbMessage.Text = "Messaggi"; @@ -90,12 +94,34 @@ this.sbStatusLabel.Tag = "Label"; this.sbStatusLabel.ToolTipText = "Stato di eventuali operazioni in background"; // + // btnTest + // + this.btnTest.Location = new System.Drawing.Point(4, 4); + this.btnTest.Name = "btnTest"; + this.btnTest.Size = new System.Drawing.Size(62, 48); + this.btnTest.TabIndex = 4; + this.btnTest.Text = "Test"; + this.btnTest.UseVisualStyleBackColor = true; + this.btnTest.Click += new System.EventHandler(this.btnTest_Click); + // + // btnTest2 + // + this.btnTest2.Location = new System.Drawing.Point(72, 4); + this.btnTest2.Name = "btnTest2"; + this.btnTest2.Size = new System.Drawing.Size(62, 48); + this.btnTest2.TabIndex = 5; + this.btnTest2.Text = "Test 2"; + this.btnTest2.UseVisualStyleBackColor = true; + this.btnTest2.Click += new System.EventHandler(this.btnTest2_Click); + // // frmReader // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(700, 444); this.ControlBox = false; + this.Controls.Add(this.btnTest2); + this.Controls.Add(this.btnTest); this.Controls.Add(this.rtb); this.Controls.Add(this.sb); this.Name = "frmReader"; @@ -115,6 +141,7 @@ private System.Windows.Forms.ToolStripStatusLabel sbMessage; private System.Windows.Forms.ToolStripProgressBar sbProgressBar; private System.Windows.Forms.ToolStripStatusLabel sbStatusLabel; - + private System.Windows.Forms.Button btnTest; + private System.Windows.Forms.Button btnTest2; } } \ No newline at end of file diff --git a/Src/A69WD/UI/frmReader.cs b/Src/A69WD/UI/frmReader.cs index f19345a..8c5a185 100644 --- a/Src/A69WD/UI/frmReader.cs +++ b/Src/A69WD/UI/frmReader.cs @@ -746,6 +746,75 @@ namespace A69WD.UI { cTools.ShowOnNotepad(Cfg.Settings.PathFileLogs); } - + private async void btnTest_Click(object sender, EventArgs e) { + // Test download multipli con sempre lo stesso client per condividere la sessione + + // - Creo istanza del client per il sito con autenticazione: + sbStatusLabel.Text = "Login on A69"; + cSite_A69 site = new cSite_A69(); + + // - Eseguo l'autenticazione + bool res = await site.Authenticate(); + if (res) { + sbStatusLabel.Text = "A69 logged"; + Logger.Information("Sito A69 loggato: " + res.ToString()); + } else { + Logger.Information("Login Fault"); + return; + } + + // - Verifico path per il download + if (!System.IO.Directory.Exists(Cfg.Settings.PathDownloadWorking)) System.IO.Directory.CreateDirectory(Cfg.Settings.PathDownloadWorking); + cLink_A69[] lks = new cLink_A69[26]; + for (int i = 1; i < lks.Length; i++) { + lks[i] = new cLink_A69($"https://www.annunci69.it/racconti-erotici/racconti_lista.php?genere=altro&pagina={i}", $"_A69_altro_{i.ToString("0000")}.html"); + } + + DateTime start = DateTime.Now; + // - Download dei link come finora fato (steso client per condividere la sessione): + for (int i = 1; i < lks.Length; i++) { + List urls = await site.CrawlPageAsyncTest1(lks[i]); + } + DateTime end = DateTime.Now; + + sbStatusLabel.Text = (start - end).ToString(); + + } + + private async void btnTest2_Click(object sender, EventArgs e) { + + // Test download multipli con client diversi ma che condividono solo la sessione + + // - Creo istanza del client per il sito con autenticazione: + sbStatusLabel.Text = "Login on A69"; + cSite_A69 site = new cSite_A69(); + + // - Eseguo l'autenticazione + bool res = await site.Authenticate(); + if (res) { + sbStatusLabel.Text = "A69 logged"; + Logger.Information("Sito A69 loggato: " + res.ToString()); + } else { + Logger.Information("Login Fault"); + return; + } + + // - Verifico path per il download + if (!System.IO.Directory.Exists(Cfg.Settings.PathDownloadWorking)) System.IO.Directory.CreateDirectory(Cfg.Settings.PathDownloadWorking); + cLink_A69[] lks = new cLink_A69[26]; + for (int i = 1; i < lks.Length; i++) { + lks[i] = new cLink_A69($"https://www.annunci69.it/racconti-erotici/racconti_lista.php?genere=altro&pagina={i}", $"_A69_altro_{i.ToString("0000")}.html"); + } + + DateTime start = DateTime.Now; + // - Download dei link come finora fato (steso client per condividere la sessione): + for (int i = 1; i < lks.Length; i++) { + List urls = await site.CrawlPageAsyncTest1(lks[i]); + } + DateTime end = DateTime.Now; + + sbStatusLabel.Text = (start - end).ToString(); + + } } }