-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpx.ps1
executable file
·72 lines (56 loc) · 2 KB
/
gpx.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<#
.SYNOPSIS
Publish GPX from strava to OSM
.PARAMETER Pick
Optionally, choose from recent activities
.PARAMETER Pick
Optionally, paste in a specific strava activity ID
#>
param(
[switch]$Pick = $false,
[string]$Id = $null
)
$script:ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
Add-Type -TypeDefinition (Get-Content (Join-Path $PSScriptRoot "GpxUpload.cs") -Raw)
if ($Pick -and -not $Id) {
$u, $p = [GpxUpload]::OsmAuth() -split -split ":"
$secpasswd = ConvertTo-SecureString $p -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($u, $secpasswd)
$traces = Invoke-RestMethod "https://api.openstreetmap.org/api/0.6/user/gpx_files" -Authentication Basic -Credential $credential
$regex = [regex]"\d{4}-\d{2}-\d{2}"
$dates = @{}
$traces.osm.gpx_file.description | % { $v = $regex.Matches($_); if ($v) { $dates[$v.Value] = 1 } }
foreach ($line in strava activities) {
$v = $regex.Matches($line)
if (!$v) { $line; continue }
if (!$dates[$v.Value]) { $line }
}
$id = Read-Host "Paste Activity ID"
} elseif ($Id) {
$id = $Id
} else {
$id = strava activities --index 0 --quiet --per_page 1
}
$started = [DateTime]::Now
"Opening browser to download Strava activity $id ..."
Start-Process "https://www.strava.com/activities/$id/export_gpx"
# Put this delay after starting download
$details = strava activity $id -o json | ConvertFrom-Json
$timeout = New-TimeSpan -Seconds 20
$startTime = Get-Date
while (!($gpxFile = Get-ChildItem ~/Downloads/*.gpx | `
Where-Object LastWriteTime -gt $started | `
Select-Object -First 1)) {
if ((Get-Date) - $startTime -ge $timeout) {
Write-Error "Timeout waiting for GPX file to download"
return
}
Start-Sleep 0.1
}
"Using OSM API to upload $gpxFile..."
# Call a static method
$task = [GpxUpload]::Run($gpxFile.FullName, $details.start_date_local)
while (-not $task.AsyncWaitHandle.WaitOne(50)) { }
$gpxID = $task.GetAwaiter().GetResult()
Start-Process "https://www.openstreetmap.org/edit?gpx=$gpxID"