-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCardsLoader.cs
136 lines (117 loc) · 5.32 KB
/
CardsLoader.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
using System;
using System.Linq;
using Newtonsoft.Json.Linq;
using System.Net;
using System.IO;
using System.Collections.Generic;
namespace JsonReaderYugi
{
class CardsLoader
{
private const string pathCards = @"sources\cards\";
private const string pathSmallCards = @"sources\smallCards\";
WebClient client = new();
List<Card> ListOfcards = new List<Card>();
public List<Card> StartDownload(string path)
{
using (StreamReader ReaderObject = new StreamReader(path))
{
string Line;
ListOfcards = new List<Card>();
// ReaderObject reads a single line, stores it in Line string variable and then displays it on console
if (!Directory.Exists(pathCards))
{
Directory.CreateDirectory(pathCards);
Console.WriteLine(pathCards);
}
if (!Directory.Exists(pathSmallCards))
{
Directory.CreateDirectory(pathSmallCards);
Console.WriteLine(pathSmallCards);
}
while ((Line = ReaderObject.ReadLine()) != null)
{
getJasonFromApiCard(Line);
client = new();
}
Console.WriteLine("Se guardaron las cartas");
Serializator.SerializeCards(ListOfcards);
}
return ListOfcards;
}
private void getJasonFromApiCard(String id)
{
Card card = new();
String url = "https://db.ygoprodeck.com/api/v7/cardinfo.php?name=" + id;
String myJson = client.DownloadString(url);
var file = Newtonsoft.Json.JsonConvert.DeserializeObject(myJson);
//get values from data in json file
var prop = ((JProperty)((JObject)file).First).Value;
foreach (JObject item in ((JArray)prop).Children())
{
//To get value use JProperty.Value\
JProperty ID = item.Properties().FirstOrDefault(p => p.Name.Contains("id"));
JProperty name = item.Properties().FirstOrDefault(p => p.Name.Contains("name"));
JProperty type = item.Properties().FirstOrDefault(p => p.Name.Contains("type"));
JProperty def = item.Properties().FirstOrDefault(p => p.Name.Contains("def"));
JProperty desc = item.Properties().FirstOrDefault(p => p.Name.Contains("desc"));
JProperty atk = item.Properties().FirstOrDefault(p => p.Name.Contains("atk"));
JProperty level = item.Properties().FirstOrDefault(p => p.Name.Contains("level"));
JProperty race = item.Properties().FirstOrDefault(p => p.Name.Contains("race"));
JProperty attribute = item.Properties().FirstOrDefault(p => p.Name.Contains("attribute"));
JProperty card_images = item.Properties().FirstOrDefault(p => p.Name.Contains("card_images"));
String IDF = (String)ID.Value;
String nameF = (String)name.Value;
String typeF = (String)type.Value;
String descF = (String)desc.Value;
int atkF = -1;
int defF = -1;
int levelF = -1;
String raceF = "";
String attributeF = "";
try {
atkF = (int)atk.Value;
defF = (int)def.Value;
levelF = (int)level.Value;
raceF = (String)race.Value;
attributeF = (String)attribute.Value;
}
catch (NullReferenceException e)
{
//if (e.Source != null)
//Console.WriteLine("IOException source: {0}", e.Source);
}
JArray card_imagesF = (JArray)card_images.Value;
String cardUrl = "";
String smallCardUrl = "";
foreach (JObject imagT in card_imagesF.Children())
{
JProperty imgUrl = imagT.Properties().FirstOrDefault(p => p.Name.Contains("image_url"));
JProperty smallImgUrl = imagT.Properties().FirstOrDefault(p => p.Name.Contains("image_url_small"));
cardUrl = (string)imgUrl.Value;
smallCardUrl = (string)smallImgUrl.Value;
}
saveImage(cardUrl, IDF);
saveImageSmall( smallCardUrl, IDF);
card = new Card(IDF, nameF, typeF, descF, defF, atkF, levelF, raceF, attributeF,cardUrl,smallCardUrl);
ListOfcards.Add(card);
break;
}
Console.WriteLine("Card : " + id + " has been charged.");
}
public void saveImage(String imgUrl, String id)
{
if (!File.Exists(pathCards + id + ".jpg"))
{
client.DownloadFile(new Uri(imgUrl), pathCards + id + ".jpg");
}
}
public void saveImageSmall(String imgUrl, String id)
{
if (!File.Exists(pathSmallCards + id + ".jpg"))
{
client.DownloadFile(new Uri(imgUrl), pathSmallCards + id + ".jpg");
}
}
}
}