Revize 7a998d66
Přidáno uživatelem Eliška Mourycová před více než 3 roky(ů)
Server/ServerApp/DataDownload/DataDownloader.cs | ||
---|---|---|
8 | 8 |
using System.IO.Compression; |
9 | 9 |
using System.Net; |
10 | 10 |
|
11 |
namespace DataDownload |
|
11 |
namespace ServerApp.DataDownload
|
|
12 | 12 |
{ |
13 | 13 |
/// <summary> |
14 | 14 |
/// Enum representing all of the available data types (not all will be used in this project) |
... | ... | |
28 | 28 |
XML, JSON, CSV |
29 | 29 |
} |
30 | 30 |
|
31 |
public enum TimeSpan |
|
32 |
{ |
|
33 |
|
|
34 |
} |
|
35 |
|
|
31 | 36 |
/// <summary> |
32 | 37 |
/// This class takes care of downloading of data. Download happens from http://openstore.zcu.cz/. |
33 | 38 |
/// </summary> |
... | ... | |
36 | 41 |
/// <summary> |
37 | 42 |
/// The root directory containing all downloaded data |
38 | 43 |
/// </summary> |
39 |
public string DataDirectory { get; } |
|
44 |
public string RootDataDirectory { get; } |
|
45 |
|
|
46 |
/// <summary> |
|
47 |
/// For a DataType key returns full (absolute) path to a direcotry, where this type of data is stored |
|
48 |
/// </summary> |
|
49 |
public Dictionary<DataType, string> DataSubDirectories { get; } |
|
40 | 50 |
|
41 | 51 |
/// <summary> |
42 | 52 |
/// Flag stating whether files which already exist should be overwritten when downloaded again |
... | ... | |
55 | 65 |
// a shortcut to writing Path.DirectorySeparatorChar |
56 | 66 |
private char sep = Path.DirectorySeparatorChar; |
57 | 67 |
|
58 |
public DataDownloader()
|
|
68 |
public DataDownloader(string rootDataDir, string website, string namingConvention) // todo take naming conventons specifiaction into account
|
|
59 | 69 |
{ |
60 | 70 |
// initialize all needed variables: |
61 | 71 |
|
72 |
DataSubDirectories = new Dictionary<DataType, string>(); |
|
73 |
|
|
62 | 74 |
Console.WriteLine(Directory.GetCurrentDirectory()); |
63 |
site = "http://openstore.zcu.cz/"; |
|
75 |
site = website;//"http://openstore.zcu.cz/";
|
|
64 | 76 |
dataStr = "OD_ZCU_"; |
65 | 77 |
|
66 |
DataDirectory = $"..{sep}..{sep}data{sep}auto";
|
|
78 |
RootDataDirectory = rootDataDir;//$"..{sep}..{sep}..{sep}data{sep}auto";
|
|
67 | 79 |
OverwriteExisting = false; |
68 | 80 |
|
69 | 81 |
webClient = new WebClient(); |
... | ... | |
89 | 101 |
string monthYr = monthStr + "_" + yearStr; |
90 | 102 |
string url = site + "/" + dataStr + monthYr + "/" + dataStr + type + "_" + monthYr + "_" + format + ".zip"; |
91 | 103 |
string nameZip = dataStr + type + "_" + monthYr + "_" + format + ".zip"; |
92 |
string nameFolder = DataDirectory + sep + type + sep; //+ dataStr + type + "_" + monthYr + "_" + format; |
|
104 |
string nameFolder = RootDataDirectory + sep + type + sep; //+ dataStr + type + "_" + monthYr + "_" + format;
|
|
93 | 105 |
|
94 | 106 |
try |
95 | 107 |
{ |
96 |
Console.WriteLine("Downloading .zip to " + Path.GetFullPath(nameZip) + "..."); |
|
108 |
//Console.WriteLine("Downloading .zip to " + Path.GetFullPath(nameZip) + "...");
|
|
97 | 109 |
|
98 | 110 |
// Download the zip file: |
99 | 111 |
webClient.DownloadFile(url, nameZip); |
... | ... | |
134 | 146 |
// dispose of the archive: |
135 | 147 |
zipArchive.Dispose(); |
136 | 148 |
|
137 |
Console.WriteLine("Extracted to " + Path.GetFullPath(nameFolder)); |
|
138 |
Console.WriteLine("Deleting .zip from " + Path.GetFullPath(nameZip) + "..."); |
|
139 |
Console.WriteLine("Finished downloading " + nameZip); |
|
149 |
//Console.WriteLine("Extracted to " + Path.GetFullPath(nameFolder));
|
|
150 |
//Console.WriteLine("Deleting .zip from " + Path.GetFullPath(nameZip) + "...");
|
|
151 |
//Console.WriteLine("Finished downloading " + nameZip);
|
|
140 | 152 |
|
141 | 153 |
// delete the previously downloaded zip file, files contained in it have been extracted: |
142 | 154 |
File.Delete(nameZip); // todo check? |
... | ... | |
145 | 157 |
catch(System.Net.WebException we) |
146 | 158 |
{ |
147 | 159 |
// download fails, if the specified url is invalid |
148 |
Console.WriteLine("Download from " + url + " failed."); |
|
149 |
Console.WriteLine(we.Message); |
|
160 |
//Console.WriteLine("Download from " + url + " failed.");
|
|
161 |
//Console.WriteLine(we.Message);
|
|
150 | 162 |
} |
151 | 163 |
|
152 | 164 |
|
... | ... | |
160 | 172 |
/// Downloads selected type and time span of data in the desired format, returns a list of full paths to all successfully saved files. |
161 | 173 |
/// If some of the files already existed and were not overwritten, then the returned List contains paths to these files also. |
162 | 174 |
/// </summary> |
163 |
/// <param name="type">The type of data, e.g. the jis data</param> |
|
164 |
/// <param name="format">The desired format of data files, available are CSV, XML and JSON formats</param> |
|
165 |
/// <param name="startYear">The start year to start the download from, inclusive</param> |
|
166 |
/// <param name="endYear">The end year to start the download from, inclusive</param> |
|
167 |
/// <param name="startMonth">The start month to start the download from, inclusive</param> |
|
168 |
/// <param name="endMonth">The end month to start the download from, inclusive</param> |
|
169 |
/// <returns>A list of full paths to all successfully saved files</returns> |
|
170 |
public List<string> DownloadData(DataType type, DataFormat format, int startYear, int endYear, int startMonth, int endMonth) |
|
175 |
public List<string> DownloadData(DataType type, DataFormat format, Date startDate, Date endDate/*int startYear, int endYear, int startMonth, int endMonth*/) |
|
171 | 176 |
{ |
177 |
if (startDate > endDate) |
|
178 |
throw new ArgumentException("startDate must be the same as or before the endDate."); |
|
179 |
|
|
180 |
// initialize: |
|
172 | 181 |
List<string> savedFiles = new List<string>(); |
173 |
DirectoryInfo di = Directory.CreateDirectory(DataDirectory + sep + type); |
|
182 |
string subDirectory = RootDataDirectory + sep + type; |
|
183 |
DirectoryInfo di = Directory.CreateDirectory(subDirectory); |
|
184 |
|
|
185 |
// create subdirectory record if it doesn't exist: |
|
186 |
if (!DataSubDirectories.ContainsKey(type)) |
|
187 |
DataSubDirectories.Add(type, Path.GetFullPath(subDirectory)); |
|
174 | 188 |
|
175 |
for (int y = startYear; y <= endYear; y++) |
|
189 |
|
|
190 |
Date currentDate = startDate; |
|
191 |
bool firstLoop = true; |
|
192 |
do |
|
176 | 193 |
{ |
177 |
for (int m = startMonth; m <= endMonth; m++) |
|
194 |
Console.WriteLine("current date: " + currentDate); |
|
195 |
savedFiles.AddRange(DownloadData(type, format, (int)currentDate.Year, (int)currentDate.Month)); |
|
196 |
Date nextDate = currentDate.IncreaseMonthByOne(); |
|
197 |
|
|
198 |
// also try to find the 00 file for each year: |
|
199 |
if(nextDate.Year > currentDate.Year || firstLoop) |
|
178 | 200 |
{ |
179 |
savedFiles.AddRange(DownloadData(type, format, y, m)); |
|
201 |
savedFiles.AddRange(DownloadData(type, format, (int)currentDate.Year, 0)); |
|
202 |
if (firstLoop) |
|
203 |
firstLoop = false; // so that we don't download the same thing all the time |
|
204 |
|
|
180 | 205 |
} |
181 |
} |
|
206 |
|
|
207 |
// assign the increased date to the current date: |
|
208 |
currentDate = nextDate; |
|
209 |
|
|
210 |
|
|
211 |
} while (currentDate != endDate); |
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
//for (int y = startYear; y <= endYear; y++) |
|
217 |
//{ |
|
218 |
// for (int m = startMonth; m <= endMonth; m++) |
|
219 |
// { |
|
220 |
// savedFiles.AddRange(DownloadData(type, format, y, m)); |
|
221 |
// } |
|
222 |
//} |
|
182 | 223 |
|
183 | 224 |
return savedFiles; |
184 | 225 |
} |
185 | 226 |
|
227 |
public bool CheckForNewData() |
|
228 |
{ |
|
229 |
throw new NotImplementedException(); |
|
230 |
} |
|
231 |
|
|
232 |
public List<string> GetData(string subDirectory, int startYear, int endYear, int startMonth, int endMonth) |
|
233 |
{ |
|
234 |
throw new NotImplementedException(); |
|
235 |
} |
|
236 |
|
|
186 | 237 |
|
187 | 238 |
#region UNUSED |
188 | 239 |
//public string GetDirectoryListingRegexForUrl(string url) |
Také k dispozici: Unified diff
Re #8691. Big refactor + config file added + working on providing methods for Parser.