📜 ⬆️ ⬇️

Let's talk about the weather

Recently, I had to quite thoroughly communicate with several services of Yahoo! In this article I will talk about one of them, which may be useful for many. This is Yahoo! Weather .

About two years ago I searched the network for a convenient and open source of such data, but I did not find anything. All domestic services exported data for money (it was not the price that scared off rather than having to pay), but Westerners either did not provide data in a convenient format for automatic processing, or did not know about the existence of the city in which I live.

Yahoo! Weather pleased with the absence of these shortcomings. Information about the current weather, as well as the forecast for the next 5 days is exported from their server in RSS format. In my opinion, the choice of this format is pulled by the ears, because weather RSS "feed" contains only one item. The same data could be exported in a more concise way, but Yahoo seemed to rely on standard RSS processing methods, which is also quite a weighty argument. Standard is still standard.
')
The RSS feed address for the city of interest can be found at weather.yahoo.com .



In general, the URL is as follows:

xml.weather.yahoo.com/forecastrss?p=_&u=_

The first parameter is the identifier of the city of interest. The second parameter determines in which units the temperature will be displayed. The value of "c" corresponds to degrees Celsius, "f" - degrees Fahrenheit.
A few examples:



Automate it



The YahooWeather class (PHP5) described below is designed to download RSS over HTTP and preprocess data.

  <? php

 class YahooWeather {
	 // Wind
	 public $ wind_chill;
	 public $ wind_direction;
	 public $ wind_speed;
	
	 // Atmospheric indicators
	 public $ humidity;
	 public $ visibility;
	 public $ pressure;
	
	 // We translate sunrise and sunset time to unix time
	 public $ sunrise;
	 public $ sunset;
	
	 // Current air temperature and weather
	 public $ temp;
	 public $ condition_text;
	 public $ condition_code;
	
	 // Weather forecast for 5 days
	 public $ forecast;
	
	 public $ units;
	
	 function __construct ($ code, $ units = 'c', $ lang = 'en') {
		 $ this-> units = ($ units == 'c')? 'c': 'f';
		
		 $ url = 'http://xml.weather.yahoo.com/forecastrss?p='.
			 $ code. '& u ='. $ this-> units;
		
		 $ xml_contents = file_get_contents ($ url);
		 if ($ xml_contents === false) 
			 throw new Exception ('Error loading'. $ url);
		
		 $ xml = new SimpleXMLElement ($ xml_contents);
		
		 // Wind
		 $ tmp = $ xml-> xpath ('/ rss / channel / yweather: wind');
		 if ($ tmp === false) throw new Exception ("Error parsing XML.");
		 $ tmp = $ tmp [0];
		
		 $ this-> wind_chill = (int) $ tmp ['chill'];
		 $ this-> wind_direction = (int) $ tmp ['direction'];
		 $ this-> wind_speed = (int) $ tmp ['speed'];
		
		 // Atmospheric indicators
		 $ tmp = $ xml-> xpath ('/ rss / channel / yweather: atmosphere');
		 if ($ tmp === false) throw new Exception ("Error parsing XML.");
		 $ tmp = $ tmp [0];
		
		 $ this-> humidity = (int) $ tmp ['humidity'];
		 $ this-> visibility = (int) $ tmp ['visibility'];
		 $ this-> pressure = (int) $ tmp ['pressure'];
		
		 // We translate sunrise and sunset time to unix time
		 $ tmp = $ xml-> xpath ('/ rss / channel / yweather: astronomy');
		 if ($ tmp === false) throw new Exception ("Error parsing XML.");
		 $ tmp = $ tmp [0];
		
		 $ this-> sunrise = strtotime ($ tmp ['sunrise']);
		 $ this-> sunset = strtotime ($ tmp ['sunset']);
		
		 // Current air temperature and weather
		 $ tmp = $ xml-> xpath ('/ rss / channel / item / yweather: condition');
		 if ($ tmp === false) throw new Exception ("Error parsing XML.");
		 $ tmp = $ tmp [0];
		
		 $ this-> temp = (int) $ tmp ['temp'];
		 $ this-> condition_text = strtolower ((string) $ tmp ['text']);
		 $ this-> condition_code = (int) $ tmp ['code'];
		
		 // Weather forecast for 5 days
		 $ forecast = array ();
		 $ tmp = $ xml-> xpath ('/ rss / channel / item / yweather: forecast');
		 if ($ tmp === false) throw new Exception ("Error parsing XML.");
		
		 foreach ($ tmp as $ day) {
			 $ this-> forecast [] = array (
				 'date' => strtotime ((string) $ day ['date']),
				 'low' => (int) $ day ['low'],
				 'high' => (int) $ day ['high'],
				 'text' => (string) $ day ['text'],
				 'code' => (int) $ day ['code']
			 );
		 }
	 }
	
	 public function __toString () {
		 $ u = "°". (($ this-> units == 'c')? 'C': 'F');
		 return $ this-> temp. '  '. $ u.', '. $ this-> condition_text;
	 }
 }

 ?> 


Example of use:

  <? php

 try {
	 $ weather = new YahooWeather ('RSXX0091');
 } catch (Exception $ e) {
	 echo "Caught exception:". $ e-> getMessage ();
	 exit ();
 }

 echo '<h1>'. $ weather. '</ h1>';

 echo "<pre>";
 print_r ($ weather);
 echo "</ pre>";

 ?> 


Conclusion:



Sunrise and sunset time values ​​(sunrise and sunset fields, respectively) are automatically converted to the unix time format. It should also pay attention to the fact that for some unknown reason, Yahoo! gives an obviously incorrect value of atmospheric pressure (0). But the very presence of the corresponding attribute in XML gives some hope that sooner or later the export of this data will be repaired.

The condition_code field is used to store the weather code code. The text decoding of the current value is stored in the condition_text field, and the full list of possible options is given in the following table:

CodeDescription
0tornado
onetropical storm
2hurricane
3severe thunderstorms
fourthunderstorms
fivemixed rain and snow
6mixed rain and sleet
7mixed snow and sleet
eightfreezing drizzle
9drizzle
tenfreezing rain
elevenshowers
12showers
13snow flurries
14light snow showers
15blowing snow
sixteensnow
17hail
18sleet
nineteendust
20foggy
21haze
22smoky
23blustery
24windy
25cold
26cloudy
27mostly cloudy (night)
28mostly cloudy (day)
29partly cloudy (night)
thirtypartly cloudy (day)
31clear (night)
32sunny
33fair (night)
34fair (day)
35mixed rain and hail
36hot
37isolated thunderstorms
38scattered thunderstorms
39scattered thunderstorms
40scattered showers
41heavy snow
42scattered snow showers
43heavy snow
44partly cloudy
45thundershowers
46snow showers
47isolated thundershowers
3200not available


The article was published on the paradigm.ru blog.

Related Links



Source: https://habr.com/ru/post/20461/


All Articles