Guest User

Starline Track Downloader

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