static List<string> getListing(string url)
{
string name = new Uri(url).AbsolutePath;
name = Path.GetFileName(name.TrimEnd('/'));
List<string> list = new List<string>();
if (File.Exists(datadirectory + Path.DirectorySeparatorChar + name))
{
var fi = new FileInfo(datadirectory + Path.DirectorySeparatorChar + name);
if (fi.Length > 0 && fi.LastWriteTime.AddDays(7) > DateTime.Now)
{
using (StreamReader sr = new StreamReader(datadirectory + Path.DirectorySeparatorChar + name))
{
while (!sr.EndOfStream)
{
list.Add(sr.ReadLine());
}
sr.Close();
}
return list;
}
}
try
{
log.Info("srtm req " + url);
WebRequest req = HttpWebRequest.Create(url);
using (WebResponse res = req.GetResponse())
using (StreamReader resstream = new StreamReader(res.GetResponseStream()))
{
string data = resstream.ReadToEnd();
Regex regex = new Regex("href=\"([^\"]+)\"", RegexOptions.IgnoreCase);
if (regex.IsMatch(data))
{
MatchCollection matchs = regex.Matches(data);
for (int i = 0; i < matchs.Count; i++)
{
if (matchs[i].Groups[1].Value.ToString().Contains(".."))
continue;
if (matchs[i].Groups[1].Value.ToString().Contains("http"))
continue;
if (matchs[i].Groups[1].Value.ToString().EndsWith("/srtm/version2_1/"))
continue;
list.Add(url.TrimEnd(new char[] {'/', '\\'}) + "/" + matchs[i].Groups[1].Value.ToString());
}
}
}
using (StreamWriter sw = new StreamWriter(datadirectory + Path.DirectorySeparatorChar + name))
{
list.ForEach(x =>
{
sw.WriteLine((string) x);
});
if (name.Equals("README.txt") || name.Equals("Region_definition.jpg"))
sw.Write(" ");
sw.Close();
}
}
catch (WebException ex)
{
log.Error(ex);
throw;
}
return list;
}