-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
178 lines (152 loc) · 7.79 KB
/
Program.cs
File metadata and controls
178 lines (152 loc) · 7.79 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using JsonConverter;
using System;
using System.Collections.Generic;
namespace JsonCrawlParser
{
/// <summary>
/// An application used to parse the results of a Ferguson client site crawl performed by
/// ParseHub.
/// </summary>
class Program
{
/// <summary>
/// Main entry point.
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
/*******************************************************
* CONSTANTS
* ------------------------------------------------------
* (none)
********************************************************/
string resourcePath; // The root path to save documents/images.
JsonConverter converter; // Converts json to product data.
int imageCount; // Total number of images saved locally.
string json; // Json string contained within the source file (raw products).
List<JsonConverter.ProductModel> products; // All products found in json.
string choice; // Response to prompt question.
string jsonSource; // Full path to json file containing crawl results.
// Instantiate / initialize.
converter = new JsonConverter();
imageCount = 0;
// Get the full path to the json file containing product crawl results.
Console.WriteLine("Read path to .json crawl file:");
jsonSource = Console.ReadLine();
// Read all text from the json file and convert to products.
Console.WriteLine("Parsing Data...");
json = System.IO.File.ReadAllText(jsonSource);
products = converter.Convert(json);
// Ask the user if they would like to download the product's associated documents?
Console.WriteLine("Download documents: y/n");
choice = Console.ReadLine();
// Should the product's documents be downloaded?
if (choice == "y")
{
/*******************************************************
* CONSTANTS
* ------------------------------------------------------
* (none)
********************************************************/
FileDownload fileLoader; // Used to download the document file from the web.
// Get the path to the folder the resources should be saved to.
Console.WriteLine("Write path for documents: ");
resourcePath = Console.ReadLine(); // Ex. @"C:\Users\{User Name}\Desktop\product_images\";
// Instantiate.
fileLoader = new FileDownload();
// Iterate over all of the products.
foreach (var item in products)
{
/*******************************************************
* CONSTANTS
* ------------------------------------------------------
* (none)
********************************************************/
string savePath; // Save path for the product's document.
// Is there a document?
if (item.Doc1Href.Length != 0)
{
// Set the save path to the resource root. Use the product's model number
// and the document's name as the file name.
savePath = String.Format("{0}{1}_{2}.pdf", resourcePath, item.Model, item.Doc1Name);
// Download the file.
fileLoader.Download(item.Doc1Href, savePath);
}
// Is there a second document?
if (item.Doc2Href.Length != 0)
{
// Set the save path to the resource root. Use the product's model number
// and the document's name as the file name.
savePath = String.Format("{0}{1}_{2}.pdf", resourcePath, item.Model, item.Doc2Name);
// Download the file.
fileLoader.Download(item.Doc2Href, savePath);
}
}
}
// Ask the user if they would like to download the product's images?
Console.WriteLine("Download images: y/n");
choice = Console.ReadLine();
// Download the images?
if (choice == "y")
{
/*******************************************************
* CONSTANTS
* ------------------------------------------------------
* (none)
********************************************************/
ImageConverter imageConv;
// Instantiate.
imageConv = new ImageConverter();
// Get the path to the folder the resources should be saved to.
Console.WriteLine("Write path for images: ");
resourcePath = Console.ReadLine(); // Ex. @"C:\Users\{User Name}\Desktop\product_images\";
// Update user that the save it in process.
Console.WriteLine("Save Images...");
// Iterate over all products.
foreach (var item in products)
{
// Is there an image to convert?
if (item.ImageBase64.Length != 0 && item.Model.Length != 0)
{
/*******************************************************
* CONSTANTS
* ------------------------------------------------------
* (none)
********************************************************/
//"data:image/png;base64,iVBORw0KGgoA...
string[] imageData = item.ImageBase64.Split(',');
string[] typeData = imageData[0].Split(';');
string type = typeData[0].Replace("data:image/", "");
// Save the image to the resources folder.
imageConv.SaveImage(imageData[1], String.Format("{0}{0}.{1}", resourcePath, item.Model, type));
Console.Clear();
Console.WriteLine(String.Format("Image: {0}", ++imageCount));
}
}
}
// Ask the user if they would like to write the products to Excel?
Console.WriteLine("Write to Excel: y/n");
choice = Console.ReadLine();
// Write to Excel?
if (choice == "y")
{
/*******************************************************
* CONSTANTS
* ------------------------------------------------------
* (none)
********************************************************/
string savePath; // The path to save the products to.
ExcelDAO dao; // Utility for saving the products to Excel.
// Get save path.
Console.WriteLine("Full path for xls file to be created: ");
savePath = Console.ReadLine(); // Ex. @"C:\Users\{User Name}\Desktop\pipe_and_tubing_Parsed.xls";
// Save.
dao = new ExcelDAO(savePath);
dao.Write(ref products);
}
// Done.
Console.WriteLine("Done!");
Console.Read();
}
}
}