Unix Weather equivalent

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of 37mm 37mm 11 months, 1 week ago.

  • Author
    Posts
  • #33487
    Profile photo of 37mm
    37mm
    Participant

    Made the powershell version, Cheers!

    function Weather { 
    
        $url = "http://checkip.dyndns.com" 
    	$webclient = New-Object System.Net.WebClient
    	$Ip = $webclient.DownloadString($url)
    	$Ip2 = $Ip.ToString()
    	$ip3 = $Ip2.Split(" ")
    	$ip4 = $ip3[5]
    	$ip5 = $ip4.replace("","")
    	$IPAddress = $ip5.replace("","")
        $infoService = "http://freegeoip.net/xml/$ipaddress"
        $geoip = Invoke-RestMethod -Method Get -URI $infoService   
        $city = $geoip.Response.City
        $country = $geoip.Response.CountryName
        $api = "692447105f67a34c06d1cbeab56b17c5"
        [xml]$wr = Invoke-WebRequest "api.openweathermap.org/data/2.5/weather?q=$City,$country&APPID=$api&mode=xml"
        $data = $wr.current
        $weather = [PSCustomObject]@{
                                    City       =  $data.city.name + ","+ $data.city.country
                                    Weather    =  $data.weather.value
                                    TempNOW    = [string]([math]::Round(($data.temperature.min - 273.15)*9/5+32))+'°F' 
                                    tempMAX    = [string]([math]::Round(($data.temperature.max - 273.15)*9/5+32))+'°F'
                                    TempMIN    = [string]([math]::Round(($data.temperature.min - 273.15)*9/5+32))+'°F'
                                    humidity   =  $data.humidity.value + $data.humidity.unit
                                    Clouds     =  $data.clouds.name
                                    Rain       =  $data.precipitation.mode
                                    Wind       =  $data.wind.Value
                                    pressure   =  $data.pressure.value
    }
        return $weather
    }
    
  • #33503
    Profile photo of Flynn Bundy
    Flynn Bundy
    Participant

    Good little project to get you familiar with xml and web requests, I couldn't get your code to work so I redid it.

    $x = Invoke-WebRequest "http://freegeoip.net/xml/" -UseBasicParsing
    $Country = ($x.Content | Select-String '\w+\/\w+' -AllMatches).Matches.Value
    [xml]$data = Invoke-WebRequest "api.openweathermap.org/data/2.5/weather?q=$(($Country | sls '\w+$' -AllMatches).Matches.Value),$(($Country | sls '^\w+' -AllMatches).Matches.Value)&APPID=692447105f67a34c06d1cbeab56b17c5&mode=xml"
    
      [PSCustomObject]@{
      City       =  $data.current.city.name + ","+ $data.current.city.country
      Weather    =  $data.current.weather.value
      TempNOW    = [string]([math]::Round(($data.current.temperature.min - 273.15)*9/5+32))+'°F' 
      tempMAX    = [string]([math]::Round(($data.current.temperature.max - 273.15)*9/5+32))+'°F'
      TempMIN    = [string]([math]::Round(($data.current.temperature.min - 273.15)*9/5+32))+'°F'
      humidity   =  $data.current.humidity.value + $data.current.humidity.unit
      Clouds     =  $data.current.clouds.name
      Rain       =  $data.current.precipitation.mode
      Wind       =  $data.current.wind.speed.name
      pressure   =  $data.current.pressure.value
      }
    

    But this just reminds me of how cold it is in Europe this time of year 🙁

  • #33504
    Profile photo of 37mm
    37mm
    Participant

    I am using powershell on win8.1 (version 4)
    Do you mind telling me what didn't work on your setup?

You must be logged in to reply to this topic.