Ticket #6262: google_weather.diff

File google_weather.diff, 8.1 KB (added by Markus Schulz <msc@…>, 12 years ago)

svn diff based on myhtplugins path.

  • mythweather/scripts/google/googlecurrentxml.pl

     
     1#! /usr/bin/perl -w
     2
     3use strict;
     4use XML::Simple;
     5require LWP::UserAgent;
     6use Getopt::Std;
     7use File::Basename;
     8
     9our ($opt_v, $opt_t, $opt_T, $opt_l, $opt_u);
     10
     11my $name = 'GOOGLE-Current-XML';
     12my $version = 0.1;
     13my $author = 'Markus Schulz';
     14my $email = 'msc@antzsystem.de';
     15my $updateTimeout = 120*60;
     16my $retrieveTimeout = 30;
     17my @types = ('cclocation', 'station_id', 'copyright',
     18    'observation_time', 'weather', 'temp', 'relative_humidity',
     19    'wind_dir', 'pressure', 'visibility', 'weather_icon',
     20    'appt', 'wind_spdgst');
     21my %iconmap = (
     22    "cloudy.gif"            , "cloudy.png",
     23    "rain.gif"              , "showers.png",
     24    "flurries.gif"          , "flurries.png",
     25    "fog.gif"               , "fog.png",
     26    "showers.gif"           , "showers.png",
     27    "sunny.gif"             , "sunny.png",
     28    "storm.gif"             , "thunshowers.png",
     29    "sleet.gif"             , "rainsnow.png",
     30    "icy.gif"               , "",
     31    "snow.gif"              , "snowshow.png",
     32    "mostly_sunny.gif"      , "pcloudy.png",
     33    "mostly_cloudy.gif"     , "mcloudy.png",
     34    "chance_of_rain.gif"    , "lshowers.png",
     35    "chance_of_snow.gif"    , "flurries.png",
     36    "chance_of_storm.gif"   , "thunshowers.png",
     37    ""                      , "unknown.png"
     38# "Unkown" , "fair.png"
     39    );
     40
     41getopts('Tvtlu:d:');
     42
     43if (defined $opt_v) {
     44    print "$name,$version,$author,$email\n";
     45    exit 0;
     46}
     47
     48if (defined $opt_T) {
     49    print "$updateTimeout,$retrieveTimeout\n";
     50    exit 0;
     51}
     52
     53if (defined $opt_l) {
     54
     55  my $search = shift;
     56  my $url = 'http://www.google.com/ig/api?weather=' . $search;
     57  my $ua = LWP::UserAgent->new;
     58  $ua->agent('Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.5ko/2008122011 Iceweasel/3.0.5 (Debian-3.0.5-1)');
     59  my $response = $ua->get($url);
     60  die unless defined $response;
     61  my $xml = XMLin($response->decoded_content);
     62  if (!$xml) {
     63    die;
     64  }
     65  if (defined $xml->{weather}->{problem_cause}) {
     66    exit 0;
     67  }
     68  print $search . "::" .  $xml->{weather}->{forecast_information}->{city}->{data}  . "\n";
     69  exit 0;
     70}
     71
     72if (defined $opt_t) {
     73    foreach (@types) {print; print "\n";}
     74    exit 0;
     75}
     76
     77my $locid = shift;
     78if (!defined $locid || $locid eq "") {
     79      die "Invalid usage";
     80}
     81
     82my $units = "SI";
     83if (defined $opt_u) {
     84  $units = $opt_u;
     85}
     86my $url = 'http://www.google.com/ig/api?weather=' . $locid;
     87if ($units =~ /SI/) {
     88#small hack to force SI value data
     89  $url .= "&hl=de";
     90}
     91my $ua = LWP::UserAgent->new;
     92$ua->agent('Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.5ko/2008122011 Iceweasel/3.0.5 (Debian-3.0.5-1)');
     93my $response = $ua->get($url);
     94die unless defined $response;
     95my $xml = XMLin($response->decoded_content);
     96if (!$xml) {
     97    die "Not xml";
     98}
     99
     100printf "appt::NA\n";
     101printf "visibility::?\n";
     102printf "weather::N/A\n";
     103
     104printf "copyright::Google Weather API\n";
     105printf "station_id::" . $locid . "\n";
     106printf "cclocation::" . $xml->{weather}->{forecast_information}->{city}->{data} . "\n";
     107printf "observation_time::" . $xml->{weather}->{forecast_information}->{current_date_time}->{data} . "\n";
     108my $icon = $xml->{weather}->{current_conditions}->{icon}->{data};
     109$icon =~ s/^\/images\/weather\/(.*)$/$1/;
     110printf "weather_icon::" . $iconmap{$icon}."\n";
     111if ($units =~ /SI/) {
     112  printf "temp::" . $xml->{weather}->{current_conditions}->{temp_c}->{data} . "\n";
     113}
     114else {
     115  printf "temp::" . $xml->{weather}->{current_conditions}->{temp_f}->{data} . "\n";
     116}
     117my $humidity = $xml->{weather}->{current_conditions}->{humidity}->{data};
     118$humidity =~ s/.*?(\d*)%$/$1/;
     119printf "relative_humidity::" . $humidity . "\n";
     120my $wind = $xml->{weather}->{current_conditions}->{wind_condition}->{data};
     121$wind =~ s/^Wind: ([^ ]+) [^ ]+ ([0-9]+).*/wind_dir::$1\nwind_spdgst::$2\n/;
     122printf $wind;
     123
     124
     125
  • mythweather/scripts/google/googlethreedayxml.pl

    EigenschaftsÀnderungen: mythweather/scripts/google/googlecurrentxml.pl
    ___________________________________________________________________
    HinzugefÃŒgt: svn:executable
       + *
    
     
     1#! /usr/bin/perl -w
     2
     3use strict;
     4use XML::Simple;
     5require LWP::UserAgent;
     6use Getopt::Std;
     7use File::Basename;
     8
     9our ($opt_v, $opt_t, $opt_T, $opt_l, $opt_u);
     10
     11my $name = 'GOOGLE-3day-XML';
     12my $version = 0.1;
     13my $author = 'Markus Schulz';
     14my $email = 'msc@antzsystem.de';
     15my $updateTimeout = 360*60;
     16my $retrieveTimeout = 30;
     17my @types = ('3dlocation', 'station_id', 'copyright', 'weather_icon',
     18        'date-0', 'icon-0', 'low-0', 'high-0',
     19        'date-1', 'icon-1', 'low-1', 'high-1',
     20        'date-2', 'icon-2', 'low-2', 'high-2', 'updatetime');
     21my %iconmap = (
     22    "cloudy.gif"            , "cloudy.png",
     23    "rain.gif"              , "showers.png",
     24    "flurries.gif"          , "flurries.png",
     25    "fog.gif"               , "fog.png",
     26    "showers.gif"           , "showers.png",
     27    "sunny.gif"             , "sunny.png",
     28    "storm.gif"             , "thunshowers.png",
     29    "sleet.gif"             , "rainsnow.png",
     30    "icy.gif"               , "",
     31    "snow.gif"              , "snowshow.png",
     32    "mostly_sunny.gif"      , "pcloudy.png",
     33    "mostly_cloudy.gif"     , "mcloudy.png",
     34    "chance_of_rain.gif"    , "lshowers.png",
     35    "chance_of_snow.gif"    , "flurries.png",
     36    "chance_of_storm.gif"   , "thunshowers.png",
     37    ""                      , "unknown.png"
     38# "Unkown" , "fair.png"
     39    );
     40
     41getopts('Tvtlu:d:');
     42
     43if (defined $opt_v) {
     44    print "$name,$version,$author,$email\n";
     45    exit 0;
     46}
     47
     48if (defined $opt_T) {
     49    print "$updateTimeout,$retrieveTimeout\n";
     50    exit 0;
     51}
     52
     53if (defined $opt_l) {
     54
     55  my $search = shift;
     56  my $url = 'http://www.google.com/ig/api?weather=' . $search;
     57  my $ua = LWP::UserAgent->new;
     58  $ua->agent('Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.5ko/2008122011 Iceweasel/3.0.5 (Debian-3.0.5-1)');
     59  my $response = $ua->get($url);
     60  die unless defined $response;
     61  my $xml = XMLin($response->decoded_content);
     62  if (!$xml) {
     63    die;
     64  }
     65  if (defined $xml->{weather}->{problem_cause}) {
     66    exit 0;
     67  }
     68  print $search . "::" .  $xml->{weather}->{forecast_information}->{city}->{data}  . "\n";
     69  exit 0;
     70}
     71
     72if (defined $opt_t) {
     73    foreach (@types) {print; print "\n";}
     74    exit 0;
     75}
     76
     77my $locid = shift;
     78if (!defined $locid || $locid eq "") {
     79      die "Invalid usage";
     80}
     81
     82my $units = "SI";
     83if (defined $opt_u) {
     84  $units = $opt_u;
     85}
     86my $url = 'http://www.google.com/ig/api?weather=' . $locid;
     87if ($units =~ /SI/) {
     88#small hack to force SI value data
     89  $url .= "&hl=de";
     90}
     91my $ua = LWP::UserAgent->new;
     92$ua->agent('Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.5ko/2008122011 Iceweasel/3.0.5 (Debian-3.0.5-1)');
     93my $response = $ua->get($url);
     94die unless defined $response;
     95my $xml = XMLin($response->decoded_content);
     96if (!$xml) {
     97    die "Not xml";
     98}
     99
     100
     101printf "copyright::Google Weather API\n";
     102printf "station_id::" . $locid . "\n";
     103printf "3dlocation::" . $xml->{weather}->{forecast_information}->{city}->{data} . "\n";
     104printf "updatetime::Updated " . $xml->{weather}->{forecast_information}->{current_date_time}->{data} . "\n";
     105
     106my $i = 0;
     107my $item;
     108foreach $item (@{$xml->{weather}->{forecast_conditions}}) {
     109
     110  my $icon = $item->{icon}->{data};
     111  $icon =~ s/^\/images\/weather\/(.*)$/$1/;
     112  printf "date-" . $i . "::" . $item->{day_of_week}->{data} . "\n";
     113  printf "low-" . $i . "::" . $item->{low}->{data} . "\n";
     114  printf "high-" . $i . "::" . $item->{high}->{data} . "\n";
     115  printf "icon-" . $i . "::" . $iconmap{$icon} ."\n";
     116  $i++;
     117}