-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileFetcher.cs
117 lines (98 loc) · 3.49 KB
/
FileFetcher.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChatGPTBasedBot
{
class FileFetcher
{
private string? productPath;
private string? imageFolder;
private string? productFolder;
private string? path;
private string[]? files;
private int? fileNumber;
public string? imagePath { get; set; }
public string? subject { get; set; }
public string? data { get; set; }
public decimal? price { get; set; }
public string? link { get; set; }
public FileFetcher()
{
productFolder = "Produkty";
imageFolder = "Obrazy";
}
public void FetchProdAndImg()
{
path = Path.Combine(Directory.GetCurrentDirectory(), productFolder);
files = Directory.GetFiles(path);
Random rnd = new Random();
// Losowanie numeru pliku w zakresie od 1 do liczby plików
fileNumber = rnd.Next(1, files.Length + 1);
productPath = Path.Combine(path, "Produkt" + fileNumber.ToString() + ".txt");
imagePath = imageFolder + '\\' + "ObrazProduktu" + fileNumber + ".jpg";
}
public void Fill()
{
string temp = "";
using (StreamReader sr = new StreamReader(productPath))
{
temp = sr.ReadToEnd();
}
string[] strings = temp.Split("\n");
for (var i = 0; i < strings.Length; i++)
{
if (strings[i].Contains("Tytuł: "))
{
subject = strings[i].Replace("Tytuł: ", "").Trim();
}
if (strings[i].Contains("div class"))
{
strings[i] = "";
}
if (strings[i].Contains("Opis: <h2>Opis</h2>"))
{
data = FetchData(strings, i + 1);
}
else if (strings[i].Contains("Opis: "))
{
data = strings[i].Replace("Opis: ", "").Trim();
}
if (strings[i].Contains("Cena: "))
{
string priceString = strings[i].Replace("Cena: ", "").Replace(" zł", "").Trim();
if (decimal.TryParse(priceString, out decimal parsedPrice))
{
price = parsedPrice;
}
else
{
price = 0;
}
}
if (strings[i].Contains("ID Produktu:"))
{
strings[i] = "";
}
if (strings[i].Contains("Link: "))
{
link = strings[i].Replace("Link: ", "").Trim();
}
}
}
private string FetchData(string[] strings, int startIndex)
{
string data = "";
for (var i = startIndex; i < strings.Length; i++)
{
if (!strings[i].Contains("Cena: ") && !strings[i].Contains("ID Produktu:") && !strings[i].Contains("Link: "))
{
data += strings[i].Trim() + "\n";
}
else break;
}
return data.Trim();
}
}
}