263 lines
9.5 KiB
C#
263 lines
9.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using System.IO;
|
|
|
|
|
|
|
|
namespace A69WD {
|
|
public partial class Form1 : Form {
|
|
|
|
|
|
List<string> AllLinks = null;
|
|
List<string> toDownload = null;
|
|
List<string> Downloaded = null;
|
|
List<string> RacLinks = null;
|
|
|
|
DateTime dtStartTime;
|
|
|
|
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/";
|
|
|
|
const int MAXLINK = 1000;
|
|
int cnt = 0;
|
|
|
|
bool aborting = false;
|
|
bool DownRac = false;
|
|
|
|
|
|
public string DstIdx {
|
|
get {
|
|
if (string.IsNullOrEmpty(dstIdx)) {
|
|
// creo path e verifico l'esistenza
|
|
dstIdx = Path.Combine(System.IO.Path.Combine(tbPath.Text, DateTime.Now.ToString("yyyy-MM-dd")), "Indici");
|
|
if (!Directory.Exists(dstIdx)) Directory.CreateDirectory(dstIdx);
|
|
}
|
|
return dstIdx;
|
|
}
|
|
}
|
|
private string dstIdx = "";
|
|
|
|
|
|
public string DstRac {
|
|
get {
|
|
if (string.IsNullOrEmpty(dstRac)) {
|
|
// creo path e verifico l'esistenza
|
|
dstRac = Path.Combine(System.IO.Path.Combine(tbPath.Text, DateTime.Now.ToString("yyyy-MM-dd")), "Testi");
|
|
if (!Directory.Exists(dstRac)) Directory.CreateDirectory(dstRac);
|
|
}
|
|
return dstRac;
|
|
}
|
|
}
|
|
private string dstRac = "";
|
|
|
|
|
|
|
|
|
|
// Mettere che alla chiusura del form, salvo i valori degli elenchi tramite serializzzazione
|
|
|
|
|
|
|
|
public Form1() {
|
|
InitializeComponent();
|
|
// Inizializzo gli archivi (dai file in memoria)
|
|
AllLinks = new List<string>();
|
|
toDownload = new List<string>();
|
|
Downloaded = new List<string>();
|
|
RacLinks = new List<string>();
|
|
|
|
wb1.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));
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void btnDownloadLink_Click(object sender, EventArgs e) {
|
|
dtStartTime = DateTime.Now;
|
|
|
|
DownRac = false;
|
|
|
|
UpdateST("Inizio lo scrap dei link dei racconti");
|
|
if ((toDownload.Count > 0) && (cnt < MAXLINK)) {
|
|
// proseguo con il download:
|
|
string lk = toDownload.First();
|
|
toDownload.Remove(lk);
|
|
wb1.Navigate(lk);
|
|
}
|
|
|
|
btnDownloadLink.Enabled = false;
|
|
}
|
|
|
|
|
|
private void btnDownloadAll_Click(object sender, EventArgs e) {
|
|
dtStartTime = DateTime.Now;
|
|
|
|
DownRac = true;
|
|
|
|
UpdateST("Inizio lo scrap dei link dei racconti");
|
|
if ((toDownload.Count > 0) && (cnt < MAXLINK)) {
|
|
// proseguo con il download:
|
|
string lk = toDownload.First();
|
|
toDownload.Remove(lk);
|
|
wb1.Navigate(lk);
|
|
}
|
|
|
|
btnDownloadLink.Enabled = false;
|
|
}
|
|
|
|
|
|
private void btnHide_Click(object sender, EventArgs e) { MessageBox.Show("Metodo ancora da fare"); }
|
|
|
|
|
|
private void btnClose_Click(object sender, EventArgs e) { aborting = true; }
|
|
|
|
|
|
private void btbPath_Click(object sender, EventArgs e) { MessageBox.Show("Metodo ancora da fare"); }
|
|
|
|
|
|
private void tbPath_TextChanged(object sender, EventArgs e) {
|
|
dstIdx = dstRac = "";
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void Wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
|
|
object obj = new Object();
|
|
lock (obj) {
|
|
cnt++;
|
|
StringBuilder sb;
|
|
|
|
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(DstRac, 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) && ((cnt < MAXLINK) || (DownRac)) && (!aborting)) {
|
|
// proseguo con il download:
|
|
string lk = toDownload.First();
|
|
toDownload.Remove(lk);
|
|
wb.Navigate(lk);
|
|
} else {
|
|
// Finito download, salvo gli archivi:
|
|
SaveState();
|
|
}
|
|
|
|
}//lock
|
|
}
|
|
|
|
|
|
private void SaveState() {
|
|
StringBuilder sb;
|
|
|
|
// Preparo il path di destinazione
|
|
string dstIdx = Path.Combine(System.IO.Path.Combine(tbPath.Text, DateTime.Now.ToString("yyyy-MM-dd")), "Indici");
|
|
if (!Directory.Exists(dstIdx)) Directory.CreateDirectory(dstIdx);
|
|
string dstRac = Path.Combine(System.IO.Path.Combine(tbPath.Text, DateTime.Now.ToString("yyyy-MM-dd")), "Testi");
|
|
if (!Directory.Exists(dstRac)) Directory.CreateDirectory(dstRac);
|
|
|
|
// 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(DstIdx, 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(DstIdx, 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(DstIdx, 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(DstIdx, string.Format("_A69_RacLinks_{0}.tes", dt)), sb);
|
|
|
|
// avviso utente:
|
|
UpdateST(string.Format("Finito il download in {0}", DateTime.Now - dtStartTime));
|
|
|
|
// Riattivo UI:
|
|
btnDownloadLink.Enabled = true;
|
|
btnDownloadAll.Enabled = true;
|
|
|
|
// Chiudo applicazione
|
|
this.Close();
|
|
}
|
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
|
|
|
}
|
|
}
|