added missing yweather configuration in rome.properties, fixed some

minor bugs, fixes #10
This commit is contained in:
Martin Kurz 2014-04-20 21:24:40 +02:00
parent 62af757574
commit 01d9400eda
5 changed files with 100 additions and 28 deletions

View file

@ -39,6 +39,7 @@ package org.rometools.feed.module.yahooweather.io;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import org.jdom2.Element;
@ -58,11 +59,11 @@ import com.sun.syndication.io.ModuleGenerator;
*/
public class WeatherModuleGenerator implements ModuleGenerator {
private static final Namespace NS = Namespace.getNamespace("yweather", YWeatherModule.URI);
private static final SimpleDateFormat TIME_ONLY = new SimpleDateFormat("h:mm a");
private static final SimpleDateFormat LONG_DATE = new SimpleDateFormat("EEE, d MMM yyyy h:mm a zzz");
private static final SimpleDateFormat SHORT_DATE = new SimpleDateFormat("d MMM yyyy");
private static final SimpleDateFormat TIME_ONLY = new SimpleDateFormat("h:mm a", Locale.US);
private static final SimpleDateFormat LONG_DATE = new SimpleDateFormat("EEE, d MMM yyyy h:mm a zzz", Locale.US);
private static final SimpleDateFormat SHORT_DATE = new SimpleDateFormat("d MMM yyyy", Locale.US);
/** Creates a new instance of SlashModuleGenerator */
/** Creates a new instance of SlashModuleGenerator. */
public WeatherModuleGenerator() {
}

View file

@ -69,12 +69,7 @@ import com.sun.syndication.io.ModuleParser;
* @author <a href="mailto:cooper@screaming-penguin.com">Robert "kebernet" Cooper</a>
*/
public class WeatherModuleParser implements ModuleParser {
private static final Logger LOG = LoggerFactory.getLogger(WeatherModuleParser.class);
private static final SimpleDateFormat TIME_ONLY = new SimpleDateFormat("h:mm a");
private static final SimpleDateFormat LONG_DATE = new SimpleDateFormat("EEE, d MMM yyyy h:mm a zzz");
private static final SimpleDateFormat SHORT_DATE = new SimpleDateFormat("d MMM yyyy");
private static final Namespace NS = Namespace.getNamespace(YWeatherModule.URI);
@Override
@ -129,8 +124,9 @@ public class WeatherModuleParser implements ModuleParser {
if (astronomy != null) {
try {
final Astronomy a = new Astronomy(TIME_ONLY.parse(astronomy.getAttributeValue("sunrise").replaceAll("am", "AM").replaceAll("pm", "PM")),
TIME_ONLY.parse(astronomy.getAttributeValue("sunset").replaceAll("am", "AM").replaceAll("pm", "PM")));
final SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm a", locale);
final Astronomy a = new Astronomy(timeFormat.parse(astronomy.getAttributeValue("sunrise").replaceAll("am", "AM").replaceAll("pm", "PM")),
timeFormat.parse(astronomy.getAttributeValue("sunset").replaceAll("am", "AM").replaceAll("pm", "PM")));
module.setAstronomy(a);
} catch (final ParseException pe) {
LOG.warn("ParseException processing <astronomy> tag.", pe);
@ -141,8 +137,9 @@ public class WeatherModuleParser implements ModuleParser {
if (condition != null) {
try {
final SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy h:mm a zzz", locale);
final Condition c = new Condition(condition.getAttributeValue("text"), ConditionCode.fromCode(Integer.parseInt(condition
.getAttributeValue("code"))), Integer.parseInt(condition.getAttributeValue("temp")), LONG_DATE.parse(condition
.getAttributeValue("code"))), Integer.parseInt(condition.getAttributeValue("temp")), dateFormat.parse(condition
.getAttributeValue("date").replaceAll("pm", "PM").replaceAll("am", "AM")));
module.setCondition(c);
} catch (final NumberFormatException nfe) {
@ -158,11 +155,12 @@ public class WeatherModuleParser implements ModuleParser {
final Forecast[] f = new Forecast[forecasts.size()];
int i = 0;
final SimpleDateFormat dateFormat = new SimpleDateFormat("d MMM yyyy", locale);
for (final Iterator<Element> it = forecasts.iterator(); it.hasNext(); i++) {
final Element forecast = it.next();
try {
f[i] = new Forecast(forecast.getAttributeValue("day"), SHORT_DATE.parse(forecast.getAttributeValue("date")), Integer.parseInt(forecast
f[i] = new Forecast(forecast.getAttributeValue("day"), dateFormat.parse(forecast.getAttributeValue("date")), Integer.parseInt(forecast
.getAttributeValue("low")), Integer.parseInt(forecast.getAttributeValue("high")), forecast.getAttributeValue("text"),
ConditionCode.fromCode(Integer.parseInt(forecast.getAttributeValue("code"))));
} catch (final NumberFormatException nfe) {

View file

@ -11,8 +11,8 @@ rss_2.0.feed.ModuleParser.classes=org.rometools.feed.module.cc.io.ModuleParserRS
org.rometools.feed.module.mediarss.io.MediaModuleParser \
org.rometools.feed.module.itunes.io.ITunesParserOldNamespace \
org.rometools.feed.module.mediarss.io.AlternateMediaModuleParser \
org.rometools.feed.module.sle.io.ModuleParser
org.rometools.feed.module.sle.io.ModuleParser \
org.rometools.feed.module.yahooweather.io.WeatherModuleParser
rss_1.0.feed.ModuleParser.classes=org.rometools.feed.module.cc.io.ModuleParserRSS1 \
org.rometools.feed.module.content.io.ContentModuleParser
@ -45,7 +45,8 @@ rss_2.0.item.ModuleParser.classes=org.rometools.feed.module.cc.io.ModuleParserRS
org.rometools.feed.module.photocast.io.Parser \
org.rometools.feed.module.itunes.io.ITunesParserOldNamespace \
org.rometools.feed.module.mediarss.io.AlternateMediaModuleParser \
org.rometools.feed.module.sle.io.ItemParser
org.rometools.feed.module.sle.io.ItemParser \
org.rometools.feed.module.yahooweather.io.WeatherModuleParser
rss_1.0.item.ModuleParser.classes=org.rometools.feed.module.cc.io.ModuleParserRSS1 \
org.rometools.feed.module.base.io.GoogleBaseParser \
@ -83,7 +84,8 @@ rss_2.0.feed.ModuleGenerator.classes=org.rometools.feed.module.cc.io.CCModuleGen
org.rometools.feed.module.georss.W3CGeoGenerator \
org.rometools.feed.module.photocast.io.Generator \
org.rometools.feed.module.mediarss.io.MediaModuleGenerator \
org.rometools.feed.module.sle.io.ModuleGenerator
org.rometools.feed.module.sle.io.ModuleGenerator \
org.rometools.feed.module.yahooweather.io.WeatherModuleGenerator
rss_1.0.feed.ModuleGenerator.classes=org.rometools.feed.module.content.io.ContentModuleGenerator
@ -111,7 +113,8 @@ rss_2.0.item.ModuleGenerator.classes=org.rometools.feed.module.cc.io.CCModuleGen
org.rometools.feed.module.georss.SimpleGenerator \
org.rometools.feed.module.georss.W3CGeoGenerator \
org.rometools.feed.module.photocast.io.Generator \
org.rometools.feed.module.mediarss.io.MediaModuleGenerator
org.rometools.feed.module.mediarss.io.MediaModuleGenerator \
org.rometools.feed.module.yahooweather.io.WeatherModuleGenerator
rss_1.0.item.ModuleGenerator.classes=org.rometools.feed.module.base.io.GoogleBaseGenerator \
org.rometools.feed.module.content.io.ContentModuleGenerator \

View file

@ -24,23 +24,31 @@ import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.SyndFeedOutput;
/**
*
*
* @author <a href="mailto:cooper@screaming-penguin.com">Robert "kebernet" Cooper</a>
*/
public class WeatherGeneratorTest extends AbstractTestCase {
private static final Logger LOG = LoggerFactory.getLogger(WeatherGeneratorTest.class);
/**
* @param testName name of the test
*/
public WeatherGeneratorTest(final String testName) {
super(testName);
}
/**
* @return test suite
*/
public static Test suite() {
return new TestSuite(WeatherGeneratorTest.class);
}
/**
* Test of generate method, of class com.totsp.xml.syndication.base.io.SlashGenerator.
*
* @throws Exception on error
*/
public void testGenerate() throws Exception {
LOG.debug("testGenerate");
@ -56,18 +64,15 @@ public class WeatherGeneratorTest extends AbstractTestCase {
final SyndFeed feed = input.build(testFiles[h]);
output.output(feed, new File("target/" + testFiles[h].getName()));
final SyndFeed feed2 = input.build(new File("target/" + testFiles[h].getName()));
{
final YWeatherModule weather = (YWeatherModule) feed.getModule(YWeatherModule.URI);
final YWeatherModule weather2 = (YWeatherModule) feed2.getModule(YWeatherModule.URI);
Assert.assertEquals(testFiles[h].getName(), weather, weather2);
}
final YWeatherModule weather = (YWeatherModule) feed.getModule(YWeatherModule.URI);
final YWeatherModule weather2 = (YWeatherModule) feed2.getModule(YWeatherModule.URI);
Assert.assertEquals(testFiles[h].getName(), weather, weather2);
for (int i = 0; i < feed.getEntries().size(); i++) {
final SyndEntry entry = feed.getEntries().get(i);
final SyndEntry entry2 = feed2.getEntries().get(i);
final YWeatherModule weather = (YWeatherModule) entry.getModule(YWeatherModule.URI);
final YWeatherModule weather2 = (YWeatherModule) entry2.getModule(YWeatherModule.URI);
Assert.assertEquals(testFiles[h].getName(), weather, weather2);
final YWeatherModule weatherEntry = (YWeatherModule) entry.getModule(YWeatherModule.URI);
final YWeatherModule weatherEntry2 = (YWeatherModule) entry2.getModule(YWeatherModule.URI);
assertEquals(testFiles[h].getName(), weatherEntry, weatherEntry2);
}
}

View file

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0"
xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>
<!-- http://xml.weather.yahoo.com/forecastrss/ITXX0090.xml -->
<title>Yahoo! Weather - Milano/Linate, IT</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Milano/Linate__IT/*http://weather.yahoo.com/forecast/ITXX0090_f.html</link>
<description>Yahoo! Weather for Milano/Linate, IT</description>
<language>en-us</language>
<lastBuildDate>Sun, 20 Apr 2014 8:19 pm CEST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Milano/Linate" region=""
country="IT" />
<yweather:units temperature="F" distance="mi" pressure="in"
speed="mph" />
<yweather:wind chill="59" direction="210" speed="7" />
<yweather:atmosphere humidity="72" visibility="6210"
pressure="29.71" rising="0" />
<yweather:astronomy sunrise="6:29 am" sunset="8:12 pm" />
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url>
</image>
<item>
<title>Conditions for Milano/Linate, IT at 8:19 pm CEST</title>
<geo:lat>45.43</geo:lat>
<geo:long>9.28</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Milano/Linate__IT/*http://weather.yahoo.com/forecast/ITXX0090_f.html</link>
<pubDate>Sun, 20 Apr 2014 8:19 pm CEST</pubDate>
<yweather:condition text="Mostly Cloudy" code="27"
temp="59" date="Sun, 20 Apr 2014 8:19 pm CEST" />
<description><![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/27.gif"/><br />
<b>Current Conditions:</b><br />
Mostly Cloudy, 59 F<BR />
<BR /><b>Forecast:</b><BR />
Sun - Rain. High: 65 Low: 52<br />
Mon - Light Rain. High: 61 Low: 50<br />
Tue - PM Showers. High: 69 Low: 49<br />
Wed - Sunny. High: 74 Low: 50<br />
Thu - Mostly Sunny. High: 76 Low: 52<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Milano/Linate__IT/*http://weather.yahoo.com/forecast/ITXX0090_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]></description>
<yweather:forecast day="Sun" date="20 Apr 2014"
low="52" high="65" text="Rain" code="12" />
<yweather:forecast day="Mon" date="21 Apr 2014"
low="50" high="61" text="Light Rain" code="11" />
<yweather:forecast day="Tue" date="22 Apr 2014"
low="49" high="69" text="PM Showers" code="39" />
<yweather:forecast day="Wed" date="23 Apr 2014"
low="50" high="74" text="Sunny" code="32" />
<yweather:forecast day="Thu" date="24 Apr 2014"
low="52" high="76" text="Mostly Sunny" code="34" />
<guid isPermaLink="false">ITXX0090_2014_04_24_7_00_CEST</guid>
</item>
</channel>
</rss>
<!-- fan1572.sports.bf1.yahoo.com Sun Apr 20 18:57:39 PST 2014 -->