You are on page 1of 6

//Pointui C Script /* COPYRIGHT Copyright (c) 2008-2009 Pointui Pty Ltd, All Rights Reserved.

ABN: 80 129 073 678 This software is provided under license by Pointui Pty Ltd, and use ther eof is subject to the licensing terms. Distribution of this software is on an "AS IS" bas is, WITHOUT WARRANTY OF ANY KIND. This software is protected by Australian and international law. This sof tware program may not be reproduced, transmitted, or disclosed to third parties, in whole or in part, in any form or by a ny manner, electronic or mechanical, without the express written consent of Pointui Pty Ltd, except to the extent provide d for by applicable license. AccuweatherAPI Develope by BlueOcean */ //extend the default HttpFetch object so we can attach some more info to a reque st class AccuHttpFetch : HttpFetch { String CityID; //twitter url being fetched int Handle; //unique handle of this request } class AccuWeatherAPI { //Have to keep track of outstanding requests so they don't get garbage c ollected before they have finished Collection PendingRequests; //Collection PendingImageRequests; //Incrementing counter to identify a request int CurrentHandle; //Location where the cache files are stored String CacheLocation; Event OnComplete; Event OnFullComplete; #region Internal //provides the class an opportunity to initialize itself AccuWeatherAPI() { //OfflineMode = false; CurrentHandle = 0; SetCacheLocation("{Application}\\Cache\\AccuWeather"); } void SetCacheLocation(String folder) { //ensure the cache directory exists CacheLocation = folder; if (!File.Exists(CacheLocation)) {

Path.CreateDirectory(CacheLocation); } } void Update(String CityID, String unit) { //setup the request String s; s = CityID; s.Replace(" ", "%20"); String weatherRssUrl; weatherRssUrl = "http://vortex.accuweather.com/adcbin/netweather _v2/datav2.asp?zipcode="; weatherRssUrl += s; weatherRssUrl += "&metric="; weatherRssUrl += unit; AccuHttpFetch AcFetch; AcFetch.OnComplete = AcFetch_OnComplete; //keep the request in scope PendingRequests.Add(AcFetch); //start the request - NOTE: reqType is either "Get" or "Post" AcFetch.Start(weatherRssUrl); //save some info about this fetch to allow it to be looked up wh en the server responds AcFetch.CityID = CityID; AcFetch.Handle = CurrentHandle; CurrentHandle++; } //called when the twitter server responds to the request void AcFetch_OnComplete(HttpFetch* fetch) { String s; AccuHttpFetch* ptr; int ItemIndex, ItemCount; int x; //get count of pending requests ItemCount = PendingRequests.GetCount(); //try and find this request who just fired the OnComplete event off x = 0; while (x < ItemCount) { ptr = PendingRequests.Item(x); if (ptr == fetch) { ItemIndex = x; } x++; } ptr = PendingRequests.Item(ItemIndex); //save the response to the file system String fn = GetCacheFilename(ptr.CityID);

String s; ptr.GetContent(s); s=ChangeUpdateTime(s); File.Write(fn, s); //ptr.SaveResponseToFile(fn); //remove the http object so it can be garbage collected PendingRequests.Remove(ItemIndex); //fire the event to indicate a response has been received OnComplete(ptr.CityID, fn); //get count of pending requests ItemCount = PendingRequests.GetCount(); if (ItemCount==0) { OnFullComplete(); } } String ChangeUpdateTime(String ts) { String time,s; DateTime dt; dt.Now(); s = ts; time = dt.ToString("M/d/yyyy h:mm:ss tt"); int i, j,len; i = s.IndexOf("&ln="); j = s.IndexOf("&lrl="); i += 4; len=s.GetLength(); s = s.SubString(0, i) + time + s.SubString(j, len - j); return s; } String GetCacheData(String CityID) { //lookup the cache to see if a file exists String fn; fn = GetCacheFilename(CityID); String result; result=""; if (File.Exists(fn)) { File.Read(fn, result); } return result; } String GetCacheFilename(String CityID) { String result; result = "WeatherData_" + CityID + ".xml"; result.Replace(" ", "_"); result = Path.Combine(CacheLocation, result); return result; } String TempConvert(String cTemp, int unit)

{ String s; int t; if (unit == 1) { s = cTemp; } else { t = cTemp; t = (9 * t) / 5; t += 32; s = t; } return s; } String GetLastUpdate(String WeatherReport) { String result, s; DateTime dt,dtnow; int i, j, dayDiff; i = WeatherReport.IndexOf("&ln="); i += 4; j = WeatherReport.IndexOf(i, " "); s = WeatherReport.SubString(i, j - i); s.FlattenWhitespace(); if (s == "") { return; } dt = s; dt.SetStartOfDay(); dtnow.Now(); dayDiff = (dtnow.DifferenceInSeconds(dt))/86400; if (dayDiff < 0) { dayDiff = 0; } if (dayDiff >=1) { s = dayDiff; if (dayDiff == 1) { result = s + " " + Terms.Get("day ago"); } else { result = s + " " + Terms.Get("days ago"); } } else { i = j + 1; j = WeatherReport.IndexOf("&lrl="); result = WeatherReport.SubString(i, j - i); i = result.IndexOfRev(":"); j = result.IndexOfRev(" "); result.Delete(i, j - i); }

return result; } String GetCurrentTemp(String WeatherReport, String tempUnit)//0:F 1:C { String result; int i, j; i = WeatherReport.IndexOf("&ut="); i += 7; i = WeatherReport.IndexOf("&ct="); i += 4; j = WeatherReport.IndexOf("&cc="); result = TempConvert(WeatherReport.SubString(i, j - i), tempUnit); return result; } String GetCurrentTitle(String WeatherReport) { String result; int i, j; i = WeatherReport.IndexOf("&cs="); i += 4; j = WeatherReport.IndexOf("&fd1="); result = WeatherReport.SubString(i, j - i); return result; } String GetCurrentIconNumber(String WeatherReport) { String result; int i, j; i = WeatherReport.IndexOf("&cc="); i += 4; j = WeatherReport.IndexOf("&cws="); result = WeatherReport.SubString(i, j - i); return result; } String GetHiTemp(String WeatherReport, String tempUnit, String day) { String result, s; int i, j; s = "&fh" + day + "="; i = WeatherReport.IndexOf(s); i += 5; s = "&fl" + day + "="; j = WeatherReport.IndexOf(s); result = TempConvert(WeatherReport.SubString(i, j - i), tempUnit); return result; } String GetLowTemp(String WeatherReport, String tempUnit, String day) { String result,s; int i, j; s = "&fl" + day + "="; i = WeatherReport.IndexOf(s); i += 5; s = "&fsk" + day + "="; j = WeatherReport.IndexOf(s);

result = TempConvert(WeatherReport.SubString(i, j - i), tempUnit); return result; } String GetForecastIconNumber(String WeatherReport, String day) { String result,s; int i, j; s = "&fsk" + day + "="; i = WeatherReport.IndexOf(s); i += 6; s = "&fsk" + day + "n="; j = WeatherReport.IndexOf(s); result = WeatherReport.SubString(i, j - i); return result; } String GetDayName(String WeatherReport, String day) { String result,s; int i, j; s = "&fd" + day + "="; i = WeatherReport.IndexOf(s); i += 5; s = "&fdt" + day + "="; j = WeatherReport.IndexOf(s); result = WeatherReport.SubString(i, j - i); return result; } } #endregion

You might also like