Skip to content

Instantly share code, notes, and snippets.

@elvith-de
Last active June 24, 2024 15:40
Show Gist options
  • Save elvith-de/89107061661e001df659d7a7d413092b to your computer and use it in GitHub Desktop.
Save elvith-de/89107061661e001df659d7a7d413092b to your computer and use it in GitHub Desktop.
A small script for Powershell to export Lemmy profile settings. useful e.g. if the instance API is still working, but the frontend has problems (e.g. feddit.de problems)
# CHANGE THESE VALUES
$my_instance="https://feddit.de" # e.g. https://feddit.nl
$target_file = "C:\Temp\export.json"
########################################################
#Ask user for username and password
$credentials = Get-Credential -Message "Logindata for $my_instance" -Title "Login"
$twoFACode = Read-Host -Prompt "2FA Code (leave empty if 2FA is not active)"
$my_username= $credentials.UserName
$my_password= $credentials.GetNetworkCredential().Password
# Lemmy API version
$API="api/v3"
# Login
function Get-AuthToken() {
$end_point="user/login"
$json_data= @{
"username_or_email" = $my_username;
"password" = $my_password;
"totp_2fa_token" = $twoFACode
} | ConvertTo-Json
$url="$my_instance/$API/$end_point"
(Invoke-RestMethod -Headers @{"Content-Type" = "application/json"} -Body $json_data -Method Post -Uri $url).JWT
}
# Get userdata as JSON
function Get-UserData() {
$end_point="user/export_settings"
$url="$my_instance/$API/$end_point"
Invoke-RestMethod -Headers @{"Authorization"="Bearer $($JWT)"} -Method Get -Uri $url
}
$JWT= Get-AuthToken
Write-Host "Got JWT Token: $JWT"
Write-Host "Exporting data to $target_file"
Get-UserData | ConvertTo-Json | Out-File -FilePath $target_file
@elvith-de
Copy link
Author

Now with support for 2FA enabled logins

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment