Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $outerpath = "S:\Tracks\"
- $email = 'example@contoso.com' #example@contoso.com - No more encoding
- $pass = '12345%zzz' #12345%zzz - No more encoding
- $uptodate = [datetime]::today #EndDate
- $godate = Get-Date "2020-01-17" #StartDate
- #$uptodate = Get-Date "2016-09-05"
- $startdate = Get-Date "1970-01-01"
- $js = @{password=$pass;username=$email} | ConvertTo-Json -Compress
- $URL = "https://starline-online.ru/rest/security/login"
- $m1 = New-Object Microsoft.PowerShell.Commands.WebRequestSession
- $login = Invoke-WebRequest -Uri $URL -WebSession $m1 -Method POST -Body $js -UseBasicParsing -UserAgent "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:100500.0) Gecko/20100101 Firefox/100500.0" -ContentType 'application/json'
- if (($login.Content | ConvertFrom-Json).errors.title -eq "index.error.wrongUserNameOrPassword")
- {
- Write-Host "incorrect login"
- }
- else
- {
- $devices = Invoke-WebRequest 'https://starline-online.ru/device?tz=180' -Method GET -WebSession $m1 -UseBasicParsing -UserAgent "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:100500.0) Gecko/20100101 Firefox/100500.0"
- $dev = $devices.content | ConvertFrom-Json
- $devid = $dev.answer.devices[0].device_id
- if ($godate.year -ne 2010 -and $godate.month -ne 1) {Write-Host "Current year: $($godate.Year)`nCurrent month: $($godate.month)`nDays: " -NoNewline}
- $beginsecond = ($godate - $startdate).totalseconds
- for ($i = ($godate - $startdate).totalseconds; $i -lt ($uptodate - $startdate).totalseconds; $i += 86400)
- {
- $nowday = $startdate.AddSeconds($i)
- if ($nowday.Month -eq 1 -and $nowday.day -eq 1) {Write-Host;Write-Host "Current year: $($nowday.Year)" -NoNewline}
- if ($nowday.Day -eq 1) {Write-Host;Write-Host "Current month: $($nowday.month)`nDays: " -NoNewline}
- Write-Host "$($nowday.Day)" -NoNewline
- do
- {
- $Failed = $false
- try
- {
- sleep 3
- $getcurrentdaytrack = Invoke-WebRequest "https://starline-online.ru/device/$devid/route?beginTime=$($i)&endTime=$($i+86400)" -Method GET -WebSession $m1 -UseBasicParsing -UserAgent "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:100500.0) Gecko/20100101 Firefox/100500.0"
- $jsonparsed = $getcurrentdaytrack.content | convertfrom-json
- if ($jsonparsed.meta.mileage -gt 0)
- {
- $curday = $outerpath + $nowday.ToString("yyyy_MM_dd")
- if (!(Test-Path $curday)) {New-Item -Path $curday -ItemType Directory | Out-Null}
- $allnodes = ($jsonparsed.data | ? {$_.type -eq "track"}).nodes
- [xml]$doc = New-Object System.Xml.XmlDocument
- $decl = $doc.CreateXmlDeclaration("1.0","UTF-8",$null)
- $doc.AppendChild($decl) > $null
- $gpxnode = $doc.CreateNode("element","gpx",$null)
- $doc.appendchild($gpxnode) > $null
- $gpxattr = $doc.CreateAttribute("version");$gpxattr.value = "1.0";$gpxnode.Attributes.Append($gpxattr) > $null
- $gpxattr = $doc.CreateAttribute("creator");$gpxattr.value = "LeVoN-CCCP";$gpxnode.Attributes.Append($gpxattr) > $null
- $gpxattr = $doc.CreateAttribute("xmlns:xsi");$gpxattr.value = "http://www.w3.org/2001/XMLSchema-instance";$gpxnode.Attributes.Append($gpxattr) > $null
- $gpxattr = $doc.CreateAttribute("xmlns");$gpxattr.value = "http://www.topografix.com/GPX/1/0";$gpxnode.Attributes.Append($gpxattr) > $null
- $gpxattr = $doc.CreateAttribute("xsi:schemaLocation");$gpxattr.value = "http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd";$gpxnode.Attributes.Append($gpxattr) > $null
- $metadatanode = $doc.CreateNode("element","metadata",$null)
- $gpxnode.AppendChild($metadatanode) > $null
- $metatime = $doc.CreateNode("element","time",$null)
- $metadatanode.appendchild($metatime) > $null
- $trk = $doc.CreateNode("element","trk",$null)
- $gpxnode.AppendChild($trk) > $null
- $trkname = $doc.CreateNode("element","name",$null)
- $trkname.InnerText = "Track 000"
- $trk.AppendChild($trkname) > $null
- $doctempl = $doc.clone()
- $trkseg = $doc.CreateNode("element","trkseg",$null)
- $trk.AppendChild($trkseg) > $null
- $trkpttempl = $doc.CreateNode("element","trkpt",$null)
- $lat = $doc.CreateAttribute("lat");$trkpttempl.Attributes.Append($lat) > $null
- $lon = $doc.CreateAttribute("lon");$trkpttempl.Attributes.Append($lon) > $null
- $ele = $doc.CreateNode("element","ele",$null)
- $trkpttempl.appendchild($ele) > $null
- $time = $doc.CreateNode("element","time",$null)
- $trkpttempl.appendchild($time) > $null
- $sat = $doc.CreateNode("element","sat",$null)
- $trkpttempl.appendchild($sat) > $null
- $metatime.innertext = $startdate.AddSeconds($allnodes[0].t).tostring("yyyy-MM-ddTHH:mm:ssZ")
- foreach ($node in $allnodes)
- {
- $trkpt = $trkpttempl.clone()
- $trkpt.lat = $node.x.tostring().replace(",",".")
- $trkpt.lon = $node.y.tostring().replace(",",".")
- $trkpt.ele = $node.z.tostring().replace(",",".")
- $trkpt.time = $startdate.AddSeconds($node.t).tostring("yyyy-MM-ddTHH:mm:ssZ")
- $trkpt.sat = $node.sat_qty.tostring()
- $trkseg.AppendChild($trkpt) > $null
- }
- $begin = 0
- $count = 0
- for ($j = 0; $j -lt $doc.gpx.trk.trkseg.ChildNodes.count - 1; $j++)
- {
- if ((([datetime]$doc.gpx.trk.trkseg.ChildNodes[$j+1].time-[datetime]$doc.gpx.trk.trkseg.ChildNodes[$j].time).totalminutes -gt 3) -or ($j -eq ($doc.gpx.trk.trkseg.ChildNodes.count - 2)))
- {
- $curtrack = $doctempl.clone()
- $curtrkseg = $curtrack.CreateNode("element","trkseg",$null)
- $curtrack.gpx.trk.AppendChild($curtrkseg) > $null
- $curtrack.gpx.metadata.time = $doc.gpx.trk.trkseg.ChildNodes[$begin].time
- $curtrack.gpx.trk.name = "Track " + ("{0:D3}" -f $count)
- for ($q = $begin; $q -le $j; $q++) {$curtrkseg.appendchild($curtrkseg.OwnerDocument.ImportNode($doc.gpx.trk.trkseg.ChildNodes[$q],$true)) > $null}
- $begin = $j+2
- $count += 1
- $StringWriter = New-Object System.IO.StringWriter
- $XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter
- $xmlWriter.Formatting = "indented"
- $xmlWriter.Indentation = 2
- $curtrack.WriteContentTo($XmlWriter)
- $XmlWriter.Flush()
- $StringWriter.Flush()
- set-content -value $StringWriter.ToString() -path "$($curday)\$(([datetime]$curtrack.gpx.metadata.time).tostring('yyyy-MM-dd HH_mm')).txt"
- }
- }
- }
- }
- catch {$Failed = $true;Write-Host -NoNewline " - restart"}
- } while ($Failed)
- Write-Host ";" -NoNewline
- }
- }
Add Comment
Please, Sign In to add comment