Guest User

Starline Track Downloader p.2

a guest
Aug 4th, 2020
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $outerpath = "S:\Tracks\"
  2. $email = 'example@contoso.com' #example@contoso.com - No more encoding
  3. $pass = '12345%zzz' #12345%zzz - No more encoding
  4.  
  5. $uptodate = [datetime]::today #EndDate
  6. $godate = Get-Date "2020-01-17" #StartDate
  7.  
  8. #$uptodate = Get-Date "2016-09-05"
  9.  
  10. $startdate = Get-Date "1970-01-01"
  11.  
  12.  
  13. $js = @{password=$pass;username=$email} | ConvertTo-Json -Compress
  14. $URL = "https://starline-online.ru/rest/security/login"
  15. $m1 = New-Object Microsoft.PowerShell.Commands.WebRequestSession
  16.  
  17. $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'
  18.  
  19.  
  20. if (($login.Content | ConvertFrom-Json).errors.title -eq "index.error.wrongUserNameOrPassword")
  21. {
  22.     Write-Host "incorrect login"
  23. }
  24. else
  25. {
  26.     $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"
  27.     $dev = $devices.content | ConvertFrom-Json
  28.     $devid = $dev.answer.devices[0].device_id
  29.  
  30.     if ($godate.year -ne 2010 -and $godate.month -ne 1) {Write-Host "Current year: $($godate.Year)`nCurrent month: $($godate.month)`nDays: " -NoNewline}
  31.  
  32.     $beginsecond = ($godate - $startdate).totalseconds
  33.     for ($i = ($godate - $startdate).totalseconds; $i -lt ($uptodate - $startdate).totalseconds; $i += 86400)
  34.     {
  35.         $nowday = $startdate.AddSeconds($i)
  36.         if ($nowday.Month -eq 1 -and $nowday.day -eq 1) {Write-Host;Write-Host "Current year: $($nowday.Year)" -NoNewline}
  37.         if ($nowday.Day -eq 1) {Write-Host;Write-Host "Current month: $($nowday.month)`nDays: " -NoNewline}
  38.         Write-Host "$($nowday.Day)" -NoNewline
  39.         do
  40.         {
  41.             $Failed = $false
  42.             try
  43.             {
  44.                 sleep 3
  45.                 $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"
  46.                 $jsonparsed = $getcurrentdaytrack.content | convertfrom-json
  47.                 if ($jsonparsed.meta.mileage -gt 0)
  48.                 {
  49.                     $curday = $outerpath + $nowday.ToString("yyyy_MM_dd")
  50.                     if (!(Test-Path $curday)) {New-Item -Path $curday -ItemType Directory | Out-Null}
  51.                     $allnodes = ($jsonparsed.data | ? {$_.type -eq "track"}).nodes
  52.                     [xml]$doc = New-Object System.Xml.XmlDocument
  53.                     $decl = $doc.CreateXmlDeclaration("1.0","UTF-8",$null)
  54.                     $doc.AppendChild($decl) > $null
  55.                     $gpxnode = $doc.CreateNode("element","gpx",$null)
  56.                     $doc.appendchild($gpxnode) > $null
  57.                     $gpxattr = $doc.CreateAttribute("version");$gpxattr.value = "1.0";$gpxnode.Attributes.Append($gpxattr) > $null
  58.                     $gpxattr = $doc.CreateAttribute("creator");$gpxattr.value = "LeVoN-CCCP";$gpxnode.Attributes.Append($gpxattr) > $null
  59.                     $gpxattr = $doc.CreateAttribute("xmlns:xsi");$gpxattr.value = "http://www.w3.org/2001/XMLSchema-instance";$gpxnode.Attributes.Append($gpxattr) > $null
  60.                     $gpxattr = $doc.CreateAttribute("xmlns");$gpxattr.value = "http://www.topografix.com/GPX/1/0";$gpxnode.Attributes.Append($gpxattr) > $null
  61.                     $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
  62.                     $metadatanode = $doc.CreateNode("element","metadata",$null)
  63.                     $gpxnode.AppendChild($metadatanode) > $null
  64.                     $metatime = $doc.CreateNode("element","time",$null)
  65.                     $metadatanode.appendchild($metatime) > $null
  66.                     $trk = $doc.CreateNode("element","trk",$null)
  67.                     $gpxnode.AppendChild($trk) > $null
  68.                     $trkname = $doc.CreateNode("element","name",$null)
  69.                     $trkname.InnerText = "Track 000"
  70.                     $trk.AppendChild($trkname) > $null
  71.                    
  72.                     $doctempl = $doc.clone()
  73.                    
  74.                     $trkseg = $doc.CreateNode("element","trkseg",$null)
  75.                     $trk.AppendChild($trkseg) > $null
  76.                    
  77.                     $trkpttempl = $doc.CreateNode("element","trkpt",$null)
  78.                     $lat = $doc.CreateAttribute("lat");$trkpttempl.Attributes.Append($lat) > $null
  79.                     $lon = $doc.CreateAttribute("lon");$trkpttempl.Attributes.Append($lon) > $null
  80.                     $ele = $doc.CreateNode("element","ele",$null)
  81.                     $trkpttempl.appendchild($ele) > $null
  82.                     $time = $doc.CreateNode("element","time",$null)
  83.                     $trkpttempl.appendchild($time) > $null
  84.                     $sat = $doc.CreateNode("element","sat",$null)
  85.                     $trkpttempl.appendchild($sat) > $null
  86.                    
  87.                     $metatime.innertext = $startdate.AddSeconds($allnodes[0].t).tostring("yyyy-MM-ddTHH:mm:ssZ")
  88.                    
  89.                     foreach ($node in $allnodes)
  90.                     {
  91.                        
  92.                         $trkpt = $trkpttempl.clone()
  93.                         $trkpt.lat = $node.x.tostring().replace(",",".")
  94.                         $trkpt.lon = $node.y.tostring().replace(",",".")
  95.                         $trkpt.ele = $node.z.tostring().replace(",",".")
  96.                         $trkpt.time = $startdate.AddSeconds($node.t).tostring("yyyy-MM-ddTHH:mm:ssZ")
  97.                         $trkpt.sat = $node.sat_qty.tostring()
  98.                         $trkseg.AppendChild($trkpt) > $null
  99.                     }
  100.                    
  101.                     $begin = 0
  102.                     $count = 0
  103.                     for ($j = 0; $j -lt $doc.gpx.trk.trkseg.ChildNodes.count - 1; $j++)
  104.                     {
  105.                         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)))
  106.                         {
  107.                             $curtrack = $doctempl.clone()
  108.                             $curtrkseg = $curtrack.CreateNode("element","trkseg",$null)
  109.                             $curtrack.gpx.trk.AppendChild($curtrkseg) > $null
  110.                             $curtrack.gpx.metadata.time = $doc.gpx.trk.trkseg.ChildNodes[$begin].time
  111.                             $curtrack.gpx.trk.name = "Track " + ("{0:D3}" -f $count)
  112.                             for ($q = $begin; $q -le $j; $q++) {$curtrkseg.appendchild($curtrkseg.OwnerDocument.ImportNode($doc.gpx.trk.trkseg.ChildNodes[$q],$true)) > $null}
  113.                             $begin = $j+2
  114.                             $count += 1
  115.                             $StringWriter = New-Object System.IO.StringWriter
  116.                             $XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter
  117.                             $xmlWriter.Formatting = "indented"
  118.                             $xmlWriter.Indentation = 2
  119.                             $curtrack.WriteContentTo($XmlWriter)
  120.                             $XmlWriter.Flush()
  121.                             $StringWriter.Flush()
  122.                             set-content -value $StringWriter.ToString() -path "$($curday)\$(([datetime]$curtrack.gpx.metadata.time).tostring('yyyy-MM-dd HH_mm')).txt"
  123.                         }
  124.                     }
  125.                 }
  126.             }
  127.             catch {$Failed = $true;Write-Host -NoNewline " - restart"}
  128.         } while ($Failed)
  129.         Write-Host ";" -NoNewline
  130.     }
  131. }
  132.  
  133.  
Add Comment
Please, Sign In to add comment