1   /**
2    * Copyright (c) 2000-2004 Liferay, LLC. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.weather.util;
24  
25  import com.liferay.portal.util.WebCacheable;
26  import com.liferay.portlet.weather.model.Weather;
27  import com.liferay.util.ConverterException;
28  import com.liferay.util.Html;
29  import com.liferay.util.Http;
30  import com.liferay.util.StringPool;
31  import com.liferay.util.StringUtil;
32  import com.liferay.util.Time;
33  
34  import java.net.URL;
35  
36  import java.util.Vector;
37  
38  import org.apache.soap.Constants;
39  import org.apache.soap.rpc.Call;
40  import org.apache.soap.rpc.Parameter;
41  import org.apache.soap.rpc.Response;
42  
43  /**
44   * <a href="WeatherConverter.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author  Brian Wing Shun Chan
47   * @version $Revision: 1.13 $
48   *
49   */
50  public class WeatherConverter implements WebCacheable {
51  
52      public static int NWS = 1;
53      public static int XMETHODS = 2;
54      public static int WEATHER_CHANNEL = 3;
55      public static int YAHOO = 4;
56      public static int TYPE = YAHOO;
57  
58      public WeatherConverter(String zip) {
59          _zip = zip;
60      }
61  
62      public Object convert(String id) throws ConverterException {
63          Weather weather = null;
64  
65          try {
66              if (TYPE == NWS) {
67                  String text = Http.URLtoString(
68                      "http://www.srh.noaa.gov/zipcity.php?inputstring=" + _zip);
69  
70                  int pos = text.indexOf("currentconds.jpg");
71  
72                  int x = text.indexOf("&deg;F", pos);
73                  int y = text.substring(0, x).lastIndexOf(">") + 1;
74  
75                  weather = new Weather(
76                      _zip, Float.parseFloat(text.substring(y, x)));
77              }
78              else  if (TYPE == XMETHODS) {
79                  URL url = new URL(
80                      "http://services.xmethods.com:80/soap/servlet/rpcrouter");
81  
82                  Call call = new Call ();
83                  call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
84                  call.setTargetObjectURI("urn:xmethods-Temperature");
85                  call.setMethodName("getTemp");
86  
87                  Vector params = new Vector ();
88                  params.addElement(
89                      new Parameter("zipcode", String.class, _zip, null));
90  
91                  call.setParams (params);
92  
93                  Response resp = call.invoke(url, StringPool.BLANK);
94  
95                  if (resp.generatedFault()) {
96                      throw new Exception();
97                  }
98                  else {
99                      Parameter result = resp.getReturnValue ();
100                     Float temperature = (Float)result.getValue();
101 
102                     weather = new Weather(_zip, temperature.floatValue());
103                 }
104             }
105             else if (TYPE == WEATHER_CHANNEL) {
106                 String text = Http.URLtoString(
107                     "http://www.weather.com/search/search?where=" +
108                     Http.encodeURL(_zip));
109 
110                 int x = text.indexOf("<!-- insert wx icon -->") + 23;
111 
112                 String iconURL = text.substring(
113                     text.indexOf("http://", x),
114                     text.indexOf(".gif", x) + 4);
115                 iconURL = StringUtil.replace(iconURL, "/52/", "/31/");
116 
117                 x = text.indexOf("<!-- insert current temp -->") + 28;
118                 int y = text.indexOf("&deg;F", x);
119 
120                 float temperature = Float.parseFloat(text.substring(x, y));
121 
122                 weather = new Weather(_zip, iconURL, temperature);
123             }
124             else if (TYPE == YAHOO) {
125                 String text = Html.stripComments(Http.URLtoString(
126                     "http://search.weather.yahoo.com/search/weather2?p=" +
127                     Http.encodeURL(_zip)));
128 
129                 int x = text.indexOf("<b>", text.indexOf("Currently:")) + 3;
130                 int y = text.indexOf("&deg;", x);
131 
132                 float temperature = Float.parseFloat(
133                     text.substring(x, y).trim());
134 
135                 String iconURL = text.substring(
136                     text.indexOf("http://", x),
137                     text.indexOf(".gif", x) + 4);
138 
139                 iconURL = StringUtil.replace(iconURL, "/52/", "/31/");
140 
141                 weather = new Weather(_zip, iconURL, temperature);
142             }
143         }
144         catch (Exception e) {
145             throw new ConverterException(_zip + " " + e.toString());
146         }
147 
148         return weather;
149     }
150 
151     public long getRefreshTime() {
152         return _refreshTime;
153     }
154 
155     private long _refreshTime = Time.MINUTE * 20;
156 
157     private String _zip;
158 
159 }