From Networks & Transactions 1

Assignments: Assignment2David

Six degrees

Online version: http://ernie.art.yale.edu/~davidyun/weather/

index.php

<?php

// Get the weather functions
require 'weather_fns.php';

?>

<html>

<head>

<title>Six Degrees</title>

<style type="text/css" title="text/css">
<!--

        body {
                /*background-color: FFFF66#F7F7F0;*/
        background-color: #F7F7F0;
            margin:0px;
        padding: 0px;
        }
       
        td {
        font-family: Times, Georgia, serif;
        font-size: 11px;
        /* line-height: 16px; */
        color: #000000;
        font-weight: normal;

        }
       
        #imgtype {
                float:left;
        font-size: 13px;
                color:#000;
                background-color:#000;
        }

        #condition {
                float:left;
        font-size: 23px;
                color:#000;
                background-color:#000;
        }
       
        #clearboth {
                clear: both;
        }
        
        #container {
              text-align: left;
               width: 605px;
              margin-left: auto;
               margin-right: auto;
               font-family: times, serif;
        }
        #city {
                float: left;
        }
-->
</style>

</head>

<body>



<div align="center">

<table width="100%" height="100%">
 <tr align="center" valign="middle">
  <td>
 
   <table cellpadding="0" cellspacing="0" border="0" width="800">
    <tr>
         <td>


<form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Enter your zip: <input type="text" name="zip" size="5" value="<?php echo $_REQUEST['zip']; ?>" maxlength="5"/>
        <input type="submit" name="go" value="Go" />
</form>
<br />
         </td>
         <td>
<?php

$rand = rand(1, 10000000);
if ($_REQUEST['zip']) {
?>
        <form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <input type="hidden" name="zip" value="<?php echo $_REQUEST['zip']; ?>"/>
        <input type="hidden" name="rand" value="<?php echo $rand; ?>"/>
        <input type="submit" name="refresh" value="Refresh" />
        </form>
<?php } ?>
         </td>
        
        </tr>
       
        <tr>
         <td valign="top" height="300">
<?php

if ($_REQUEST['zip']) {
        $zip = $_REQUEST['zip'];
       
//      echo "<div id='city'>";
        display_weather($zip);
        echo "\n</td>\n\n";

//      echo "<div id='city'>";
        echo "\n<td valign=\"top\"  height=\"300\">\n\n";
        display_weather(false);

}

?>
         </td>
        </tr>
        <tr>
         <td colspan="2" height="50">
         </td>
        </tr>
   </table>
               
 </td>
 </tr>
</table>

</div>

</body>
</html>

weather_fns.php

<?php

// Get the cities list
require 'cities.php';



