-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
159 lines (136 loc) · 5.97 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
namespace WinXpath
{
public partial class Form1 : Form
{
private string _inicioSolo ;
private string _archivoLog;
public Form1()
{
InitializeComponent();
}
private void cargarArchivoConfig() {
//inicioYCierraSolo
this.txPagina.Text = ConfigurationManager.AppSettings["pagina_busqueda"];
this.txXpath.Text = ConfigurationManager.AppSettings["xpath_busqueda"];
this.txXpathVal.Text = ConfigurationManager.AppSettings["xpath_valida"];
this.txXpathValor.Text = ConfigurationManager.AppSettings["xpath_valida_valor"];
this.txXpath2.Text = ConfigurationManager.AppSettings["xpath_busqueda2"];
this.txXpathVal2.Text = ConfigurationManager.AppSettings["xpath_valida2"];
this.txXpathValor2.Text = ConfigurationManager.AppSettings["xpath_valida_valor2"];
this.txArchivoResultado.Text = ConfigurationManager.AppSettings["archivo_resultado"];
this._inicioSolo = ConfigurationManager.AppSettings["inicioYCierraSolo"];
this._archivoLog = AppDomain.CurrentDomain.BaseDirectory + "log.txt";
}
private void guardarEnArchivo(string resultado) {
String newLine = "";
System.IO.StreamWriter stream = System.IO.File.AppendText(this.txArchivoResultado.Text);
newLine = resultado + ";" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") ;
guardarLog("Escribe el resultado: " + resultado + " en el archivo: " + this.txArchivoResultado.Text );
stream.WriteLine(newLine);
stream.Close();
}
private void guardarLog(string lineaLog) {
lineaLog = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + ";" + lineaLog;
if (!System.IO.File.Exists(this._archivoLog))
{
// Create a file to write to.
using (System.IO.StreamWriter sw = System.IO.File.CreateText(this._archivoLog))
{
sw.WriteLine(lineaLog);
}
}
else {
using (System.IO.StreamWriter sw = System.IO.File.AppendText(this._archivoLog ))
{
sw.WriteLine(lineaLog);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
HtmlAgilityPack.HtmlDocument htmldocu = new HtmlAgilityPack.HtmlDocument();
try
{
WebClient myWebClient = new WebClient();
myWebClient.Encoding = Encoding.UTF8;
this.textBox1.Text = myWebClient.DownloadString(this.txPagina.Text);
htmldocu.LoadHtml(this.textBox1.Text);
}
catch (Exception ex)
{
guardarLog("Error al obtener el datos:\n" + ex.Message);
}
try
{
if (System.IO.File.Exists(this.txArchivoResultado.Text))
{
System.IO.File.Delete(this.txArchivoResultado.Text);
System.IO.StreamWriter stream = System.IO.File.CreateText(this.txArchivoResultado.Text);
stream.Close();
}
//dolar
if (this.txXpath.Text != "")
{
string resultado = htmldocu.DocumentNode.SelectSingleNode(this.txXpath.Text).InnerHtml;
//si tiene datos que validar
if (this.txXpathValor.Text != "")
{
string resultado_valida = htmldocu.DocumentNode.SelectSingleNode(this.txXpathVal.Text).InnerHtml;
guardarLog("Compara el valor:" + this.txXpathValor.Text +" contra el resultado: "+ resultado_valida);
if (resultado_valida == this.txXpathValor.Text)
{
guardarEnArchivo(resultado);
}
}
else {
guardarEnArchivo(resultado);
}
}
//euro
if (this.txXpath2.Text != "")
{
string resultado = htmldocu.DocumentNode.SelectSingleNode(this.txXpath2.Text).InnerHtml;
//si tiene datos que validar
if (this.txXpathValor2.Text != "")
{
string resultado_valida = htmldocu.DocumentNode.SelectSingleNode(this.txXpathVal2.Text).InnerHtml;
guardarLog("Compara el valor:" + this.txXpathValor2.Text + " contra el resultado: " + resultado_valida);
if (resultado_valida == this.txXpathValor2.Text)
{
guardarEnArchivo(resultado);
}
}
else
{
guardarEnArchivo(resultado);
}
}
}
catch (Exception ex)
{
guardarLog("Error al guardar el datos:\n" + ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
cargarArchivoConfig();
guardarLog("Inicio de proceso");
if (this._inicioSolo == "true") {
this.button1_Click(sender, e);
this.Close();
System.Windows.Forms.Application.Exit();
}
guardarLog("Fin de proceso");
}
}
}