Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

yetiboy

macrumors member
Original poster
Mar 23, 2011
35
0
I was inspired partly by Binarymix's post (https://forums.macrumors.com/posts/19010600/) and partly from seeing Windows desktops by people using Rainmeter to make a weather script that was more than just current conditions.

I've put together a script that takes data from the Environment Canada Atom feed for a location and produces files for current temperature, current conditions, forecast conditions and any weather warnings for the next 5 days and uses Binarymix's icons to get the proper icons for all days as well. Unfortunately, EC doesn't use weather codes that match up with icons like Yahoo does so I was forced to create a function to do that for me. I have the weather warnings geeklets (whether there is one or not) setup on top of each other on my desktop so if there's no warning I get a message saying no warnings in white to match the rest of my forecasts or in red if there's a warning.

If you're going to use this, you'll have to make several changes to the top of the file for folder locations and the proper XML file. All of the weather icons are in a subfolder called "icons" and the icons being used on my desktop are being output by the script to a folder called "output_icons", so you'll need to create those folders and make sure you put the icons you are using in the "icons" folder. Some tweaking will have to go on as I get new weather forecasts (eg. Sunny, Mix of sun and cloud, etc.) that I haven't encountered yet.

I hope I'm not the only Canadian who can use this.

yeti

Code:
<?php