function display_weather ($zip)
{

        global $cities, $countryCodes;

        if ($zip) {
                $weather = get_yahooweather($zip);
        } else {
                // Loop until finding weather for a random city
                while (empty($city)) {
                        // Get a random city
                        $randomcity = rand(0,count($cities));
                        // Check the weather for that city
                        $weather = get_yahooweather($cities[$randomcity]);
                        $city = $weather['city'];
                }
        }
       
        $city = $weather['city'];
        $region = trim($weather['region']);
        $country = $weather['country'];
        $temperature = $weather['temperature'];
        $condition = $weather['condition'];
        $date = $weather['date'];

        // Check to make sure the city value is returned
        if (empty($city)) {
                echo "Sorry, we couldn't find the weather for your zip code. Please try again.";
                return false;
        }
       
        // Set the image search query
        if (!empty($region) && $region!="") {
                $citysearch = $city." ".$region;
        } else {
                $citysearch = $city." ".$countryCodes["$country"];
        }
       
        echo "\n\n<!-- Searched Yahoo images for: ".$citysearch." -->\n\n";
       
        // Get the image from Yahoo image search
        $imageinfo = get_yahooimage(rawurlencode($citysearch));
        $height = $imageinfo['height'];
        $url = $imageinfo['url'];
               

        // Set the color for this temp range
        if ($temperature >= 0  && $temperature < 5) { $tempcolor = "0000FF"; }
        if ($temperature >= 5  && $temperature < 10) { $tempcolor = "004AFF"; } 
        if ($temperature >= 10  && $temperature < 15) { $tempcolor = "0073FF"; }       
        if ($temperature >= 15  && $temperature < 20) { $tempcolor = "00A3FF"; }
        if ($temperature >= 20  && $temperature < 25) { $tempcolor = "00CCFF"; }
        if ($temperature >= 25  && $temperature < 30) { $tempcolor = "00E6FF"; }
        if ($temperature >= 30  && $temperature < 35) { $tempcolor = "00FFFF"; }
        if ($temperature >= 35  && $temperature < 40) { $tempcolor = "00FFB3"; }
        if ($temperature >= 40  && $temperature < 45) { $tempcolor = "7FFF00"; }
        if ($temperature >= 45  && $temperature < 50) { $tempcolor = "CEFF00"; }
        if ($temperature >= 50  && $temperature < 55) { $tempcolor = "FFFF00"; }
        if ($temperature >= 55  && $temperature < 60) { $tempcolor = "FFE600"; }
        if ($temperature >= 60  && $temperature < 65) { $tempcolor = "FFCC00"; }
        if ($temperature >= 65  && $temperature < 70) { $tempcolor = "FFAE00"; }
        if ($temperature >= 70  && $temperature < 75) { $tempcolor = "FF9900"; }
        if ($temperature >= 75  && $temperature < 80) { $tempcolor = "FF7F00"; }
        if ($temperature >= 80  && $temperature < 85) { $tempcolor = "FF4F00"; }
        if ($temperature >= 85  && $temperature < 90) { $tempcolor = "FF0000"; }
        if ($temperature >= 90  && $temperature < 95) { $tempcolor = "FF4545"; }
        if ($temperature >= 95  && $temperature < 100) { $tempcolor = "FF6868"; }
        if ($temperature >= 100  && $temperature < 105) { $tempcolor = "FF8787"; }
        if ($temperature >= 105  && $temperature < 110) { $tempcolor = "FF9E9E"; }
       
       

        echo "\t<div style='background: #".$tempcolor." url(".$url.") top left no-repeat; width:400; height:300; cursor:hand;' onClick=\"window.open('".$url."','image')\">\n";
        echo "\n\t<div id='imgtype' style='background-color: #".$tempcolor."'>".$city;
        if (!empty($region) && $region!="") { echo ", ".$region; }
        if (!empty($country)) { echo ", ".$countryCodes["$country"]; }
        echo "</div>\n";
        echo "\t<div id='clearboth'>\n";
        echo "\t<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />\n";
        echo "\t<div id='condition' style='background-color: #".$tempcolor."'>Currently ".$temperature."&deg;</div>\n";
        echo "\t<div id='clearboth'>\n";
        echo "\t<div id='condition' style='background-color: #".$tempcolor."'>and ".$condition."</div>\n";
        echo "\t</div>\n";
        echo "\t<br />";
       
        //display_image ($city,$country);
       
}




function display_image ($city, $country)
{
        $imageinfo = get_yahooimage(rawurlencode($city));

        $src = $imageinfo['url'];
        $height = $imageinfo['height'];
        $width = $imageinfo['width'];

        echo "<div style='z-index:1;'><img src='$src' width='400' border='0'/></div>";
       
}




function get_yahooimage ($query)
{

        // ----- LOAD THE XML (RSS) FEED

        // First just load the file as straight data. We need to alter it before we can begin.
        $xmlContents = file_get_contents("http://api.search.yahoo.com/ImageSearchService/V1/imageSearch?appid=6degrees&query=$query&results=10");

        echo "\n\n<!--".$query."-->\n\n";

        // Now load the string into SimpleXML
        $xml = simplexml_load_string($xmlContents);
       

        // ----- LOOP THROUGH RESULTS TO FIND AN IMAGE
       
        foreach ($xml->Result as $result) {
                $url = $result->Url;
                $height = $result->Height;
                $width = $result->Width;
                $fileSize = $result->FileSize;
                settype($fileSize,'int');
               
                // Make sure you have an link that works,
                // and return it when you find one
                if (fopen($url,'r')) {
                        return array ('url'=>$url, 'width'=>$width, 'height'=>$height);
                }
        }

}       





