Revize 55d35561
Přidáno uživatelem Eliška Mourycová před téměř 4 roky(ů)
Server/ServerApp/DataDownload/DataDownloader.cs | ||
---|---|---|
60 | 60 |
// a shortcut to writing Path.DirectorySeparatorChar |
61 | 61 |
private char sep = Path.DirectorySeparatorChar; |
62 | 62 |
|
63 |
// lists used for parsing the file names: |
|
63 | 64 |
private List<string> separatedFileName; |
64 | 65 |
private List<string> variablesInsertions; |
65 | 66 |
private List<char> nameTurns; |
... | ... | |
68 | 69 |
{ |
69 | 70 |
// initialize all needed variables: |
70 | 71 |
|
71 |
DataSubDirectories = new Dictionary<DataType, string>(); |
|
72 |
|
|
73 | 72 |
//Console.WriteLine(Directory.GetCurrentDirectory()); |
74 | 73 |
site = website;//"http://openstore.zcu.cz/"; |
75 | 74 |
|
... | ... | |
79 | 78 |
RootDataDirectory = rootDataDir;//$"..{sep}..{sep}..{sep}data{sep}auto"; |
80 | 79 |
OverwriteExisting = false; |
81 | 80 |
|
81 |
DataSubDirectories = new Dictionary<DataType, string>(); |
|
82 |
CreateSubdirectories(); // todo should we do it like this? |
|
83 |
|
|
82 | 84 |
webClient = new WebClient(); |
83 | 85 |
} |
84 | 86 |
|
87 |
/// <summary> |
|
88 |
/// Creates subdirectories for all data types |
|
89 |
/// TBD if we want to do it this way |
|
90 |
/// </summary> |
|
91 |
private void CreateSubdirectories() |
|
92 |
{ |
|
93 |
foreach (DataType type in (DataType[])Enum.GetValues(typeof(DataType))) |
|
94 |
{ |
|
95 |
string subDirectory = RootDataDirectory + sep + type; |
|
96 |
DirectoryInfo di = Directory.CreateDirectory(subDirectory); |
|
97 |
|
|
98 |
// create subdirectory record if it doesn't exist: |
|
99 |
if (!DataSubDirectories.ContainsKey(type)) |
|
100 |
DataSubDirectories.Add(type, Path.GetFullPath(subDirectory)); |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
/// <summary> |
|
105 |
/// Parses the naming convention to be later used for determining URLs for data download |
|
106 |
/// </summary> |
|
107 |
/// <param name="namingConvention">The configured naming convention</param> |
|
85 | 108 |
private void ParseNaming(string namingConvention) |
86 | 109 |
{ |
87 | 110 |
separatedFileName = new List<string>(); |
... | ... | |
124 | 147 |
Console.WriteLine(); |
125 | 148 |
} |
126 | 149 |
|
150 |
// Adds to name parts |
|
127 | 151 |
private void AddToNameParts(string s) |
128 | 152 |
{ |
129 | 153 |
if (s.Length > 0) |
... | ... | |
134 | 158 |
|
135 | 159 |
} |
136 | 160 |
|
161 |
// Adds to variable name parts |
|
137 | 162 |
private void AddToVariables(string s) |
138 | 163 |
{ |
139 | 164 |
if (s.Length > 0) |
... | ... | |
144 | 169 |
|
145 | 170 |
} |
146 | 171 |
|
147 |
|
|
172 |
/// <summary> |
|
173 |
/// Builds the name of the downloaded file. Takes naming convention into account |
|
174 |
/// </summary> |
|
175 |
/// <param name="type">The type of data</param> |
|
176 |
/// <param name="format">The data format</param> |
|
177 |
/// <param name="year">The year</param> |
|
178 |
/// <param name="month">The month</param> |
|
179 |
/// <returns></returns> |
|
148 | 180 |
private string BuildDownloadedName(DataType type, DataFormat format, int year, int month) |
149 | 181 |
{ |
150 | 182 |
string nameZip = ""; |
... | ... | |
186 | 218 |
} |
187 | 219 |
|
188 | 220 |
|
189 |
private void ExtractDateFromFileName(string fileName, ref int month, ref int year) |
|
190 |
{ |
|
191 |
|
|
192 |
} |
|
193 |
|
|
194 | 221 |
/// <summary> |
195 | 222 |
/// Downloads a specific archive. |
196 | 223 |
/// </summary> |
... | ... | |
285 | 312 |
/// Downloads selected type and time span of data in the desired format, returns a list of full paths to all successfully saved files. |
286 | 313 |
/// If some of the files already existed and were not overwritten, then the returned List contains paths to these files also. |
287 | 314 |
/// </summary> |
315 |
/// <param name="type">The requested data type</param> |
|
316 |
/// <param name="format">The data format</param> |
|
317 |
/// <param name="startDate">The start date</param> |
|
318 |
/// <param name="endDate">The end date</param> |
|
319 |
/// <returns></returns> |
|
288 | 320 |
public List<string> DownloadData(DataType type, DataFormat format, Date startDate, Date endDate/*int startYear, int endYear, int startMonth, int endMonth*/) |
289 | 321 |
{ |
290 | 322 |
if (startDate > endDate) |
... | ... | |
292 | 324 |
|
293 | 325 |
// initialize: |
294 | 326 |
List<string> savedFiles = new List<string>(); |
295 |
string subDirectory = RootDataDirectory + sep + type; |
|
296 |
DirectoryInfo di = Directory.CreateDirectory(subDirectory); |
|
327 |
//string subDirectory = RootDataDirectory + sep + type;
|
|
328 |
//DirectoryInfo di = Directory.CreateDirectory(subDirectory);
|
|
297 | 329 |
|
298 |
// create subdirectory record if it doesn't exist: |
|
299 |
if (!DataSubDirectories.ContainsKey(type)) |
|
300 |
DataSubDirectories.Add(type, Path.GetFullPath(subDirectory)); |
|
330 |
//// create subdirectory record if it doesn't exist:
|
|
331 |
//if (!DataSubDirectories.ContainsKey(type))
|
|
332 |
// DataSubDirectories.Add(type, Path.GetFullPath(subDirectory));
|
|
301 | 333 |
|
302 | 334 |
|
303 | 335 |
Date currentDate = startDate; |
... | ... | |
343 | 375 |
} |
344 | 376 |
|
345 | 377 |
|
346 |
|
|
378 |
/// <summary> |
|
379 |
/// Retrieves all data files with dates falling within the specified range. If not all data for the specified range is found |
|
380 |
/// then returns also file/s with month 0 if exists. |
|
381 |
/// </summary> |
|
382 |
/// <param name="subDirectory">The subdirectory to search</param> |
|
383 |
/// <param name="startDate">The start date</param> |
|
384 |
/// <param name="endDate">The end date</param> |
|
385 |
/// <returns></returns> |
|
347 | 386 |
public List<string> GetData(string subDirectory, Date startDate, Date endDate) |
348 | 387 |
{ |
349 | 388 |
string[] files = Directory.GetFiles(subDirectory); |
350 |
for(int i = 0; i < files.Length; i++) |
|
389 |
List<string> found00Files = new List<string>(); |
|
390 |
List<string> relevantFiles = new List<string>(); |
|
391 |
List<Date> requestedDates = new List<Date>(); |
|
392 |
|
|
393 |
// prepare a list of requested dates: |
|
394 |
Date currentDate = startDate; |
|
395 |
do |
|
396 |
{ |
|
397 |
requestedDates.Add(currentDate); |
|
398 |
Date nextDate = currentDate.IncreaseMonthByOne(); |
|
399 |
// assign the increased date to the current date: |
|
400 |
currentDate = nextDate; |
|
401 |
|
|
402 |
} while (currentDate <= endDate); |
|
403 |
|
|
404 |
|
|
405 |
|
|
406 |
for (int i = 0; i < files.Length; i++) |
|
351 | 407 |
{ |
352 | 408 |
string currFileName = Path.GetFileName(files[i]); |
353 | 409 |
Console.WriteLine("curr file: " + currFileName); |
... | ... | |
357 | 413 |
int year = int.Parse(splits[1]); |
358 | 414 |
|
359 | 415 |
|
416 |
|
|
417 |
if (month == 0) |
|
418 |
{ |
|
419 |
found00Files.Add(files[i]); |
|
420 |
continue; |
|
421 |
} |
|
422 |
|
|
360 | 423 |
Date d = new Date((uint)month, (uint)year); |
361 | 424 |
|
425 |
if (d >= startDate && d <= endDate) |
|
426 |
{ |
|
427 |
// we want to add this |
|
428 |
relevantFiles.Add(files[i]); |
|
429 |
requestedDates.Remove(d); |
|
430 |
} |
|
431 |
} |
|
432 |
|
|
433 |
|
|
434 |
|
|
435 |
|
|
436 |
// 00 will only appear once for every year? |
|
437 |
foreach (string file00 in found00Files) |
|
438 |
{ |
|
439 |
string fileName = Path.GetFileName(file00); |
|
440 |
string[] splits = fileName.Split(new char[] { '-', '.' }); |
|
362 | 441 |
|
442 |
int month = int.Parse(splits[0]); |
|
443 |
int year = int.Parse(splits[1]); |
|
444 |
|
|
445 |
// now we have the year of one 00 file |
|
446 |
// dates not found in the directory now remain in requested dates |
|
447 |
if(requestedDates.Exists(d => d.Year == year)) |
|
448 |
{ |
|
449 |
// if the year of this 00 file remains in the list: |
|
450 |
relevantFiles.Add(file00); |
|
451 |
} |
|
363 | 452 |
} |
364 | 453 |
|
365 |
throw new NotImplementedException(); |
|
454 |
|
|
455 |
return relevantFiles; |
|
366 | 456 |
} |
367 | 457 |
|
368 | 458 |
|
Také k dispozici: Unified diff
Re #8691. Implemented downloaded data retrieving.