date_default_timezone_set('put location/in here'); //eg. 'America/Toronto'
$EC_site_xml=file_get_contents('http://weather.gc.ca/rss/city/XX-XXX_X.xml'); //put in xml file for your city here
file_put_contents('/path/to/ECWeather/folder/ECWeather.xml',$EC_site_xml);
$xml=simplexml_load_file('/path/to/ECWeather/folder/ECWeather.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
$base_folder = "/path/to/ECWeather/folder/";

$forecast_output_file = '/path/to/ECWeather/folder/ECForecast';
$warning_yes_file = '/path/to/ECWeather/folder/ECWarningYes.txt';
$warning_no_file = '/path/to/ECWeather/folder/ECWarningNo.txt';
$current_file = '/Users/Brad/Documents/bin/ECWeather/ECCurrent.txt';
$temperature_file = '/path/to/ECWeather/folder/ECTemperature.txt';

$i=0;
foreach ($xml as $each_entry):
	$title=$each_entry->title;

	//Check for watches or warnings	
	if (strpos($title, "No watches or warnings in effect") !== false) {
		file_put_contents($warning_no_file, "No watches or warnings in effect");
		file_put_contents($warning_yes_file, "");
	}
	elseif (strpos($title, "Warning") || strpos($title, "Watch") || strpos($title, "SPECIAL") !==false) {
		$summary=$each_entry->summary;
		file_put_contents($warning_yes_file, $title."\n".$summary);
		file_put_contents($warning_no_file, "");
	}	
	//"Current Conditions" section
	elseif (strpos($title, "Current Conditions") !== false) {
		$cur_cond_summary=strip_tags($each_entry->summary);
		$cur_cond_summary=str_replace("°","°",$cur_cond_summary);
		file_put_contents($current_file, $cur_cond_summary);
		//Get current temperature and export to $temperature_file
		$comma_position=strpos($title,",") +2;
		$current_temp=substr($title,$comma_position);
		file_put_contents($temperature_file, $current_temp);
		//Strip away everything after the comma (ie. temp)
		$title=substr($title,0,strpos($title,","));
		//Get current conditions (after colon)
		$cur_cond_colon_position=strpos($title,":") +2;
		$current_icon = substr($title, $cur_cond_colon_position);

		$time_of_day=date('G');
		if ($time_of_day<5 OR $time_of_day>20) {
			$night=True;
		}
		else {
			$night=False;
		}

		$current_image_file=weather_icon($current_icon, $night);
		copy ($base_folder."/icons/".$current_image_file, $base_folder."/output_icons/current.png");
	}
	//Rest of forecast	
	elseif (strpos($title, ":") !== false) {
		//remove "High..." or "Low..." from title section
		if (strpos($title, 'High') !== false) {
			$title=substr($title,0,strpos($title, ". High"));
		}
		if (strpos($title, 'Low') !== false) {
			$title=substr($title,0,strpos($title, ". Low"));
		}
		$summary=$each_entry->summary;

		//remove "Forecast issued..." text from summary section
		if (strpos($summary, 'Forecast issued') !== false) {
			$summary=substr($summary,0,strpos($summary, "Forecast issued"));
		}
		//remove html tags and add degrees symbol
		$title=strip_tags($title);
		$title=str_replace("°","°",$title);
		$summary=strip_tags($summary);
		$summary=str_replace("°","°",$summary);

		//check for "night" in title for night images
		if (strpos($title, 'night') !== false) {
			$night=True;
		}
		else {
			$night=False;
		}
		$colon_position=strpos($title,":") + 2;
		$text_for_weather_icon[$i] = substr($title,$colon_position);
		$forecast_image_file[$i]=weather_icon($text_for_weather_icon[$i], $night);
//		echo $forecast_image_file[$i]."\n";
		copy ($base_folder."/icons/".$forecast_image_file[$i], $base_folder."/output_icons/forecast".$i.".png");
		//remove rest of title except for day
		$title=substr($title,0,strpos($title, ":"));		
		file_put_contents($forecast_output_file.$i.".txt",$title.": ".$summary);

		$i++;
	}
	
endforeach;
unset($each_entry);

//weather image function
function weather_icon($icon_text, $night_value) {

	switch ($icon_text)
		{
		case "Tornado":
			$icon_file="00.png";
			break;
		case "Hurricane":
			$icon_file="01.png";
			break;
		case "Tropical Storm":
			$icon_file="02.png";
			break;
		case "Isolated Thunderstorms": //NIGHT
			if ($night_value==True){
				$icon_file="03.png";
			}
			else { // DAY
				$icon_file="39.png";
			}
			break;			
		case "Thundershowers":
			$icon_file="04.png";
			break;
		case "Mixed Rain and Snow":
			$icon_file="05.png";
			break;
		case "Mixed Rain and Sleet":
			$icon_file="06.png";
			break;
		case "Mixed Snow and Sleet":
			$icon_file="07.png";
			break;
		case "Freezing Drizzle":
			$icon_file="08.png";
			break;
		case "Drizzle":
			$icon_file="09.png";
			break;
		case "Freezing Rain":
			$icon_file="10.png";
			break;
		case "Light Rain":
			$icon_file="11.png";
			break;
		case "Rain":
			$icon_file="12.png";
			break;		
		case "Snow Flurries":
			$icon_file="13.png";
			break;
		case "Light Snow Showers":
			$icon_file="14.png";
			break;
		case "Snow":
			$icon_file="15.png";
			break;
		case "Sleet":
			$icon_file="16.png";
			break;
		case "Hail":
			$icon_file="17.png";
			break;
		case "Chance of Freezing Rain":
			$icon_file="18.png";
			break;
		case "Blowing Snow":
			$icon_file="19.png";
			break;
		case "Haze":
			$icon_file="20.png";
			break;
		case "Dust":
			$icon_file="21.png";
			break;
		case "Foggy":
			$icon_file="22.png";
			break;
		case "Blustery":
			$icon_file="23.png";
			break;
		case "Windy":
			$icon_file="24.png";
			break;
		case "Cold":
			$icon_file="25.png";
			break;
		case "Cloudy":
			$icon_file="26.png";
			break;
		case "Mostly Cloudy": // NIGHT
			if ($night_value==True){
				$icon_file="29.png";
			}
			else { // DAY
				$icon_file="30.png";
			}
			break;
		case "Clear": // NIGHT
			$icon_file="31.png";
			break;
		case "Sunny":
			$icon_file="32.png";
			break;
		case "Partly Cloudy": // NIGHT
		case "A mix of sun and cloud":  //NOTE: falling through  (2 cases)
			if ($night_value==True){
				$icon_file="33.png";
			}
			else { // DAY
				$icon_file="34.png";
			}
			break;
		case "Hail":
			$icon_file="35.png";
			break;
		case "Hot":
			$icon_file="36.png";
			break;
		case "Mixed Rain and Hail":
			$icon_file="37.png";
			break;
		case "Showers":
			$icon_file="40.png";
			break;
		case "Heavy Snow":
			$icon_file="41.png";
			break;
		case "Scattered Snow Showers":
			$icon_file="42.png";
			break;
		case "Snow Showers":
			$icon_file="43.png";
			break;
		case "Fair":
			$icon_file="44.png";
			break;
		case "Thundershowers":
			$icon_file="45.png";
			break;
		case "Chance of showers":
			$icon_file="46.png";
			break;
	}
	return $icon_file;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.