function get_yahooweather ($citycode)
{


        // ----- LOAD THE XML (RSS) FEED

        // First just load the file as straight data. We need to alter it before we can begin.
        $xmlContents = file_get_contents("http://xml.weather.yahoo.com/forecastrss?p=$citycode");
        $xmlContents = str_replace("yweather:", "", $xmlContents);
        $xmlContents = str_replace("geo:", "", $xmlContents);

        // Now load the string into SimpleXML
        $xml = simplexml_load_string($xmlContents);
       
        // ----- FOR CONVENIENCE, LOAD DATA WE CARE ABOUT INTO VARIABLES

        // <channel> node contains some of the info
        $channel = $xml->channel;

        // Location name: only the attributes are meaningful
        $location = $channel->location;
        $city = $location["city"]; // e.g. "New Haven"
        $region = $location["region"]; // e.g. "CT"
        $country = $location["country"]; // e.g. "US"

        // Units info: only the attributes are meaningful
        $units = $channel->units;
        $tempUnits = $units["temperature"]; // e.g. "F"

        // <item> node is within the <channel> node, contains additional info
        $item = $channel->item;
       
        // Condition data: only the attributes are meaningful
        $condition = $item->condition;
        $conditionText = $condition["text"]; // e.g. "Mostly Cloudy" (corresponds to code below)
        $conditionCode = $condition["code"]; // e.g. "26" (47 possibilities)
        $temp = $condition["temp"]; // e.g. 57
        $date = $condition["date"]; // e.g. Tue, 29 Nov 2005 3:56 pm PST
       
        // Make an array of the basic elements we need
        $simpleWeather = array('city'=>$city,'region'=>$region,'country'=>$country,'temperature'=>$temp,'condition'=>$conditionText,'date'=>$date);
        return $simpleWeather;

/*
        // <forecast> nodes are within the item nodes, one per forecasted day.
        // So this will be an array.
        $forecasts = $item->forecast;
       
        $distanceUnits = $units["distance"]; // e.g. "mi"
        $pressureUnits = $units["pressure"]; // e.g. "in"
        $speedUnits = $units["speed"]; // e.g. "mph"

        // Wind data: only the attributes are meaningful
        $wind = $channel->wind;
        $windChill = $wind["chill"]; // e.g. "57"
        $windDir = $wind["direction"]; // e.g. "350"
        $windSpeed = $wind["speed"]; // e.g. "7"

        // Atmosphere data: only the attributes are meaningful
        $atmosphere = $channel->atmosphere;
        $humidity = $atmosphere["humidity"]; // e.g. "93";
        $visibility = $atmosphere["visibility"]; // e.g. "1609";
        $pressure = $atmosphere["pressure"]; // e.g. "30.12";
        $rising = $atmosphere["rising"]; // e.g. "0";

        // Astronomy data: only the attributes are meaningful
        $astronomy = $channel->astronomy;
        $sunrise = $astronomy["sunrise"]; // e.g. "7:02 am";
        $sunset = $astronomy["sunset"]; // e.g. "4:51 pm";



        $lat = $item->lat; // Latitude, e.g. "37.39"
        $long = $item->long; // Longitude, e.g. "122.03"
*/



}




?>

cities.php

<?php

$cities = array(

//      Format:
//      "yahoo city code", // City, Country (or State)

/*      "NIXX0013",   // Maiduguri, Nigeria
        "IZXX0008",     // Baghdad, Iraq
        "AFXX0003",     // Kabul, Afghanistan
        "PKXX0006",     // Pakistan
        "HAXX0004",     // Port-au-Prince, Haiti
        "PKXX0011",     // Pakistan
        "SOXX0002",     // Somalia
        "KNXX0006",     // Pyongyang, North Korea
        "USDC0001"      // Washington, DC
*/
     
        // New York Times, International Section
        // Sunday, March 19, 2006, 19:16 EST
        "IZXX0008",     // Baghdad, Iraq     
        "USDC0001",     // Washington, DC
        "BOXX0005", // MINSK, Belarus
        "FRXX0076", // Paris, France
        "CHXX0255", // DONGXIANG, China
        "SPXX0050", // MADRID, Spain
        "YIXX0005", // BELGRADE, Serbia (yahoo calls it Yugoslavia)
        "ISXX0010", // JERUSALEM, Israel
        "IRXX0018", // TEHRAN, Iran
        "NLXX0016", // THE HAGUE, the Netherlands
        "USLA0079", // CHALMETTE, Louisiana
        "IZXX0008",     // Baghdad, Iraq
        "AFXX0003", // GHAZNI, Afghanistan (Kabul was the closest city on Yahoo)
        "USAS0001", // PAGO PAGO, American Samoa
        "MXJO0693", // JUCHITAN, Mexico
        "AEXX0004", //DUBAI, United Arab Emirates
       
        // BBC News, Home Page
        // Sunday, March 19, 2006, 20:16 EST
        "BOXX0005", // MINSK, Belarus
        "ASXX0060", //Queensland at Innisfail, Australia
        "FRXX0076", // Paris, France
        "WEXX0005", // Ramallah, Palestinian Authority
        "INXX0188" //Guwahati, India
);


$countryCodes = array(
        "NI"=>"Nigeria",
        "HA"=>"Haiti",
        "KN"=>"North Korea",
        "IZ"=>"Iraq",
        "AF"=>"Afghanistan",
        "PK"=>"Pakistan",
        "US"=>"United States",
        "SO"=>"Somalia",
        "FR"=>"France",
        "CH"=>"China",
        "SP"=>"Spain",
        "YI"=>"Serbia",
        "IS"=>"Israel",
        "IR"=>"Iran",
        "NL"=>"the Netherlands",
        "MX"=>"Mexico",
        "AS"=>"Australia",
        "WE"=>"Palestinian Authority",
        "IN"=>"India",
        "BO"=>"Belarus"
);


?>

Retrieved from http://linkedbyair.net/nt1/wiki/Assignments/Assignment2David
Page last modified on April 23, 2006, at 05:20 PM