Ticket #3337: weather_com.php

File weather_com.php, 3.9 KB (added by maverik044, 13 years ago)

main script to get weather (current and forecast) from weather.com xml source

Line 
1#!/usr/bin/php
2<?php
3// Script for mythweather to fetch weather information from weather.com
4
5
6// Load the weather classes
7require_once './WeatherSite.php';
8require_once './Forecast.php';
9
10
11$name = 'weather_com';
12$version = 0.1;
13$author = 'William Stewart';
14$email = 'level42@sympatico.ca';
15$updateTimeout = 15*60;
16$retrieveTimeout = 30;
17$types = array('cclocation', 'station_id',
18        'observation_time', 'weather',
19        'temp', 'relative_humidity',
20        'wind_dir', 'wind_degrees', 'wind_speed', 'wind_gust',
21        'pressure', 'dewpoint', '3dlocation', '6dlocation',
22        'visibility', 'weather_icon', 'appt', 'wind_spdgst',
23        'high-0', 'high-1', 'high-2', 'high-3', 'high-4', 'high-5',
24        'low-0', 'low-1', 'low-2', 'low-3', 'low-4', 'low-5',
25        'icon-0', 'icon-1', 'icon-2', 'icon-3', 'icon-4', 'icon-5',
26        'date-0', 'date-1', 'date-2', 'date-3', 'date-4', 'date-5');
27
28if ($argc == 1) {
29  echo "No parameters supplied exiting..\n";
30  exit;
31}
32
33if ($argv[1] == "-v") {
34  echo $name.",".$version.",".$author.",".$email."\n";
35  exit;
36}
37
38if ($argv[1] == "-T") {
39  echo $updateTimeout.",".$retrieveTimeout."\n";
40  exit;
41}
42
43if ($argv[1] == "-t") {
44  $size=sizeof($types);
45  for($x = 0; $x < $size; $x++) {
46    echo $types[$x]."\n";
47  }
48
49}
50
51if ($argv[1] == "-l") {
52  foreach (file('../accid.dat') as $line) {
53   list($ignore, $code, $name) = explode('::', $line);
54   
55   if (stripos($name,$argv[2]) !== FALSE) {
56      echo $code."::".$name;
57   }
58  }
59 
60  exit;
61}
62
63$use_metric = 'NO';
64
65for ($x = 1; $x < $argc; $x++) {
66   if ($argv[$x] == "-d") {
67      $dir = $argv[$x + 1];
68   }
69   
70   if ($argv[$x] == "-u") {
71     if ($argv[$x + 1] == "SI") {
72        $use_metric = 'YES';
73     }
74   }
75}
76
77$loc = $argv[$argc - 1];
78
79$weather = new WeatherSite($loc, " ", $use_metric);
80
81// Output the current conditions information
82echo "cclocation::".$weather->location."\n";
83echo "station_id::".$loc."\n";
84echo "observation_time::".$weather->LastUpdated."\n";
85echo "weather::".$weather->ConditionText."\n";
86echo "temp::".$weather->Temperature."\n";
87echo "appt::".$weather->Real."\n";
88echo "relative_humidity::".$weather->Humidity."\n";
89if ($weather->WindDirection == "N") {echo "wind_dir::North\n";}
90elseif ($weather->WindDirection == "NE") {echo "wind_dir::Northeast\n";}
91elseif ($weather->WindDirection == "E") {echo "wind_dir::East\n";}
92elseif ($weather->WindDirection == "SE") {echo "wind_dir::Southeast\n";}
93elseif ($weather->WindDirection == "S") {echo "wind_dir::South\n";}
94elseif ($weather->WindDirection == "SW") {echo "wind_dir::Southwest\n";}
95elseif ($weather->WindDirection == "W") {echo "wind_dir::West\n";}
96elseif ($weather->WindDirection == "NW") {echo "wind_dir::Northwest\n";}
97else {echo "wind_dir::".$weather->WindDirection."\n";}
98
99echo "wind_degrees::".$weather->WindDegrees."\n";
100echo "wind_speed::".$weather->WindSpeed."\n";
101echo "wind_gust::".$weather->WindGust."\n";
102echo "wind_spdgst::".$weather->WindSpeed." (".$weather->WindGust.")\n";
103echo "pressure::".$weather->BarometricPressure."\n";
104echo "dewpoint::".$weather->DewPoint."\n";
105echo "visibility::".$weather->Visibility."\n";
106echo "weather_icon::".$weather->ConditionImage."\n";
107
108// Output the forecast information
109echo "3dlocation::".$weather->location."\n";
110echo "6dlocation::".$weather->location."\n";
111
112for ($x=0; $x < 6; $x++) {
113echo "high-".$x."::".$weather->Forecast[$x]->HighTemperature."\n";
114echo "low-".$x."::".$weather->Forecast[$x]->LowTemperature."\n";
115echo "icon-".$x."::".$weather->Forecast[$x]->DescImage."\n";
116
117switch ($weather->Forecast[$x]->dayofweek) {
118            case 0: echo "date-".$x."::Sunday\n"; break;
119            case 1: echo "date-".$x."::Monday\n"; break;
120            case 2: echo "date-".$x."::Tuesday\n"; break;
121            case 3: echo "date-".$x."::Wednesday\n"; break;
122            case 4: echo "date-".$x."::Thursday\n"; break;
123            case 5: echo "date-".$x."::Friday\n"; break;
124            case 6: echo "date-".$x."::Saturday\n"; break;
125             }
126
127}
128