La doc de l’API Yahoo est ici : https://developer.yahoo.com/weather/documentation.html.
Utiliser le site https://fr.meteo.yahoo.com/ pour trouver le WOEID.
On trouve des docs sur les expressions régulières (regex) ici :
- http://cyberzoide.developpez.com/tutoriels/java/regex/
- http://codes-sources.commentcamarche.net/faq/1286-utilisation-des-expressions-regulieres-en-java
- http://fr.openclassrooms.com/informatique/cours/concevez-votre-site-web-avec-php-et-mysql/les-expressions-regulieres-partie-1-2
Et des testeurs de regex ici :
- http://regex101.com/
- http://www.xul.fr/ecmascript/expression-reguliere-demo.php
- http://www.regexplanet.com/advanced/java/index.html
- http://myregexp.com/
1. Le code de l’exemple.
// La doc de l'API Yahoo est ici https://developer.yahoo.com/weather/. // Attention code pour Processing 2 et 3.0a4 ne fonctionne pas avec le 1.5 //************************************* //***** Frank SAURET Octobre 2014 ***** //************************************* // Ce code lit la météo sur le site Yahoo et l'affiche String Titre ; String Ville ; String Pays ; String VentRefroidissement ; String VentDirection ; String VentVitesse ; String AtmoHumidite ; String AtmoVisibilite ; String AtmoPression ; String AtmoElevation ; String AstroAube ; String AstroCouchant ; String ActuelCondition ; String ActuelConditionCode ; String ActuelTemperature ; String ActuelDate ; String Latitude ; String Longitude ; String[] JourM =new String[5]; String[] DateM =new String[5]; String[] TemperatureMin =new String[5]; String[] TemperatureMax =new String[5]; String[] ConditionM =new String[5]; String[] ConditionCodeM =new String[5]; //Tableau pour la traduction des code météo de Yahoo. On touve le tableau original sur la doc de l'API String[] CodeMeteo= { "Tornade", "Tempête tropicale", "Ouragan", "Orages violents", "Orages", "Pluie et neige", "Pluie et grésil", "Neige et grésil", "Bruine verglaçante", "Bruine", "Pluie verglaçante", "Pluie", "Pluie", "Averses de neige", "Neige légère ", "Poudreuse", "Neige", "Grêle", "Neige fondue", "Poussière", "Brumeux", "Brume", "Brouillard", "Tempête", "Venteux", "Froid", "Nuageux", "Couvert (nuit)", "La plupart du temps nuageux (jour)", "Partiellement nuageux (nuit)", "Partiellement nuageux (jour)", "Clair (nuit)", "Ensoleillé", "Beau temps (nuit)", "Beau temps (jour)", "Pluie et grêle", "Chaud", "Orages", "Orages éparses", "Orages éparses", "Averses", "Neige importante", "Averses de neige éparses", "Neige", "Partiellement nuageux", "Orages ", "Averses de neige", "Orages isolés" }; PImage[] ImgMeteo=new PImage[6]; String[] AdresseImage =new String[6]; void setup() { size(450, 900); fill(0); textFont(createFont("Gisha", 14)); } void draw() { background(200); LireLeFlux(); //***** Afficher les images int XImage=230; //Météo actuelle AdresseImage[5]="http://l.yimg.com/a/i/us/we/52/"+ActuelConditionCode+".gif"; ImgMeteo[5]=loadImage(AdresseImage[5]); image(ImgMeteo[5], XImage, 150); // Météo Aujourd'hui AdresseImage[0]="http://l.yimg.com/a/i/us/we/52/"+ConditionCodeM[0]+".gif"; ImgMeteo[0]=loadImage(AdresseImage[0]); image(ImgMeteo[0], XImage, 350); //Prévisions for (int i = 1; i < 5; i++) { AdresseImage[i]="http://l.yimg.com/a/i/us/we/52/"+ConditionCodeM[i]+".gif"; ImgMeteo[i]=loadImage(AdresseImage[i]); image(ImgMeteo[i], XImage, 370+105*i); } //***** Ecriture de la page text("Ville de : "+ Ville+ " en "+ Pays+ " | Longitude : "+Longitude+"°"+ " | Latitude : "+Latitude+"°"+ "\n"+TraduireTexte(ActuelDate)+ "\n\nMaintenant :"+ "\n Refroidissement éolien : "+VentRefroidissement+"°"+ "\n Vitesse du vent : "+VentVitesse+" km/h"+ "\n Direction du vent : "+VentDirection+"°"+ "\n Humidité : "+AtmoHumidite+" %"+ "\n Visibilité : "+AtmoVisibilite+" km"+ "\n Pression : "+AtmoPression+" hPa"+ "\n Augmentation : "+AtmoElevation+ "\n Condition météo : "+CodeMeteo[int(ActuelConditionCode)]+ "\n Température : "+ActuelTemperature+"°"+ "\n\nAujourd\'hui :"+ "\n Lever du soleil : "+ConvertireHeure(AstroAube)+ "\n Coucher du soleil : "+ConvertireHeure(AstroCouchant)+ "\n Température min : "+TemperatureMin[0]+"°"+ "\n Température max : "+TemperatureMax[0]+"°"+ "\n Condition météo : "+CodeMeteo[int(ConditionCodeM[0])]+ "\n\nDemain : "+TraduireTexte(JourM[1])+" "+TraduireTexte(DateM[1])+ "\n Température min : "+TemperatureMin[1]+"°"+ "\n Température max : "+TemperatureMax[1]+"°"+ "\n Condition météo : "+CodeMeteo[int(ConditionCodeM[1])]+ "\n\nAprès demain : "+TraduireTexte(JourM[2])+" "+TraduireTexte(DateM[2])+ "\n Température min : "+TemperatureMin[2]+"°"+ "\n Température max : "+TemperatureMax[2]+"°"+ "\n Condition météo : "+CodeMeteo[int(ConditionCodeM[2])]+ "\n\n"+TraduireTexte(JourM[3])+" "+TraduireTexte(DateM[3])+ "\n Température min : "+TemperatureMin[3]+"°"+ "\n Température max : "+TemperatureMax[3]+"°"+ "\n Condition météo : "+CodeMeteo[int(ConditionCodeM[3])]+ "\n\n"+TraduireTexte(JourM[4])+" "+TraduireTexte(DateM[4])+ "\n Température min : "+TemperatureMin[4]+"°"+ "\n Température max : "+TemperatureMax[4]+"°"+ "\n Condition météo : "+CodeMeteo[int(ConditionCodeM[4])] , 20, 20); } //***************************************************** //****** Traduction des jours et mois en français ***** //***** Utilisation de la fonction string.replace ***** //***************************************************** String TraduireTexte (String Texte) { // Déclaration des mots à traduire String[] MotAnglais= new String[] { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Jan ", "Feb ", "Mar ", "Apr ", "May ", "Jun ", "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec " }; //Déclaration de la traduction String[] MotFrancais= new String[] { "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche", "Janvier ", "Février ", "Mars ", "Avril ", "Mai ", "Juin ", "Juillet ", "Aout ", "Septembre ", "Octobre ", "Novembre ", "Décembre " }; for (int i = 0; i < MotAnglais.length; i++) { Texte=Texte.replaceAll(MotAnglais[i], MotFrancais[i]); } return Texte; } //******************************************************************* //****** Conversion de l'heure anglaise en heure internationale ***** //********* Utilisation de la fonction match et d'une regex ********* //******************************************************************* String ConvertireHeure(String HeureAnglaise) { String[] list=split(HeureAnglaise, ":"); int HeureA=int(list[0]); int MinuteA=int(list[1]); String Correspondance[]=match(HeureAnglaise, "..$"); String MatA=trim(Correspondance[0]); if (MatA.equalsIgnoreCase("pm") && (HeureA<12)) { HeureA=HeureA+12; } String HeureInternationale=str(HeureA)+":"+nf(MinuteA, 2); return HeureInternationale; } //************************************* //***** Lecture du flux rxx (xml) ***** //************************************* void LireLeFlux() { /* *** Premier paramètre ?w= : 12725111 = le WOEID de saint-nazaire. Utiliser le site https://fr.meteo.yahoo.com/ pour trouver le WOEID *** Deuxième paramètre les unitées &u= : ** si f : - Vitesse du vent = mph - temperature = fahrenheit - distance = mile - pression = in ** si c: - Vitesse du vent = km/h - temperature = celsius - distance = km - pression = millibar ou hPa */ // Charge le flux RSS XML RSS = loadXML ("http://weather.yahooapis.com/forecastrss?w=12725111&u=c"); //***** Lecture du flux //Lecture des éléments par getcontent et des attributs par get string Titre=rss.getChild("channel/item/title").getContent(); Ville=rss.getChild("channel/yweather:location").getString("city"); Pays=rss.getChild("channel/yweather:location").getString("country"); VentRefroidissement=rss.getChild("channel/yweather:wind").getString("chill"); VentDirection=rss.getChild("channel/yweather:wind").getString("direction"); VentVitesse=rss.getChild("channel/yweather:wind").getString("speed"); AtmoHumidite=rss.getChild("channel/yweather:atmosphere").getString("humidity"); AtmoVisibilite=rss.getChild("channel/yweather:atmosphere").getString("visibility"); AtmoPression=rss.getChild("channel/yweather:atmosphere").getString("pressure"); AtmoElevation=rss.getChild("channel/yweather:atmosphere").getString("rising"); AstroAube=rss.getChild("channel/yweather:astronomy").getString("sunrise"); AstroCouchant=rss.getChild("channel/yweather:astronomy").getString("sunset"); ActuelCondition=rss.getChild("channel/item/yweather:condition").getString("text"); ActuelConditionCode=rss.getChild("channel/item/yweather:condition").getString("code"); ActuelTemperature=rss.getChild("channel/item/yweather:condition").getString("temp"); ActuelDate=rss.getChild("channel/item/yweather:condition").getString("date"); Latitude=rss.getChild("channel/item/geo:lat").getContent(); Longitude=rss.getChild("channel/item/geo:long").getContent(); // lecture itérative de tous les attributs d'un éléments XML [] Previsions = rss.getChildren("channel/item/yweather:forecast"); for (int i = 0; i < Previsions.length; i++) { JourM[i] = Previsions[i].getString("day"); DateM[i] = Previsions[i].getString("date"); TemperatureMin[i] = Previsions[i].getString("low"); TemperatureMax[i] = Previsions[i].getString("high"); ConditionM[i] = Previsions[i].getString("text"); ConditionCodeM[i] = Previsions[i].getString("code"); } }
2. Le code du fichier XML à traiter.
-
<RSS xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
-
<channel>
-
<title>Yahoo! Weather - St.-Nazaire, FR</title>
-
<link>http://us.rd.yahoo.com/dailynews/rss/weather/St.-Nazaire__FR/*http://weather.yahoo.com/forecast/FRPL1359_c.html</link>
-
<description>Yahoo! Weather for St.-Nazaire, FR</description>
-
<language>en-us</language>
-
<lastBuildDate>Mon, 20 Oct 2014 12:29 pm CEST</lastBuildDate>
-
<ttl>60</ttl>
-
<yweather:location city="St.-Nazaire" region="" country="France"/>
-
<yweather:units temperature="C" distance="km" pressure="mb" speed="km/h"/>
-
<yweather:wind chill="16" direction="0" speed="1.61"/>
-
<yweather:atmosphere humidity="100" visibility="6" pressure="1015.92" rising="0"/>
-
<yweather:astronomy sunrise="8:33 am" sunset="7:10 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 St.-Nazaire, FR at 12:29 pm CEST</title>
-
<geo:lat>47.28</geo:lat>
-
<geo:long>-2.24</geo:long>
-
<link>http://us.rd.yahoo.com/dailynews/rss/weather/St.-Nazaire__FR/*http://weather.yahoo.com/forecast/FRPL1359_c.html</link>
-
<pubDate>Mon, 20 Oct 2014 12:29 pm CEST</pubDate>
-
<yweather:condition text="Cloudy" code="26" temp="16" date="Mon, 20 Oct 2014 12:29 pm CEST"/>
-
<description>
-
<![CDATA[ <img src="http://l.yimg.com/a/i/us/we/52/26.gif"/><br /> <b>Current Conditions:</b><br />
-
Cloudy, 16 C<BR /> <BR /><b>Forecast:</b><BR /> Mon - Rain. High: 18 Low: 13<br />
-
Tue - Partly Cloudy/Wind. High: 18 Low: 9<br /> Wed - Mostly Sunny. High: 17 Low: 9<br />
-
Thu - Mostly Cloudy. High: 17 Low: 13<br /> Fri - Mostly Cloudy. High: 19 Low: 14<br /> <br />
-
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/St.-Nazaire__FR/*http://weather.yahoo.com/forecast/FRPL1359_c.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="Mon" date="20 Oct 2014" low="13" high="18" text="Rain" code="12"/>
-
<yweather:forecast day="Tue" date="21 Oct 2014" low="9" high="18" text="Partly Cloudy/Wind" code="24"/>
-
<yweather:forecast day="Wed" date="22 Oct 2014" low="9" high="17" text="Mostly Sunny" code="34"/>
-
<yweather:forecast day="Thu" date="23 Oct 2014" low="13" high="17" text="Mostly Cloudy" code="28"/>
-
<yweather:forecast day="Fri" date="24 Oct 2014" low="14" high="19" text="Mostly Cloudy" code="28"/>
-
<guid isPermaLink="false">FRPL1359_2014_10_24_7_00_CEST</guid>
-
</item>
-
</channel>
-
</RSS>
-
<!-- fan1629.sports.bf1.yahoo.com Mon Oct 20 04:16:55 PDT 2014 -->