Projekt

Obecné

Profil

Stáhnout (14.5 KB) Statistiky
| Větev: | Tag: | Revize:
1
//
2
// Author: Eliska Mourycova
3
//
4

    
5
using log4net;
6
using System;
7
using System.Collections.Generic;
8
using System.IO;
9
using System.IO.Compression;
10
using System.Net;
11

    
12
namespace ServerApp.DataDownload
13
{
14
	/// <summary>
15
	/// Enum representing all of the available data types (not all will be used in this project)
16
	/// They are in Czech for easier handling file names.
17
	/// TBD: They might be translated to English later.
18
	/// </summary>
19
	public enum DataType
20
	{
21
		POCASI, ENERGO, STROJE, EMAIL, OBSAZENI_MISTNOSTI, JIS, KOLOBEZKY, WIFI
22
	}
23

    
24
	/// <summary>
25
	/// Represent all available data formats.
26
	/// </summary>
27
	public enum DataFormat
28
	{
29
		XML, JSON, CSV
30
	}
31

    
32
	/// <summary>
33
	/// This class takes care of downloading of data. Download happens from http://openstore.zcu.cz/.
34
	/// </summary>
35
	public class DataDownloader : IDataDownloader
36
	{
37
		/// <summary>
38
		/// The root directory containing all downloaded data
39
		/// </summary>
40
		public string RootDataDirectory { get; }
41

    
42
		/// <summary>
43
		/// For a DataType key returns full (absolute) path to a direcotry, where this type of data is stored
44
		/// </summary>
45
		public Dictionary<DataType, string> DataSubDirectories { get; }
46

    
47
		/// <summary>
48
		/// Flag stating whether files which already exist should be overwritten when downloaded again
49
		/// </summary>
50
		public bool OverwriteExisting { get; set; }
51

    
52
		// the main site where the data can be downloaded from
53
		private string site;
54

    
55
		// weather prediction site
56
		private string weatherPredictionSite;
57

    
58
		// the substring at the start of every file name
59
		private string dataStr;
60

    
61
		// WebClient instance used for the actual download
62
		private WebClient webClient;
63

    
64
		// a shortcut to writing Path.DirectorySeparatorChar
65
		private char sep = Path.DirectorySeparatorChar;
66

    
67
		// lists used for parsing the file names:
68
		private List<string> separatedFileName;
69
		private List<string> variablesInsertions;
70
		private List<char> nameTurns;
71

    
72
		// logger class instance
73
		private static readonly ILog logger = LogManager.GetLogger(typeof(DataDownloader));
74

    
75
		public DataDownloader(string rootDataDir, string website, string namingConvention, string weatherPredSite) // todo: take naming conventons specifiaction into account
76
		{
77
			// initialize all needed variables:
78

    
79
			//Console.WriteLine(Directory.GetCurrentDirectory());
80
			site = website;//"http://openstore.zcu.cz/";
81
			weatherPredictionSite = weatherPredSite;
82

    
83
			ParseNaming(namingConvention);
84
			dataStr = "OD_ZCU_";
85

    
86
			RootDataDirectory = rootDataDir;//$"..{sep}..{sep}..{sep}data{sep}auto";
87
			OverwriteExisting = false;
88

    
89
			DataSubDirectories = new Dictionary<DataType, string>();
90
			CreateSubdirectories(); // todo should we do it like this?
91

    
92
			webClient = new WebClient();
93

    
94
			logger.Info("Data downlader ready.");
95
		}
96

    
97
		/// <summary>
98
		/// Downloads json file - returns contents of said file
99
		/// </summary>
100
		/// <returns> String containing the weather prediction </returns>
101
		public string DownloadWeatherPrediction()
102
		{
103
			// TODO either set this path as attribute or if parameter JsonParser needs an attribute that would be set through constructor
104
			//string predictionSite = "http://wttr.in/Plzen,czechia?format=j1";
105

    
106
			//DateTime now = DateTime.Now;
107
			//WebClient webClient = new WebClient();
108
			//webClient.DownloadFile(predictionSite, $"data/{now.Year}{now.Month}{now.Day}.json");
109
			
110
			try
111
			{
112
				return webClient.DownloadString(weatherPredictionSite);// $"data/{now.Year}{now.Month}{now.Day}.json";
113
			}
114
			catch(Exception e)
115
			{
116
				// this shouldn't happen
117
				//Console.WriteLine("Weather prediction download failed!");
118
				logger.Error("Weather prediction download failed!");
119
				logger.Error(e.Message);
120
				return null;
121
			}
122
			
123
		}
124

    
125
		/// <summary>
126
		/// Creates subdirectories for all data types
127
		/// TBD if we want to do it this way
128
		/// </summary>
129
		private void CreateSubdirectories()
130
		{
131
			foreach (DataType type in (DataType[])Enum.GetValues(typeof(DataType)))
132
			{
133
				string subDirectory = RootDataDirectory + sep + type;
134
				DirectoryInfo di = Directory.CreateDirectory(subDirectory);
135

    
136
				// create subdirectory record if it doesn't exist:
137
				if (!DataSubDirectories.ContainsKey(type))
138
					DataSubDirectories.Add(type, Path.GetFullPath(subDirectory));
139
			}
140
		}
141

    
142
		/// <summary>
143
		/// Parses the naming convention to be later used for determining URLs for data download
144
		/// </summary>
145
		/// <param name="namingConvention">The configured naming convention</param>
146
		private void ParseNaming(string namingConvention)
147
		{
148
			separatedFileName = new List<string>();
149
			variablesInsertions = new List<string>();
150
			nameTurns = new List<char>();
151

    
152
			string currPart = "";
153
			string currVar = "";
154
			bool readingNormal = true;
155
			foreach (char c in namingConvention)
156
			{
157
				if (c == '{')
158
				{
159
					AddToNameParts(currPart);
160
					readingNormal = false;
161
					currPart = "";
162
				}
163
				else if (c == '}')
164
				{
165
					AddToVariables(currVar);
166
					readingNormal = true;
167
					currVar = "";
168
				}
169
				else
170
				{
171
					// normal char
172
					if (readingNormal)
173
						currPart += c;
174
					else
175
						currVar += c;
176
				}
177
			}
178

    
179
			// add the rest if there is any:
180
			if (readingNormal)
181
				AddToNameParts(currPart);
182
			else
183
				AddToVariables(currVar);
184

    
185
			//Console.WriteLine();
186
		}
187

    
188
		// Adds to name parts
189
		private void AddToNameParts(string s)
190
		{
191
			if (s.Length > 0)
192
			{
193
				separatedFileName.Add(s);
194
				nameTurns.Add('n');
195
			}
196
				
197
		}
198

    
199
		// Adds to variable name parts
200
		private void AddToVariables(string s)
201
		{
202
			if (s.Length > 0)
203
			{
204
				variablesInsertions.Add(s);
205
				nameTurns.Add('v');
206
			}
207
				
208
		}
209

    
210
		/// <summary>
211
		/// Builds the name of the downloaded file. Takes naming convention into account
212
		/// </summary>
213
		/// <param name="type">The type of data</param>
214
		/// <param name="format">The data format</param>
215
		/// <param name="year">The year</param>
216
		/// <param name="month">The month</param>
217
		/// <returns></returns>
218
		private string BuildDownloadedName(DataType type, DataFormat format, int year, int month)
219
		{
220
			string nameZip = "";
221

    
222
			int partInd = 0;
223
			int varInd = 0;
224
			for(int i = 0; i < nameTurns.Count; i++)
225
			{
226
				if (nameTurns[i] == 'n')
227
				{
228
					nameZip += separatedFileName[partInd];
229
					partInd++;
230
				}
231
				else if(nameTurns[i] == 'v')
232
				{
233
					string add = "";
234
					switch (variablesInsertions[varInd])
235
					{
236
						case "type":
237
							add = "" + type;
238
							break;
239
						case "month": 
240
							add = month < 10 ? "0" + month : "" + month;
241
							break;
242
						case "year":
243
							add = "" + year;
244
							break;
245
						case "format":
246
							add = "" + format;
247
							break;
248
						default: throw new Exception("Config file error - naming conventions can only contain variables with the following names: type, month, year, format");
249
					}
250
					nameZip += add;
251
					varInd++;
252
				}
253
			}
254

    
255
			return nameZip;
256
		}
257

    
258

    
259
		/// <summary>
260
		/// Downloads a specific archive.
261
		/// </summary>
262
		/// <param name="type">The type of data</param>
263
		/// <param name="format">The format of the data</param>
264
		/// <param name="year">The year</param>
265
		/// <param name="month">The month</param>
266
		/// <returns>A list of all extracted file names (should be only one)</returns>
267
		private List<string> DownloadData(DataType type, DataFormat format, int year, int month)
268
		{
269
			// the list of all files potentially relevant to the caller
270
			List<string> extractedFiles = new List<string>();
271

    
272
			// Prepare the url string to be downloaded from:
273
			string monthStr = month < 10 ? "0" + month : "" + month;
274
			string yearStr = "" + year;
275
			string monthYr = monthStr + "_" + yearStr;
276

    
277

    
278
			string nameZip = BuildDownloadedName(type, format, year, month);//dataStr + type + "_" + monthYr + "_" + format + ".zip";
279
			string url = site + "/" + dataStr + monthYr + "/" + nameZip;//+ dataStr + type + "_" + monthYr + "_" + format + ".zip";
280
			
281
			string nameFolder = RootDataDirectory + sep + type + sep; //+ dataStr + type + "_" + monthYr + "_" + format;
282

    
283
			try
284
			{
285
				//Console.WriteLine("Downloading .zip to " + Path.GetFullPath(nameZip) + "...");
286
				logger.Info("Trying to download " + url);
287

    
288
				// Download the zip file:
289
				webClient.DownloadFile(url, nameZip);
290

    
291
				//ZipFile.ExtractToDirectory(nameZip, nameFolder);
292
				ZipArchive zipArchive = ZipFile.OpenRead(nameZip);
293
				// Go through all the extracted files:
294
				foreach (ZipArchiveEntry entry in zipArchive.Entries)
295
				{
296
					// get the relative path to the file:
297
					string newFileName = $"{month}-{year}.{format}";
298
					string extractedFile = nameFolder + newFileName; //+ entry.Name;
299

    
300
					// add full path to the list:
301
					extractedFiles.Add(Path.GetFullPath(extractedFile));
302

    
303
					if (OverwriteExisting)
304
					{
305
						// if overwrite is desired, execute it:
306
						entry.ExtractToFile(extractedFile, OverwriteExisting);
307
						
308
					}
309
					else
310
					{
311
						// if overwrite is not desired, check if the file exists first:
312
						if(File.Exists(extractedFile/*nameFolder + entry.Name*/))
313
						{
314
							continue;
315
						}
316
						else
317
						{
318
							// if it doesn't exist, save it:
319
							entry.ExtractToFile(extractedFile, OverwriteExisting);
320
						}
321
					}
322

    
323
					
324
				}
325
				// dispose of the archive:
326
				zipArchive.Dispose();
327

    
328
				//Console.WriteLine("Extracted to " + Path.GetFullPath(nameFolder));
329
				//Console.WriteLine("Deleting .zip from " + Path.GetFullPath(nameZip) + "...");
330
				//Console.WriteLine("Finished downloading " + nameZip);
331

    
332
				// delete the previously downloaded zip file, files contained in it have been extracted:
333
				File.Delete(nameZip); // todo check?
334

    
335
			}
336
			catch(System.Net.WebException we)
337
			{
338
				// download fails, if the specified url is invalid
339
				logger.Info("Download of " + url + " was not succesful. That is no problem if the given time span does not contain respective open data.");
340
				logger.Info(we.Message);
341
				
342
			}
343

    
344

    
345

    
346
			return extractedFiles;
347
		}
348

    
349

    
350

    
351
		/// <summary>
352
		/// Downloads selected type and time span of data in the desired format, returns a list of full paths to all successfully saved files. 
353
		/// If some of the files already existed and were not overwritten, then the returned List contains paths to these files also.
354
		/// </summary>
355
		/// <param name="type">The requested data type</param>
356
		/// <param name="format">The data format</param>
357
		/// <param name="startDate">The start date</param>
358
		/// <param name="endDate">The end date</param>
359
		/// <returns>A list of full paths to all saved files</returns>
360
		public List<string> DownloadData(DataType type, DataFormat format, Date startDate, Date endDate)
361
		{
362
			if (startDate > endDate)
363
				throw new ArgumentException("startDate must be the same as or before the endDate.");
364

    
365
			// initialize:
366
			List<string> savedFiles = new List<string>();
367
			//string subDirectory = RootDataDirectory + sep + type;
368
			//DirectoryInfo di = Directory.CreateDirectory(subDirectory);
369

    
370
			//// create subdirectory record if it doesn't exist:
371
			//if (!DataSubDirectories.ContainsKey(type))
372
			//	DataSubDirectories.Add(type, Path.GetFullPath(subDirectory));
373

    
374

    
375
			Date currentDate = startDate;
376
			bool firstLoop = true;
377
			do
378
			{
379
				//Console.WriteLine("current date: " + currentDate);
380
				savedFiles.AddRange(DownloadData(type, format, (int)currentDate.Year, (int)currentDate.Month));
381
				Date nextDate = currentDate.IncreaseMonthByOne();
382

    
383
				// also try to find the 00 file for each year:
384
				if(nextDate.Year > currentDate.Year || firstLoop)
385
				{
386
					savedFiles.AddRange(DownloadData(type, format, (int)currentDate.Year, 0));
387
					if (firstLoop)
388
						firstLoop = false; // so that we don't download the same thing all the time
389
					
390
				}
391

    
392
				// assign the increased date to the current date:
393
				currentDate = nextDate;
394

    
395

    
396
			} while (currentDate <= endDate);
397

    
398

    
399
			
400

    
401
			//for (int y = startYear; y <= endYear; y++)
402
			//{
403
			//	for (int m = startMonth; m <= endMonth; m++)
404
			//	{
405
			//		savedFiles.AddRange(DownloadData(type, format, y, m));
406
			//	}
407
			//}
408

    
409
			return savedFiles;
410
		}
411

    
412

    
413
		/// <summary>
414
		/// Retrieves all data files with dates falling within the specified range. If not all data for the specified range is found
415
		/// then returns also file/s with month 0 if exists.
416
		/// If startDate and/or endDate are null, then returns all files from the given subdirectory.
417
		/// </summary>
418
		/// <param name="subDirectory">The subdirectory to search</param>
419
		/// <param name="startDate">The start date</param>
420
		/// <param name="endDate">The end date</param>
421
		/// <returns>A list of all retrieved data files from the requested time span</returns>
422
		public List<string> GetData(string subDirectory, Date startDate, Date endDate)
423
		{
424
			if (startDate == null || endDate == null)
425
				return GetData(subDirectory);
426

    
427
			string[] files = Directory.GetFiles(subDirectory);
428
			List<string> found00Files = new List<string>();
429
			List<string> relevantFiles = new List<string>();
430
			List<Date> requestedDates = new List<Date>();
431

    
432
			// prepare a list of requested dates:
433
			Date currentDate = startDate;
434
			do
435
			{
436
				requestedDates.Add(currentDate);
437
				Date nextDate = currentDate.IncreaseMonthByOne();
438
				// assign the increased date to the current date:
439
				currentDate = nextDate;
440

    
441
			} while (currentDate <= endDate);
442

    
443

    
444

    
445
			for (int i = 0; i < files.Length; i++)
446
			{
447
				string currFileName = Path.GetFileName(files[i]);
448
				//Console.WriteLine("curr file: " + currFileName);
449
				string[] splits = currFileName.Split(new char[] { '-', '.' });
450

    
451
				int month = int.Parse(splits[0]);
452
				int year = int.Parse(splits[1]);
453

    
454

    
455
				
456
				if (month == 0)
457
				{
458
					found00Files.Add(files[i]);
459
					continue;
460
				}
461

    
462
				Date d = new Date((uint)month, (uint)year);
463

    
464
				if (d >= startDate && d <= endDate)
465
				{
466
					// we want to add this
467
					relevantFiles.Add(files[i]);
468
					requestedDates.Remove(d);
469
				}
470
			}
471

    
472

    
473

    
474

    
475
			// 00 will only appear once for every year?
476
			foreach (string file00 in found00Files)
477
			{
478
				string fileName = Path.GetFileName(file00);
479
				string[] splits = fileName.Split(new char[] { '-', '.' });
480

    
481
				int month = int.Parse(splits[0]);
482
				int year = int.Parse(splits[1]);
483

    
484
				// now we have the year of one 00 file
485
				// dates not found in the directory now remain in requested dates
486
				if(requestedDates.Exists(d => d.Year == year))
487
				{
488
					// if the year of this 00 file remains in the list:
489
					relevantFiles.Add(file00);
490
				}
491
			}
492

    
493

    
494
			return relevantFiles;
495
		}
496

    
497
		/// <summary>
498
		/// Returns all file paths in a given directory
499
		/// </summary>
500
		/// <param name="subDirectory"></param>
501
		/// <returns></returns>
502
		private List<string> GetData(string subDirectory)
503
		{
504
			string[] files = Directory.GetFiles(subDirectory);
505
			List<string> relevantFiles = new List<string>(files.Length);
506

    
507
			for (int i = 0; i < files.Length; i++)
508
				relevantFiles.Add(files[i]);
509

    
510
			return relevantFiles;
511
		}
512
	}
513
}
(1-1/3)