Weather Grabber 
Monday, July 4, 2011, 12:11
Posted by Administrator
For the last couple of days I have been working the chat server and client that I made last semester. Adding additional features to it has been slow but satisfying work.

A weather function has been at the top of my list for things I wanted to see myself implement, so last night I decided to get to working on making something to retrieve the weather.

I've been using the bash script below to get weather information for months now:

#!/bin/bash

var_url="http://www.google.com/ig/api?weather=72959&hl=en"

var_weather_wget=`wget -q $var_url -O -`
var_weather_xml=`echo "$var_weather_wget" | sed 's/<forecast_conditions>.*//'`
var_weather=`echo "$var_weather_xml" | sed 's/></>\n</g'`
var_date=`echo "$var_weather" | grep -e '<forecast_date' | \
sed -e 's/<forecast_date data="//' -e 's/"\/>//'`
var_city=`echo "$var_weather" | grep -e '<city' | \
sed -e 's/<city data="//' -e 's/"\/>//'`
var_condition=`echo "$var_weather" | grep -e '<condition' | \
sed -e 's/<condition data="//' -e 's/"\/>//'`
var_temp_f=`echo "$var_weather" | grep -e '<temp_f' | \
sed -e 's/<temp_f data="//' -e 's/"\/>//'`
var_temp_c=`echo "$var_weather" | grep -e '<temp_c' | \
sed -e 's/<temp_c data="//' -e 's/"\/>//'`
var_humidity=`echo "$var_weather" | grep -e '<humidity' | \
sed -e 's/<humidity data="//' -e 's/"\/>//'`
var_wind=`echo "$var_weather" | grep -e '<wind' | \
sed -e 's/<wind_condition data="//' -e 's/"\/>//'`

echo "Date: $var_date - City: $var_city"
echo "Condition: $var_condition"
echo "Temp: $var_temp_c C"
echo "$var_humidity"
echo "$var_wind"


This has been working well, but I wanted to make something more extensible with Java that I could add to my chatserver and run with any zipcode instead of just having it set to one location.

After finagling around a little I was able to come up with something I'm liking pretty well that fit great into the server I'm putting together:

This has been working well, but I wanted to make something more extensible with Java that I could add to my chatserver and run with any zipcode instead of just having it set to one location.

After finagling around a little I was able to come up with something I'm liking pretty well that fit great into the server I'm putting together:

/*   < <  Current Weather > >      |
Forecast from Google's API |
Spike Snell |
2011 |
----------------------------------*/

import java.io.*;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class weather {

public static void main(String[] args) throws Exception {

if (args.length > 0)
System.out.println(toString(args[0]));
else
System.out.println("Please enter a valid 5 digit zipcode. Example: java weather 72701");

}

public static BufferedReader read(String url) throws Exception {
return new BufferedReader(new InputStreamReader(new URL(url).openStream()));
}

public static String toString(String zip) throws Exception {

// variables to hold the weather
String date = null;
String city = null;
String condition = null;
String tempC = null;
String humidity = null;
String wind = null;

// declare the pattern and matcher
Pattern p;
Matcher m;

// check to make sure the string is the right number of characters
if (zip.length() != 5)
return "Please enter the correct number of digits.";

// and make sure all 5 of the characters are numbers
p = Pattern.compile("\\d{5}");
m = p.matcher(zip);
if (m.find() == false)
return "Please enter a valid 5 digit zipcode.";

// open up a connection to google's weather API
BufferedReader reader = read("http://www.google.com/ig/api?weather="+ zip);
String page = reader.readLine();

// get the date
p = Pattern.compile("<forecast_date data=\"(.*?)\"/>");
m = p.matcher(page);
if (m.find() == true)
date = m.group(1);

// get the city
p = Pattern.compile("<city data=\"(.*?)\"/>");
m = p.matcher(page);
if (m.find() == true)
city = m.group(1);

// get the condition
p = Pattern.compile("<condition data=\"(.*?)\"/>");
m = p.matcher(page);
if (m.find() == true)
condition = m.group(1);

// get the temperature in C
p = Pattern.compile("<temp_c data=\"(.*?)\"/>");
m = p.matcher(page);
if (m.find() == true)
tempC = m.group(1);

// get the humidity
p = Pattern.compile("<humidity data=\"(.*?)\"/>");
m = p.matcher(page);
if (m.find() == true)
humidity = m.group(1);

// get the humidity
p = Pattern.compile("<wind_condition data=\"(.*?)\"/>");
m = p.matcher(page);
if (m.find() == true)
wind = m.group(1);

// if the date is still null there was a problem
if (date == null) {
return "There was a problem retrieving the weather.";
}

else {
// return the forecast
return city + " - " + date +
"\nCondition: " +
condition+ "\nTemp: " +
tempC + " *C\n" +
humidity + "\n" +
wind;
}
}
}

What it does is taken in a command line argument for the zipcode and after a bit of error checking (Making sure the zipcode is actually 5 numbers) it grabs the data from google's weather API, runs it through some regexes to grab the data I want and then returns everything as a string.

Here is an example of it in use:

nrg@AKIRA ~/Desktop $ java weather 72701
Fayetteville, AR - 2011-07-04
Condition: Mostly Cloudy
Temp: 27 *C
Humidity: 74%
Wind: S at 5 mph


Any feedback on improving this code or any other comments about it would be much appreciated.

add comment ( 7236 views )   |  permalink   |  $star_image$star_image$star_image$star_image$star_image ( 3 / 5249 )
Tennessee outlaws offensive imagery  
Thursday, June 9, 2011, 12:14
Posted by Administrator
A law was recently updated in Tennessee making it illegal for any resident to transmit or display any image which causes "emotional distress". Breaking this new law can cause you to receive up to a year of jailtime or fines not exceeding $2,500.

This is a blatant violation of the right to free speech and as some have pointed out the law even violates itself by being so offensive. Just the fact that something like this can get passed into effect has to make you wonder if people are really this misinformed about electronic communication or if they are just trying to pass something so ridiculous that people take notice.

The problem isn't even primarily that the law against obscenity (an undeniable trait of humanity) but that its so up to interpretation that it could be applied to almost anything.

Requirements for breaking this law are as follows:
(a) A person commits an offense who intentionally:

(4) Communicates with another person or transmits or displays an image in a manner in which there is a reasonable expectation that the image will be viewed by the victim by [by telephone, in writing or by electronic communication] without legitimate purpose:

(A) (i) With the malicious intent to frighten, intimidate or cause emotional distress; or

(ii) In a manner the defendant knows, or reasonably should know, would frighten, intimidate or cause emotional distress to a similarly situated person of reasonable sensibilities; and

(B) As the result of the communication, the person is frightened, intimidated or emotionally distressed.


Its all enough to make one relate to a gloomy proposition put forth in a book called Atlas Shrugged over a decade ago:
"Did you really think we want those laws observed?" said Dr. Ferris. "We want them to be broken. You'd better get it straight that it's not a bunch of boy scouts you're up against... We're after power and we mean it... There's no way to rule innocent men. The only power any government has is the power to crack down on criminals. Well, when there aren't enough criminals one makes them. One declares so many things to be a crime that it becomes impossible for men to live without breaking laws. Who wants a nation of law-abiding citizens? What's there in that for anyone? But just pass the kind of laws that can neither be observed nor enforced or objectively interpreted – and you create a nation of law-breakers – and then you cash in on guilt. Now that's the system, Mr. Reardon, that's the game, and once you understand it, you'll be much easier to deal with." ('Atlas Shrugged' 1957)


Hopefully this law will immediately be removed or drastically modified due to all of the attention it has been getting in the last few days.
add comment ( 6408 views )   |  permalink   |  $star_image$star_image$star_image$star_image$star_image ( 3 / 5168 )
Strobe Light  
Tuesday, June 7, 2011, 12:17
Posted by Administrator
Over the last few days I've been working on a small JavaScript/PHP website to turn a monitor into a strobe light.

I used to use a windows program called flasher3000 that would do this same effect but decided it would be more fun just to make my own than to get a windows machine up and running just to strobe for me.

Getting JavaScript to change the background color back and forth was the easiest part, its just a matter of setting document.bgColor to a new value in a loop and then using setTimeout to pause for a few hundred milliseconds. After wrapping that in a function and calling it with an onLoad the page starts running the strobe.

A day or two after playing with this idea I decided to write some PHP to pick a pseudo-random color and speed from an array and send that to the JavaScript so that every time the page is refreshed the strobe likely has different settings.

The complete code for the strobe is below:
<html>
<head>
<title>strobe - refresh to change settings</title>

<?php
// Colors array
$colors = array('ff0000','ffff00','ffffff','00ffff','0000ff','ff00ff','00ff00');
// Pick a random key for a color
$randomColor = array_rand($colors);
// Get the color at that key
$color = $colors[$randomColor];

// Refresh rate array
$speeds = array('16.666','41.666','55','65','75','80','83.333','90','100','110','120','130','140','150');
// Pick a random key for a speei
$randomSpeed = array_rand($speeds);
// Get the speei at that key
$speed = $speeds[$randomSpeed];
?>

<script>
function strobe() {
document.bgColor = document.bgColor == '#<?php echo $color ?>' ? '#000000' : '#<?php echo $color ?>';
setTimeout('strobe()', <?php echo $speed ?>);
}
</script>

</head>
<body onload='strobe();'>

<?php
echo "<font color='" . $color . "'>" . $color . " - " . $speed . "ms</font>";
?>

</body>
</html>

Here is a link to the strobelight itself.

Its worked on all the browsers I've tested it on so far after I debugged a small problem where IE wasn't interpreting uppercase RGB hex values correctly.

Additional things I'm thinking about adding to it are key inputs to cycle through the colors or possibly form the colors themselves. As well as keys to change the refresh rate so that the strobe light can be even more customizable on the fly without having to refresh the page.
add comment ( 6545 views )   |  permalink   |  $star_image$star_image$star_image$star_image$star_image ( 3 / 5280 )

<<First <Back | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Next> Last>>