Blame SOURCES/kde-workspace-4.11.7-weather-fix-bbcukmet.patch

ff3475
From 55644fae34ab49834dd52c834c211e765d41f8ab Mon Sep 17 00:00:00 2001
ff3475
From: Raphael Geissert <atomo64@gmail.com>
ff3475
Date: Sat, 15 Feb 2014 15:20:26 +0100
ff3475
Subject: [PATCH 1/3] Update to BBC's new json-based search and modified xml
ff3475
ff3475
BUG:330773
ff3475
---
ff3475
 .../weather/ions/bbcukmet/ion_bbcukmet.cpp         |   93 ++++++++++++--------
ff3475
 1 file changed, 54 insertions(+), 39 deletions(-)
ff3475
ff3475
diff --git a/plasma/generic/dataengines/weather/ions/bbcukmet/ion_bbcukmet.cpp b/plasma/generic/dataengines/weather/ions/bbcukmet/ion_bbcukmet.cpp
ff3475
index 746a734..c656e40 100644
ff3475
--- a/plasma/generic/dataengines/weather/ions/bbcukmet/ion_bbcukmet.cpp
ff3475
+++ b/plasma/generic/dataengines/weather/ions/bbcukmet/ion_bbcukmet.cpp
ff3475
@@ -80,6 +80,7 @@ QMap<QString, IonInterface::ConditionIcons> UKMETIon::setupDayIconMappings(void)
ff3475
     //dayList["sunny intervals night"] = ClearNight;
ff3475
     dayList["partly cloudy"] = PartlyCloudyDay;
ff3475
     dayList["cloudy"] = Overcast;
ff3475
+    dayList["light cloud"] = Overcast;
ff3475
     dayList["white cloud"] = Overcast;
ff3475
     dayList["grey cloud"] = Overcast;
ff3475
     //dayList["low level cloud"] = NotAvailable;
