Updated and sanitized code

This commit is contained in:
2021-12-21 10:29:20 +01:00
parent 1e31a5d4ce
commit 582151d128

191
datex.ps1
View File

@@ -1,13 +1,30 @@
# Used everytime
$User = "username"
#$password = Get-Content /home/httpd/sites/datex/cred.txt | ConvertTo-SecureString
#$credential = New-Object System.Management.Automation.PsCredential($User,$password)
#[xml]$Weather = Get-Content "C:\Users\Morten\Downloads\GetMeasuredWeatherData.xml" # Magic
#[xml]$Weather = Invoke-WebRequest -Credential (Get-Credential) -Uri https://www.vegvesen.no/ws/no/vegvesen/veg/trafikkpublikasjon/vaer/2/GetMeasuredWeatherData #[xml]$Weather = Invoke-WebRequest -Credential $credential https://www.vegvesen.no/ws/no/vegvesen/veg/trafikkpublikasjon/vaer/2/GetMeasurementWeatherSiteTable
$securepassword = ConvertTo-SecureString "password" -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($User, $securepassword)
[xml]$Weather = Invoke-WebRequest -Uri "https://www.vegvesen.no/ws/no/vegvesen/veg/trafikkpublikasjon/vaer/2/GetMeasuredWeatherData" -Credential $credentials
#MET
Invoke-webrequest https://api.met.no/weatherapi/textforecast/2.0/?forecast=landoverview -Outfile "response.xml"
[xml]$met = New-Object -TypeName xml
$met.load("response.xml")
$ostlandet_24 = $met.textforecast.time[0].forecasttype.location[0].innertext | Out-File /home/httpd/sites/datex/met24.inc
$ostlandet_48 = $met.textforecast.time[1].forecasttype.location[0].innertext | Out-File /home/httpd/sites/datex/met48.inc
# MET SLUTT
$EndResult = @() $EndResult = @()
#$IDs = "206","207","228","1125","277","95" $IDs = "206","217","342","1740","73","175","202","142","341","277","210","249","163"
$IDs = "1740","206","174"
Foreach ($place in $IDs) { Foreach ($place in $IDs) {
$Location = $Weather.d2LogicalModel.payloadPublication.siteMeasurements | Where-Object { $_.measurementsitereference.id -eq $place } $Location = $Weather.d2LogicalModel.payloadPublication.siteMeasurements | Where-Object { $_.measurementsitereference.id -eq $place }
@@ -17,14 +34,16 @@ Foreach ($place in $IDs) {
$LocationRoadTemp = ($Location.measuredValue | Where-Object {$_.index -eq "801"}).innertext $LocationRoadTemp = ($Location.measuredValue | Where-Object {$_.index -eq "801"}).innertext
[float]$LocationWindSpeed = ($Location.measuredValue | Where-Object {$_.index -eq "901"}).innertext [float]$LocationWindSpeed = ($Location.measuredValue | Where-Object {$_.index -eq "901"}).innertext
#$LocationWindDir = ($Location.measuredValue | Where-Object {$_.index -eq "901"}).innertext #$LocationWindDir = ($Location.measuredValue | Where-Object {$_.index -eq "901"}).innertext
$LocationVisibility = ($Location.measuredValue | Where-Object {$_.index -eq "1401"}).innertext # $LocationVisibility = ($Location.measuredValue | Where-Object {$_.index -eq "1401"}).innertext
$LocationPrecipitation = ($Location.measuredValue | Where-Object {$_.index -eq "2501"}).innertext + " mm/h" $LocationPrecipitation = ($Location.measuredValue | Where-Object {$_.index -eq "2501"}).innertext + " mm/h"
$LocationSnowDepth = ($Location.measuredValue | Where-Object {$_.index -eq "3301"}).innertext # $LocationSnowDepth = ($Location.measuredValue | Where-Object {$_.index -eq "3301"}).innertext
if (!$LocationSnowDepth) {$LocationSnowDepth = "-"} #if (!$LocationSnowDepth) {$LocationSnowDepth = "-"}
if (!$LocationWindSpeed) {[string]$LocationWindSpeed = "-"} else { $LocationWindSpeed = $LocationWindSpeed*5/18 } # Omregning fra km/t til m/s if (!$LocationWindSpeed) {[string]$LocationWindSpeed = "-"} else { $LocationWindSpeed = $LocationWindSpeed*5/18 } # Omregning fra km/t til m/s
if (!$LocationVisibility) {$LocationVisibility = "-"} if (!$LocationVisibility) {$LocationVisibility = "-"}
$Place = $Place.Replace("206","Lygna").Replace("207","Bekkehallum").Replace("1740","Vingromsåsen").Replace("174","Eina") $Place = $Place.Replace("206","Lygna").Replace("217","Bekkehallum").Replace("342","Vardalsåsen").Replace("1740","Vingromåsen").Replace("73","Eggemoen").Replace("175","Stryken").Replace("202","Brovoll").Replace("142","Gjelleråsen").Replace("341","Tonsåsen").Replace("277","Andelva").Replace("210","Minnesund").Replace("249","Espa").Replace("163","Vikselv")
$Resultname = [PSCustomObject][ordered]@{ $Resultname = [PSCustomObject][ordered]@{
Lokasjon = $place Lokasjon = $place
Lufttemperatur = $LocationAirTemp Lufttemperatur = $LocationAirTemp
@@ -32,9 +51,9 @@ Foreach ($place in $IDs) {
Veibanetemperatur = $LocationRoadTemp Veibanetemperatur = $LocationRoadTemp
Nedbørintensitet = $LocationPrecipitation Nedbørintensitet = $LocationPrecipitation
Vindhastighet = $LocationWindSpeed Vindhastighet = $LocationWindSpeed
Snødybde = $LocationSnowDepth # Snødybde = $LocationSnowDepth
Luftfuktighet = $LocationHumidity Luftfuktighet = $LocationHumidity
Siktrekkevidde = $LocationVisibility # Siktrekkevidde = $LocationVisibility
} }
$EndResult += $ResultName $EndResult += $ResultName
} }
@@ -42,8 +61,23 @@ Foreach ($place in $IDs) {
$EndResult|Format-Table $EndResult|Format-Table
$fragments = @() $fragments = @()
$fragments += @'
$fragments+= "<H1>Værstatistikk</H1>" <script>
$(document).ready(function(){
var refreshUsers = setInterval(function() {
$("#Container").load("mestatable.inc");
}, 1000); // 300000 ms = 5 min
});
</script>
<script type="text/javascript">
setInterval("my_function();",1000);
function my_function(){
$('#refresh').load(location.href + ' #time');
}
</script>
'@
$fragments+= '<H1><img src="logo-mesta1.png"> Værstatistikk </H1>'
[xml]$html = $EndResult | convertto-html -Fragment [xml]$html = $EndResult | convertto-html -Fragment
# Lufttemp # Lufttemp
@@ -52,13 +86,13 @@ for ($i=1;$i -le $html.table.tr.count-1;$i++) {
$class = $html.CreateAttribute("class") $class = $html.CreateAttribute("class")
$class.value = 'minus' $class.value = 'minus'
$html.table.tr[$i].childnodes[1].attributes.append($class) | out-null $html.table.tr[$i].childnodes[1].attributes.append($class) | out-null
$html.table.tr[$i].childnodes[1].'#text' = ($html.table.tr[$i].childnodes[1].'#text') + " °C" $html.table.tr[$i].childnodes[1].innertext = ($html.table.tr[$i].childnodes[1].innertext) + " °C"
} }
else { else {
$class = $html.CreateAttribute("class") $class = $html.CreateAttribute("class")
$class.value = 'pluss' $class.value = 'pluss'
$html.table.tr[$i].childnodes[1].attributes.append($class) | out-null $html.table.tr[$i].childnodes[1].attributes.append($class) | out-null
$html.table.tr[$i].childnodes[1].'#text' = ($html.table.tr[$i].childnodes[1].'#text') + " °C" $html.table.tr[$i].childnodes[1].innertext = ($html.table.tr[$i].childnodes[1].innertext) + " °C"
} }
} }
# Duggpunkt # Duggpunkt
@@ -67,13 +101,13 @@ for ($i=1;$i -le $html.table.tr.count-1;$i++) {
$class = $html.CreateAttribute("class") $class = $html.CreateAttribute("class")
$class.value = 'minus' $class.value = 'minus'
$html.table.tr[$i].childnodes[2].attributes.append($class) | out-null $html.table.tr[$i].childnodes[2].attributes.append($class) | out-null
$html.table.tr[$i].childnodes[2].'#text' = ($html.table.tr[$i].childnodes[2].'#text') + " °C" $html.table.tr[$i].childnodes[2].InnerText = ($html.table.tr[$i].childnodes[2].'#text') + " °C"
} }
else { else {
$class = $html.CreateAttribute("class") $class = $html.CreateAttribute("class")
$class.value = 'pluss' $class.value = 'pluss'
$html.table.tr[$i].childnodes[2].attributes.append($class) | out-null $html.table.tr[$i].childnodes[2].attributes.append($class) | out-null
$html.table.tr[$i].childnodes[2].'#text' = ($html.table.tr[$i].childnodes[2].'#text') + " °C" $html.table.tr[$i].childnodes[2].InnerText = ($html.table.tr[$i].childnodes[2].'#text') + " °C"
} }
} }
# Veibanetemp # Veibanetemp
@@ -82,35 +116,39 @@ for ($i=1;$i -le $html.table.tr.count-1;$i++) {
$class = $html.CreateAttribute("class") $class = $html.CreateAttribute("class")
$class.value = 'minus' $class.value = 'minus'
$html.table.tr[$i].childnodes[3].attributes.append($class) | out-null $html.table.tr[$i].childnodes[3].attributes.append($class) | out-null
$html.table.tr[$i].childnodes[3].'#text' = ($html.table.tr[$i].childnodes[3].'#text') + " °C" $html.table.tr[$i].childnodes[3].InnerText = ($html.table.tr[$i].childnodes[3].'#text') + " °C"
} }
else { else {
$class = $html.CreateAttribute("class") $class = $html.CreateAttribute("class")
$class.value = 'pluss' $class.value = 'pluss'
$html.table.tr[$i].childnodes[3].attributes.append($class) | out-null $html.table.tr[$i].childnodes[3].attributes.append($class) | out-null
$html.table.tr[$i].childnodes[3].'#text' = ($html.table.tr[$i].childnodes[3].'#text') + " °C" $html.table.tr[$i].childnodes[3].InnerText = ($html.table.tr[$i].childnodes[3].'#text') + " °C"
} }
} }
# Vindhastighet # Vindhastighet
for ($i=1;$i -le $html.table.tr.count-1;$i++) { for ($i=1;$i -le $html.table.tr.count-1;$i++) {
if ($html.table.tr[$i].td[5] -ne "-") { if ($html.table.tr[$i].td[5] -ne "-") {
$html.table.tr[$i].childnodes[5].'#text' = ($html.table.tr[$i].childnodes[5].'#text') + " m/s" $html.table.tr[$i].childnodes[5].InnerText = ($html.table.tr[$i].childnodes[5].'#text') + " m/s"
} }
} }
# Snødybde # Snødybde
for ($i=1;$i -le $html.table.tr.count-1;$i++) { #for ($i=1;$i -le $html.table.tr.count-1;$i++) {
if ($html.table.tr[$i].td[6] -ne "-") { # if ($html.table.tr[$i].td[6] -ne "-") {
$html.table.tr[$i].childnodes[6].'#text' = ($html.table.tr[$i].childnodes[6].'#text') + " meter" # $html.table.tr[$i].childnodes[6].'#text' = ($html.table.tr[$i].childnodes[6].'#text') + " meter"
} #}
} #s}
# Siktrekkevidde # Siktrekkevidde
for ($i=1;$i -le $html.table.tr.count-1;$i++) { #for ($i=1;$i -le $html.table.tr.count-1;$i++) {
if ($html.table.tr[$i].td[8] -ne "-") { # if ($html.table.tr[$i].td[7] -ne "-") {
$html.table.tr[$i].childnodes[8].'#text' = ($html.table.tr[$i].childnodes[8].'#text') + " meter" # $html.table.tr[$i].childnodes[7].'#text' = ($html.table.tr[$i].childnodes[7].'#text') + " meter"
} # }
} #}
#:Send-MailMessage -From 'User01 <steinar@engelund.nu>' -To 'User02 <zoyzat@gmail.com>' -Subject 'Test mail' -SmtpServer 'smtp.domeneshop.no'
#$fragments += $html.InnerXml #$fragments += $html.InnerXml
$html.InnerXml |Out-File C:\temp\mestatable.inc $html.InnerXml | Out-File /home/httpd/sites/datex/mestatable.inc
$fragments += @' $fragments += @'
<div id="Container"> <div id="Container">
<? include 'mestatable.inc'; ?> <? include 'mestatable.inc'; ?>
@@ -118,33 +156,85 @@ $fragments += @'
<div id="refresh"> <div id="refresh">
<div id="time"> <div id="time">
<p class='footer'>Siste oppdatering: <?php echo date('H:i:s');?></p> <p class='footer'>Siste oppdatering: <?php echo date('H:i:s');?></p>
<meta http-equiv="refresh" content="5">
</div> </div>
</div> </div>
'@
$fragments +="<h1>Værvarsel fra met.no</h1>
<b>Østlandet 24 timer:</b><p>$ostlandet_24</p>
<b>Østlandet 48 timer:</b><p>$ostlandet_48</p></br>"
$fragments += @'
<div class="w3-content w3-section" style="max-width:500px">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=109104" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=452531" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=2152383" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=440787" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=108543" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=750415" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=2056087" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=452497" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=729525" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=659547" style="width="640" Height="480">
<img class="mySlides" src="http://185.44.99.133/jpg/image.jpg" style="width="640" Height="480">
<img class="mySlides" src="https://www.synnfjellhytteeierforening.no/Images/Webcam/2/webcam2.jpgs" style="width="640" Height="480">
</div>
<div class="w3-content w3-section" style="max-width:500px">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=109104" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=452531" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=2152383" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=440787" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=108543" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=750415" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=2056087" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=452497" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=729525" style="width="640" Height="480">
<img class="mySlides" src="https://www.vegvesen.no/public/webkamera/kamera?id=659547" style="width="640" Height="480">
<img class="mySlides" src="http://185.44.99.133/jpg/image.jpg" style="width="640" Height="480">
<img class="mySlides" src="https://www.synnfjellhytteeierforening.no/Images/Webcam/2/webcam2.jpgs" style="width="640" Height="480">
</div>
<script src="https://cdn.jsdelivr.net/npm/hls.js@canary"></script>
<video id="video" controls autoplay class="videoCentered"></video>
<script> <script>
var video = document.getElementById('video'); var myIndex = 0;
if(Hls.isSupported()) { carousel();
var hls = new Hls();
hls.loadSource('https://kamera.vegvesen.no/public/0529008_1/manifest.m3u8'); function carousel() {
hls.attachMedia(video); var i;
hls.on(Hls.Events.MANIFEST_PARSED,function() { var x = document.getElementsByClassName("mySlides");
video.play(); for (i = 0; i < x.length; i++) {
}); x[i].style.display = "none";
}
myIndex++;
if (myIndex > x.length) {myIndex = 1}
x[myIndex-1].style.display = "block";
setTimeout(carousel, 5000); // Change image every 2 seconds
} }
</script> </script>
</br>
<script src="https://www.yr.no/sted/Norge/Oppland/Gran/Lygna/ekstern_boks_time_for_time.js"></script><noscript><a href="https://www.yr.no/sted/Norge/Oppland/Gran/Lygna/">yr.no: Værvarsel for Lygna</a></noscript>
<script src="https://www.yr.no/sted/Norge/Oppland/Lunner/Stryken/ekstern_boks_time_for_time.js"></script><noscript><a href="https://www.yr.no/sted/Norge/Oppland/Lunner/Stryken/">yr.no: Værvarsel for Stryken</a></noscript>
<script src="https://www.yr.no/sted/Norge/Oppland/Nordre_Land/Dokka/ekstern_boks_time_for_time.js"></script><noscript><a href="https://www.yr.no/sted/Norge/Oppland/Nordre_Land/Dokka/">yr.no: Værvarsel for Dokka</a></noscript>
'@ '@
$convertParams = @{ $convertParams = @{
head = @" head = @"
<Title>Mesta Værstatistikk</Title> <Title>Mesta Værstatistikk - datex.glaeserud.no</Title>
<style> <style>
body { background-color:#E5E4E2; body { background-color:#E5E4E2;
font-family:Monospace; font-family:Monospace;
font-size:10pt; } font-size:10pt; }
td, th { border:0px solid black; td,
th { border:0px solid black;
border-collapse:collapse; border-collapse:collapse;
white-space:pre; white-space:pre;
text-align:center; } text-align:center; }
@@ -177,20 +267,5 @@ h2 {
body = $fragments body = $fragments
} }
convertto-html @convertParams | out-file c:\temp\mesta.php convertto-html @convertParams | out-file /home/httpd/sites/datex/index7.php
Write-host -fore Yellow "## Starting FTP upload.."
$File = "C:\temp\mesta.php";
$ftp = "ftp://xxxxxxx:yyyyyyy@web.home.zyrex.org//home/httpd/sites/zyrex.org/temp/mesta.php";
$File2 = "C:\temp\mestatable.inc";
$ftp2 = "ftp://xxxxxxxx:yyyyyyy@web.home.zyrex.org//home/httpd/sites/zyrex.org/temp/mestatable.inc";
$webclient = New-Object -TypeName System.Net.WebClient;
$uri = New-Object -TypeName System.Uri -ArgumentList $ftp;
$uri2 = New-Object -TypeName System.Uri -ArgumentList $ftp2;
Write-Host -fore Yellow "## Uploading $File...";
$webclient.UploadFile($uri, $File);
Write-Host -fore Yellow "## Uploading $File2...";
$webclient.UploadFile($uri2, $File2);