Created
January 8, 2020 17:01
-
-
Save jahands/0fd2b52aa9b111a82368e75055e6e8b3 to your computer and use it in GitHub Desktop.
Jsonbin.io example usage - this is how I sync minecraft between computers without overwriting newer data with older data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class JsonBin { | |
[string]$BinID | |
[string]$SecretKey = '$xyz' | |
[object]$Data | |
JsonBin($binID){ | |
$this.BinID = $binID | |
$this.Get() | |
} | |
Get(){ | |
$res = Invoke-RestMethod -Method Get -Uri "https://api.jsonbin.io/b/$($this.BinID)/latest" -Headers @{'secret-key'=$this.SecretKey} | |
if(-not $res.success){ | |
Write-Error "Response did not contain required 'success' property! Please add it. Here's what we had: $($res | ConvertTo-Json)" -ErrorAction:Stop | |
} | |
$this.Data = $res | |
} | |
Put(){ | |
Invoke-RestMethod -Method Put -Uri "https://api.jsonbin.io/b/$($this.BinID)" -Headers @{'Content-Type'='application/json';'secret-key'=$this.SecretKey} -Body $($this.Data | ConvertTo-Json) | |
} | |
} | |
Function Sync-MultiMC { | |
[CmdletBinding()] | |
Param( | |
[Parameter(ParameterSetName = 'Upload', Mandatory = $true)] | |
[switch]$Up, | |
[Parameter(ParameterSetName = 'Download', Mandatory = $true)] | |
[switch]$Down | |
) | |
Set-AllowedHosts DTGAME, DTRANCH, ENVY10 | |
$path = @{ | |
'DT1' = 'C:\Bin\MultiMC'; | |
'DT2' = 'C:\Users\jh\Documents\bin\MultiMC'; | |
'LT1' = 'C:\Bin\MultiMC' | |
}[$env:COMPUTERNAME] | |
$b = [JsonBin]::new('abc') | |
Write-Output $b.Data | |
if ($Up) { | |
if(-not ($b.Data.last_device -eq $env:COMPUTERNAME)){ | |
Write-Error "You can't upload from this device! First upload from $($b.Data.last_device)" -ErrorAction:Stop | |
} | |
Write-Host 'Syncing client data up...' | |
rclone sync $path b2p:multimc1/bin/MultiMC -v --transfers=8 --fast-list -P | |
$b.Data.last_device = $env:COMPUTERNAME | |
$b.Data.last_action = 'upload' | |
$b.Put() | |
} elseif ($Down) { | |
if($b.Data.last_action -eq 'download'){ | |
Write-Error "You already downloaded to $($b.Data.last_device), please upload from that PC first!" -ErrorAction:Stop | |
} | |
Write-Host 'Syncing client data down...' | |
rclone sync b2p:syncit/bin/MultiMC $path -v --transfers=8 --fast-list | |
$b.Data.last_device = $env:COMPUTERNAME | |
$b.Data.last_action = 'download' | |
$b.Put() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment