Reputation: 119
In the top of class
public static List<string> countriescodes = new List<string>();
public static List<string> countriesnames = new List<string>();
public static List<List<string>> imagesUrls = new List<List<string>>();
In class constructor
public void Init()
{
if (!File.Exists(@"c:\temp\countriesandcodes.txt"))
{
w = new StreamWriter(@"c:\temp\countriesandcodes.txt");
}
else
{
lines = File.ReadAllLines(@"c:\temp\countriesandcodes.txt");
}
foreach (string countrycode in lines)
{
if (countrycode.Contains("Code"))
{
string code = countrycode.Substring(15);
countriescodes.Add(code);
}
else
{
string code = countrycode.Substring(15);
countriesnames.Add(code);
}
}
foreach (string cc in countriescodes)
{
ExtractDateAndTime("http://www.sat24.com/image2.ashx?region=" + cc);
}
ImagesLinks();
}
Then in ImagesLinks
public void ImagesLinks()
{
int cnt = 0;
foreach (string countryCode in countriescodes)
{
cnt++;
for (; cnt < DatesAndTimes.Count(); cnt++)
{
string imageUrlIrTrue = firstUrlPart + countryCode + secondUrlPart + DatesAndTimes[cnt] + thirdUrlPart + "true";
string imageUrlIrFalse = firstUrlPart + countryCode + secondUrlPart + DatesAndTimes[cnt] + thirdUrlPart + "false";
imagesUrls.Add(imageUrlIrTrue);
imagesUrls.Add(imageUrlIrFalse);
if (cnt % 10 == 0) break;
}
}
}
In the imagesUrls
I want to add each countryName
to a List.
For example in imagesUrls
I will have the first List in this format: The first item in each List will be the country name and then to build the links from the countries codes list of each country name.
Turkey
link1
link2
.
.
.
link9
Then ext Lis
t in imagesUrls
will be
Europe
link1
link2
.
.
.
link9
What I want is to create Lists in imagesUrl
s and each List will contain at the top first the country name from the countriesnames
List then the links for this country using the countriescodes
List using the imageUrlIrTrue
and imageUrlIrFalse
Then in Form1
I want to read each List
and the items of each List
.
This is a part of the file countriesandcodes.txt to see how it's formatted: What i want to do in general is to use this class in form1 so i can easy get each country and it's all links using the both lists countriescodes and countriesnames.
Country Code = eu
Country Name = Europe
Country Code = alps
Country Name = Alps
Country Code = nl
Country Name = Benelux
Country Code = de
Country Name = Germany
Country Code = sp
Country Name = Spain & Portugal
Country Code = fr
Country Name = France
This is the complete code of the class. Maybe it will be easier to look at it like this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Xml;
using HtmlAgilityPack;
using System.ComponentModel;
namespace SatelliteImages
{
class ExtractImages
{
static WebClient client;
static string htmltoextract;
public static List<string> countriescodes = new List<string>();
public static List<string> countriesnames = new List<string>();
public static List<string> DatesAndTimes = new List<string>();
public static List<List<string>> imagesUrls = new List<List<string>>();
static string firstUrlPart = "http://www.sat24.com/image2.ashx?region=";
static string secondUrlPart = "&time=";
static string thirdUrlPart = "&ir=";
StreamWriter w;
string[] lines;
public bool WebProblem = false;
public void Init()
{
if (!File.Exists(@"c:\temp\countriesandcodes.txt"))
{
w = new StreamWriter(@"c:\temp\countriesandcodes.txt");
}
else
{
lines = File.ReadAllLines(@"c:\temp\countriesandcodes.txt");
}
foreach (string countrycode in lines)
{
if (countrycode.Contains("Code"))
{
string code = countrycode.Substring(15);
countriescodes.Add(code);
}
else
{
string code = countrycode.Substring(15);
countriesnames.Add(code);
}
}
foreach (string cc in countriescodes)
{
ExtractDateAndTime("http://www.sat24.com/image2.ashx?region=" + cc);
}
ImagesLinks();
}
public void ExtractCountires()
{
try
{
htmltoextract = "http://sat24.com/en/?ir=true";//"http://sat24.com/en/";// + regions;
client = new WebClient();
client.DownloadFile(htmltoextract, @"c:\temp\sat24.html");
client.Dispose();
string tag1 = "<li><a href=\"/en/";
string tag2 = "</a></li>";
string s = System.IO.File.ReadAllText(@"c:\temp\sat24.html");
s = s.Substring(s.IndexOf(tag1));
s = s.Substring(0, s.LastIndexOf(tag2) + tag2.ToCharArray().Length);
s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
string[] parts = s.Split(new string[] { tag1, tag2 }, StringSplitOptions.RemoveEmptyEntries);
string tag3 = "<li><ahref=\"/en/";
for (int i = 0; i < parts.Length; i++)
{
if (i == 40)
{
break;
}
string l = "";
if (parts[i].Contains(tag3))
l = parts[i].Replace(tag3, "");
if (i == 39)
{
string fff = "";
}
string z1 = l.Substring(0, l.IndexOf('"'));
if (z1.Contains("</ul></li><liclass="))
{
z1 = z1.Replace("</ul></li><liclass=", "af");
}
countriescodes.Add(z1);
countriescodes.GroupBy(n => n).Any(c => c.Count() > 1);
string z2 = parts[i].Substring(parts[i].LastIndexOf('>') + 1);
if (z2.Contains("&"))
{
z2 = z2.Replace("&", " & ");
}
countriesnames.Add(z2);
countriesnames.GroupBy(n => n).Any(c => c.Count() > 1);
}
for (int i = 0; i < countriescodes.Count; i++)
{
w.WriteLine("Country Code = " + countriescodes[i]);
w.WriteLine("Country Name = " + countriesnames[i]);
}
w.Close();
}
catch (Exception e)
{
if (countriescodes.Count == 0)
{
/*countriescodes = new List<string>();
countriesnames = new List<string>();
DatesAndTimes = new List<string>();
imagesUrls = new List<string>();
Init();*/
}
}
}
public void ExtractDateAndTime(string baseAddress)
{
try
{
var wc = new WebClient();
wc.BaseAddress = baseAddress;
HtmlDocument doc = new HtmlDocument();
var temp = wc.DownloadData("/en");
doc.Load(new MemoryStream(temp));
var secTokenScript = doc.DocumentNode.Descendants()
.Where(e =>
String.Compare(e.Name, "script", true) == 0 &&
String.Compare(e.ParentNode.Name, "div", true) == 0 &&
e.InnerText.Length > 0 &&
e.InnerText.Trim().StartsWith("var region")
).FirstOrDefault().InnerText;
var securityToken = secTokenScript;
securityToken = securityToken.Substring(0, securityToken.IndexOf("arrayImageTimes.push"));
securityToken = secTokenScript.Substring(securityToken.Length).Replace("arrayImageTimes.push('", "").Replace("')", "");
var dates = securityToken.Trim().Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
var scriptDates = dates.Select(x => new ScriptDate { DateString = x });
foreach (var date in scriptDates)
{
DatesAndTimes.Add(date.DateString);
}
}
catch(WebException wex)
{
WebProblem = true;
}
}
public class ScriptDate
{
public string DateString { get; set; }
public int Year
{
get
{
return Convert.ToInt32(this.DateString.Substring(0, 4));
}
}
public int Month
{
get
{
return Convert.ToInt32(this.DateString.Substring(4, 2));
}
}
public int Day
{
get
{
return Convert.ToInt32(this.DateString.Substring(6, 2));
}
}
public int Hours
{
get
{
return Convert.ToInt32(this.DateString.Substring(8, 2));
}
}
public int Minutes
{
get
{
return Convert.ToInt32(this.DateString.Substring(10, 2));
}
}
}
public void ImagesLinks()
{
int cnt = 0;
foreach (string countryCode in countriescodes)
{
cnt++;
for (; cnt < DatesAndTimes.Count(); cnt++)
{
string imageUrlIrTrue = firstUrlPart + countryCode + secondUrlPart + DatesAndTimes[cnt] + thirdUrlPart + "true";
string imageUrlIrFalse = firstUrlPart + countryCode + secondUrlPart + DatesAndTimes[cnt] + thirdUrlPart + "false";
imagesUrls.Add(imageUrlIrTrue);
imagesUrls.Add(imageUrlIrFalse);
if (cnt % 10 == 0) break;
}
}
}
}
}
I tried to add a class now to the class
public class Country
{
public List<Tuple<Uri, bool>> URLList { get; set; }
public string Code { get; set; }
}
Not sure how to use the property Code. And i added a bool to the List since i want to make that when i loop over the list in form1 i will have links of true and false like i did in the ImagesLinks method:
string imageUrlIrTrue = firstUrlPart + countryCode + secondUrlPart + DatesAndTimes[cnt] + thirdUrlPart + "true";
string imageUrlIrFalse = firstUrlPart + countryCode + secondUrlPart + DatesAndTimes[cnt] + thirdUrlPart + "false";
But maybe instead using bool in the List and make it Tuple i should change it back to List and use the bool in other way to build the links.
In general i want that in form1 i will be able to get to 3 parameters:
The images links.
The countries codes.
The links should be true and false not bool but as part of the link for example:
imageUrlIrTrue:
http://www.sat24.com/image2.ashx?region=is&time=201701161900&ir=true
And false:
http://www.sat24.com/image2.ashx?region=is&time=201701161900&ir=false
And each country have 9 links built of the country code and date and time. So in form1 i should have something like a List with Lists. For example the class Country should have a property that if i type for example in form1: Country.Country.Turkey
And then i should i have somehow also the 9 links of turkey and the code of turkey.
It's not only to get countries and codes but also each 9 links fro each country in fact 18 links each country since each link is double true/false.
So each country have 18 links !!! and i should be able to access them in a loop in form1 and also to access the countries codes and names.
So in form1 i should have a loop that i can easy get the countries names get each country 18 links( true and false ) and each country code !!!
Upvotes: 1
Views: 118
Reputation: 2503
What I want is to create Lists in imagesUrls and each List will contain at the top first the country name from the countriesnames List then the links for this country using the countriescodes List using the imageUrlIrTrue and imageUrlIrFalse
Not sure what does "What I want is to create Lists in imagesUrls" actually means.
My question is why do you want do this? What you really need to do is use first programming, except you will need to think what classes you need.
Here is an example : Let's say you have a list of image url's for each country
class Country
{
List<URL> URLList;
}
also country has a country code
class Country
{
public List<URL> URLList{get;set;}
public string Code {get;set;}
}
Now you can grab a country and loop trough it's url's. Try to use some classes and modify your question, ping me on update and I'll modify this answer to match the question.
Upvotes: 1
Reputation: 693
This may not technically be the answer, but the better approach here is to create a class called "Country" and give it properties for url, country name, etc. Then you only need to create a list(of country) and populate each.
**Note to Daniel d e junior (I will edit this with a code example if you can provide me with what countriesandcodes.txt looks like)
Upvotes: 3