ff3475
@@ -131,6 +132,7 @@ QMap<QString, IonInterface::ConditionIcons> UKMETIon::setupNightIconMappings(voi
ff3475
     nightList["sunny intervals"] = PartlyCloudyDay; // it's not really sunny
ff3475
     nightList["sunny"] = ClearDay;
ff3475
     nightList["cloudy"] = Overcast;
ff3475
+    nightList["light cloud"] = Overcast;
ff3475
     nightList["white cloud"] = Overcast;
ff3475
     nightList["grey cloud"] = Overcast;
ff3475
     nightList["partly cloudy"] = PartlyCloudyNight;
ff3475
@@ -252,7 +254,8 @@ void UKMETIon::getXMLData(const QString& source)
ff3475
 void UKMETIon::findPlace(const QString& place, const QString& source)
ff3475
 {
ff3475
     KUrl url;
ff3475
-    url = "http://news.bbc.co.uk/weather/util/search/SearchResultsNode.xhtml?&search=" + place + "&region=world&startIndex=0&count=500";
ff3475
+    /* There's a page= parameter, results are limited to 10 by page */
ff3475
+    url = "http://www.bbc.com/locator/default/en-GB/search.json?search="+place+"&filter=international&postcode_unit=false&postcode_district=true";
ff3475
 
ff3475
     m_job = KIO::get(url.url(), KIO::Reload, KIO::HideProgressInfo);
ff3475
     m_job->addMetaData("cookies", "none"); // Disable displaying cookies
ff3475
@@ -281,7 +284,7 @@ void UKMETIon::getFiveDayForecast(const QString& source)
ff3475
     
ff3475
     int splitIDPos = xmlPath.lastIndexOf('/');
ff3475
     QString stationID = xmlPath.midRef(splitIDPos + 1).toString();
ff3475
-    m_place[source].XMLforecastURL = "http://newsrss.bbc.co.uk/weather/forecast/" + stationID + "/Next3DaysRSS.xml" + xmlMap.query();
ff3475
+    m_place[source].XMLforecastURL = "http://open.live.bbc.co.uk/weather/feeds/en/" + stationID + "/3dayforecast.rss" + xmlMap.query();
ff3475
     KUrl url(m_place[source].XMLforecastURL);
ff3475
 
ff3475
     m_job = KIO::get(url.url(), KIO::Reload, KIO::HideProgressInfo);
ff3475
@@ -303,47 +306,43 @@ void UKMETIon::readSearchHTMLData(const QString& source, const QByteArray& html)
ff3475
     QStringList tokens;
ff3475
     QString url;
ff3475
     QString tmp;
ff3475
-    int flag = 0;
ff3475
     int counter = 2;
ff3475
 
ff3475
-    // "

Vitoria, Brazil

"
ff3475
-    QRegExp grabURL("/[a-z]+/[a-z]+/([0-9]+)(\\?[^\"]+)?");
ff3475
-    QRegExp grabPlace(">([^<]*[a-z()])"); // FIXME: It would be better to strip away the extra '>'
ff3475
+#ifdef __GNUC__
ff3475
+#warning FIXME: use a json parser instead of regexes
ff3475
+#endif
ff3475
+
ff3475
+    QRegExp grabURL("\"id\":\\s*\"([0-9]+)\"");
ff3475
+    QRegExp grabPlace("\"fullName\":\\s*\"([^\"]+)\"");
ff3475
 
ff3475
     while (!stream.atEnd()) {
ff3475
        line = stream.readLine();
ff3475
-       if (line.contains("

") > 0) {

ff3475
-           flag = 1;
ff3475
-       }
ff3475
 
ff3475
-       if (line.contains("There are no forecasts matching") > 0) {
ff3475
+       if (line.contains("Sorry, no results found for") > 0) {
ff3475
            break;
ff3475
        }
ff3475
 
ff3475
-       if (flag) {
ff3475
+       if (line.contains("\"results\"") > 0) {
ff3475
 
ff3475
             if (grabURL.indexIn(line.trimmed()) > 0) {
ff3475
-                url = "http://newsrss.bbc.co.uk/weather/forecast/" + grabURL.cap(1) + "/ObservationsRSS.xml";
ff3475
-                if (grabURL.captureCount() > 1) {
ff3475
-                    url += grabURL.cap(2);
ff3475
-                }
ff3475
-                grabPlace.indexIn(line.trimmed());
ff3475
-                tmp = QString("bbcukmet|").append(grabPlace.cap(1));
ff3475
 
ff3475
-                // Duplicate places can exist
ff3475
-                if (m_locations.contains(tmp)) {
ff3475
-                    tmp = QString("bbcukmet|").append(QString("%1 (#%2)").arg(grabPlace.cap(1)).arg(counter));
ff3475
-                    counter++;
ff3475
-                }
ff3475
+                for (int captureIndex = 1; captureIndex <= grabURL.captureCount(); captureIndex++) {
ff3475
 
ff3475
-                m_place[tmp].XMLurl = url;
ff3475
-                m_place[tmp].place = grabPlace.cap(1);
ff3475
-                m_locations.append(tmp);
ff3475
-            }
ff3475
-       }
ff3475
+                    url = "http://open.live.bbc.co.uk/weather/feeds/en/" + grabURL.cap(captureIndex) + "/observations.rss";
ff3475
+                    grabPlace.indexIn(line.trimmed());
ff3475
+                    tmp = QString("bbcukmet|").append(grabPlace.cap(captureIndex));
ff3475
+
ff3475
+                    // Duplicate places can exist
ff3475
+                    if (m_locations.contains(tmp)) {
ff3475
+                        tmp = QString("bbcukmet|").append(QString("%1 (#%2)").arg(grabPlace.cap(captureIndex)).arg(counter));
ff3475
+                        counter++;
ff3475
+                    }
ff3475
 
ff3475
-       if (line.contains("
") > 0) {
ff3475
-           flag = 0;
ff3475
+                    m_place[tmp].XMLurl = url;
ff3475
+                    m_place[tmp].place = grabPlace.cap(captureIndex);
ff3475
+                    m_locations.append(tmp);
ff3475
+                }
ff3475
+            }
ff3475
        }
ff3475
     }
ff3475
 
ff3475
@@ -557,12 +556,13 @@ void UKMETIon::parseWeatherObservation(const QString& source, WeatherData& data,
ff3475
                 QStringRef conditionData = conditionString.midRef(splitIndex + 1); // Include ':'
ff3475
                 data.obsTime = conditionString.midRef(0, splitIndex).toString();
ff3475
 
ff3475
-                // Friday at 0200 GMT
ff3475
-                m_dateFormat =  QDateTime::fromString(data.obsTime.split("at")[1].trimmed(), "hhmm 'GMT'");
ff3475
+                // Saturday - 13:00 CET
ff3475
+                // Saturday - 12:00 GMT
ff3475
+                m_dateFormat = QDateTime::fromString(data.obsTime.split("-")[1].trimmed(), "hh:mm 'GMT'");
ff3475
                 data.iconPeriodHour = m_dateFormat.toString("hh").toInt();
ff3475
                 data.iconPeriodMinute = m_dateFormat.toString("mm").toInt();
ff3475
 
ff3475
-                data.condition = conditionData.toString().split('.')[0].trimmed();
ff3475
+                data.condition = conditionData.toString().split(',')[0].trimmed();
ff3475
 
ff3475
             } else if (xml.name() == "link") {
ff3475
                 m_place[source].forecastHTMLUrl = xml.readElementText();
ff3475
@@ -575,21 +575,32 @@ void UKMETIon::parseWeatherObservation(const QString& source, WeatherData& data,
ff3475
 #endif
ff3475
 
ff3475
                 data.temperature_C = observeData[1].split(QChar(176))[0].trimmed();
ff3475
-
ff3475
-                // Temperature might be not available
ff3475
-                if (data.temperature_C.contains("N/A")) {
ff3475
+                if (data.temperature_C.contains("N/A") || data.temperature_C.contains("null")) {
ff3475
                     data.temperature_C = i18n("N/A");
ff3475
                 }
ff3475
 
ff3475
                 data.windDirection = observeData[2].split(',')[0].trimmed();
ff3475
+		if (data.windDirection.contains("null")) {
ff3475
+		    data.windDirection = "";
ff3475
+		}
ff3475
+
ff3475
                 data.windSpeed_miles = observeData[3].split(',')[0].split(' ')[1].remove("mph");
ff3475
+		if (data.windSpeed_miles.contains("null")) {
ff3475
+		    data.windSpeed_miles = "N/A";
ff3475
+		}
ff3475
 
ff3475
                 data.humidity = observeData[4].split(',')[0].split(' ')[1];
ff3475
                 if (data.humidity.endsWith('%')) {
ff3475
                     data.humidity.chop(1);
ff3475
                 }
ff3475
+		if (data.humidity.contains("null")) {
ff3475
+		    data.humidity = "N/A";
ff3475
+		}
ff3475
 
ff3475
                 data.pressure = observeData[5].split(',')[0].split(' ')[1].split("mb")[0];
ff3475
+		if (data.pressure.contains("null")) {
ff3475
+		    data.pressure = "N/A";
ff3475
+		}
ff3475
                 data.pressureTendency = observeData[5].split(',')[1].trimmed();
ff3475
 
ff3475
                 data.visibilityStr = observeData[6].trimmed();
ff3475
@@ -600,6 +611,10 @@ void UKMETIon::parseWeatherObservation(const QString& source, WeatherData& data,
ff3475
             } else if (xml.name() == "long") {
ff3475
                 const QString ordinate = xml.readElementText();
ff3475
                 data.longitude = ordinate.toDouble();
ff3475
+            } else if (xml.name() == "georss:point") {
ff3475
+                const QString ordinates = xml.readElementText();
ff3475
+                data.latitude = ordinates.split(' ')[0].toDouble();
ff3475
+                data.longitude = ordinates.split(' ')[1].toDouble();
ff3475
             } else {
ff3475
                 parseUnknownElement(xml);
ff3475
             }
ff3475
@@ -675,8 +690,8 @@ void UKMETIon::parseFiveDayForecast(const QString& source, QXmlStreamReader& xml
ff3475
     QString line;
ff3475
     QString period;
ff3475
     QString summary;
ff3475
-    QRegExp high("-?\\d+");
ff3475
-    QRegExp low("-?\\d+");
ff3475
+    QRegExp high("-?\\d+.C");
ff3475
+    QRegExp low("-?\\d+.C");
ff3475
     while (!xml.atEnd()) {
ff3475
         xml.readNext();
ff3475
         if (xml.name() == "title") {
ff3475
@@ -687,8 +702,8 @@ void UKMETIon::parseFiveDayForecast(const QString& source, QXmlStreamReader& xml
ff3475
 
ff3475
             period = line.split(',')[0].split(':')[0];
ff3475
             summary = line.split(',')[0].split(':')[1].trimmed();
ff3475
-            high.indexIn(line.split(',')[1]);
ff3475
-            low.indexIn(line.split(',')[2]);
ff3475
+            high.indexIn(line.split(',')[1].split(':')[1]);
ff3475
+            low.indexIn(line.split(',')[1].split(':')[2]);
ff3475
 
ff3475
             forecast->period = period;
ff3475
             forecast->iconName = getWeatherIcon(dayIcons(), summary.toLower());
ff3475
-- 
ff3475
1.7.10
ff3475