Compilata da CSiCoAlm il 2021/10/27 - Corretto formato del numero di versione - Razionalizzato il messaggio di update dell'engine. - Inserito gli aggiornamenti dell'engine sul log degli eventi
312 lines
11 KiB
C#
312 lines
11 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, 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<string> allLinks = null;
|
|
List<string> toDownload = null;
|
|
List<string> downloaded = null;
|
|
List<string> racLinks = null;
|
|
|
|
DateTime dtStartTime;
|
|
|
|
bool downRac = false;
|
|
|
|
EngineStatusEnum state = EngineStatusEnum.NotReady;
|
|
|
|
WebBrowser wb = null;
|
|
|
|
#endregion
|
|
|
|
|
|
#region Costruttore e distruttore
|
|
|
|
public cA69Engine(WebBrowser WB) {
|
|
try {
|
|
// Inizializzo gli archivi (dai file in memoria)
|
|
allLinks = new List<string>();
|
|
toDownload = new List<string>();
|
|
downloaded = new List<string>();
|
|
racLinks = new List<string>();
|
|
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 Method:
|
|
|
|
public bool Start(bool DownRac) {
|
|
if (state != EngineStatusEnum.StandBy) return false;
|
|
|
|
dtStartTime = DateTime.Now;
|
|
downRac = DownRac;
|
|
|
|
RaiseEvent_Updating("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() {
|
|
state = EngineStatusEnum.Stopping;
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
#region Eventi:
|
|
|
|
/// <summary>
|
|
/// Evento che indica un cambiamento di stato nell'AnalizeManager
|
|
/// </summary>
|
|
public event EventHandler<EngineEventArgs> Updating;
|
|
|
|
|
|
#region Generazione degli eventi dell'engine verso le classi chiamanti:
|
|
|
|
/// <summary>
|
|
/// Attiva l'evento che segnala un cambiamento di stato nel engine (per esempio, terminato il download di un singolo file)
|
|
/// </summary>
|
|
void RaiseEvent_Updating(string msg, int tot, int toDown, int down) {
|
|
EventHandler<EngineEventArgs> tmp = Updating;
|
|
if (null != tmp) tmp(this, new EngineEventArgs(msg, tot, toDown, down));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Attiva l'evento che segnala un cambiamento di stato nel engine (per esempio, terminato il download di un singolo file)
|
|
/// </summary>
|
|
void RaiseEvent_Updating(string msg) {
|
|
EventHandler<EngineEventArgs> 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();
|
|
lock (obj) {
|
|
|
|
StringBuilder sb;
|
|
if (state == EngineStatusEnum.Starting) state = EngineStatusEnum.Started;
|
|
|
|
WebBrowser wb = sender as WebBrowser;
|
|
string link = wb.Url.ToString();
|
|
|
|
// aggiorno UI
|
|
RaiseEvent_Updating(wb.Name + " - Donwloaded: " + link.Replace("https://www.annunci69.it/racconti-erotici/","./"));
|
|
|
|
// 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() {
|
|
int res = App.DB.SaveData(allLinks, toDownload, downloaded, racLinks);
|
|
|
|
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:
|
|
RaiseEvent_Updating(string.Format("Finito il download in {0}", DateTime.Now - dtStartTime));
|
|
|
|
state = EngineStatusEnum.Stopped;
|
|
|
|
}
|
|
|
|
|
|
private string GetFileName(string url) { return url.Replace("https://www.annunci69.it/racconti-erotici/", "A69_").Replace("/", "_").Replace(":", "_").Replace(@"\", "_"); }
|
|
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
|
|
|
|
|
|
/// <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
|
|
|
|
|
|
public class EngineEventArgs : EventArgs {
|
|
|
|
|
|
#region Property and Fields:
|
|
|
|
|
|
/// <summary>
|
|
/// Numero di file da analizzare, in totale
|
|
/// </summary>
|
|
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
|