Version Update Code

Sep 29, 2010 at 3:41 PM
Edited Sep 29, 2010 at 4:44 PM

Here is my code for version update. A gift from me to you ;). I hope this saves you some trouble :). Of course Goooogle may change their javascript code to prevent us from updating... Great project!

		bool TryCorrectGoogleVersions()
		{
			string url = @"http://maps.google.com";
			try
			{
				HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
				if (Proxy != null)
				{
					request.Proxy = Proxy;
					request.PreAuthenticate = true;
				}
				else
				{
					request.Proxy = WebRequest.DefaultWebProxy;
				}
				request.UserAgent = UserAgent;
				request.Timeout = Timeout;
				request.ReadWriteTimeout = Timeout * 6;

				using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
				{
					using (Stream responseStream = response.GetResponseStream())
					{
						using (StreamReader read = new StreamReader(responseStream))
						{
							string html = read.ReadToEnd();

							Regex reg = new Regex("\"*http://mt0.google.com/vt/lyrs=m@(\\d*)", RegexOptions.IgnoreCase);
							Match mat = reg.Match(html);
							if(mat.Success)
							{
								GroupCollection gc = mat.Groups;
								int count = gc.Count;
								if(count > 0)
									VersionGoogleMap = string.Format("m@{0}", gc[1].Value);
							}

							reg = new Regex("\"*http://mt0.google.com/vt/lyrs=h@(\\d*)", RegexOptions.IgnoreCase);
							mat = reg.Match(html);
							if (mat.Success)
							{
								GroupCollection gc = mat.Groups;
								int count = gc.Count;
								if (count > 0)
									VersionGoogleLabels = string.Format("h@{0}", gc[1].Value);
							}

							reg = new Regex("\"*http://khm0.google.com/kh/v=(\\d*)", RegexOptions.IgnoreCase);
							mat = reg.Match(html);
							if (mat.Success)
							{
								GroupCollection gc = mat.Groups;
								int count = gc.Count;
								if (count > 0)
									VersionGoogleSatellite = gc[1].Value;
							}

							reg = new Regex("\"*http://mt0.google.com/vt/lyrs=t@(\\d*),r@(\\d*)", RegexOptions.IgnoreCase);
							mat = reg.Match(html);
							if (mat.Success)
							{
								GroupCollection gc = mat.Groups;
								int count = gc.Count;
								if (count > 1)
									VersionGoogleTerrain = string.Format("t@{0},r@{1}", gc[1].Value, gc[2].Value);
							}
						}
					}
				}
				VersionsUpdated = true;
				return true;
			}
			catch (WebException)
			{
				//...
			}
			catch (Exception ex)
			{
				ReportException(ex);
			}
			return false;
		}

(Updated to fix sth.)

Coordinator
Sep 29, 2010 at 3:45 PM

a gift for all of us, thx ;}

Sep 29, 2010 at 4:48 PM

I have updated the code above.

Coordinator
Sep 29, 2010 at 6:09 PM

commited

Sep 30, 2010 at 8:19 AM

For Bing Maps, it could be sth like this (http://www.bing.com/maps/):

Regex reg = new Regex("http://ecn.t(\\d*).tiles.virtualearth.net/tiles/r(\\d*)[?*]g=(\\d*)", RegexOptions.IgnoreCase);
Match mat = reg.Match(html);
if(mat.Success)
{
	GroupCollection gc = mat.Groups;
	int count = gc.Count;
	if(count > 2)
		VersionBingMap = gc[2].Value;
}

In the previous code (for Google Maps) it would be better if you advanced the check values by one, e.g.:

if (count > 0)  --> if (count > 1)

I've missed this one...

Coordinator
Sep 30, 2010 at 2:23 PM

hm.. but whats the difference in: if (count > 0) --> if (count > 1) ?

Oct 1, 2010 at 6:14 AM

Actually, I think that when "mat.Success==true" it doesn't matter anyway, since "count" will always be the desired value. But I tend to put more checks to be sure, so I have put this check to be sure that "gc[k]" always exists before accessing it...

Am I missing sth again ?

Coordinator
Oct 1, 2010 at 7:13 AM

i see, i've just wondered why you use gc[1] instead of gc[0] ?

Oct 1, 2010 at 9:04 AM

Because gc[0] contains the whole text match (e.g. "\"http://khm0.google.com/kh/v=71"). It's weird but that's the way it is...

Coordinator
Oct 1, 2010 at 9:29 AM

roger ;}