1
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
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("°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("°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("°", 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 }