234 lines
8.2 KiB
C#
234 lines
8.2 KiB
C#
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<string> allLinks = null;
|
|
List<string> toDownload = null;
|
|
List<string> downloaded = null;
|
|
List<string> 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<string>();
|
|
toDownload = new List<string>();
|
|
downloaded = new List<string>();
|
|
racLinks = new List<string>();
|
|
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
|
|
|
|
|
|
/// <summary>
|
|
/// Enumerazione di tutti i possibili stati dell'engine:
|
|
/// </summary>
|
|
public enum EngineStatusEnum {
|
|
/// <summary>
|
|
/// Engine da inizializzare
|
|
/// </summary>
|
|
NotReady = 1,
|
|
/// <summary>
|
|
/// Engine inizializzato e pronto a partire
|
|
/// </summary>
|
|
StandBy = 1,
|
|
Starting = 2,
|
|
Started = 3,
|
|
Stopping = 4,
|
|
Stopped = 5,
|
|
} // enum EngineStatusEnum
|
|
|
|
}//namespace A69WD
|