Skip to content

Instantly share code, notes, and snippets.

@orlea
Created June 28, 2021 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orlea/ec1afd8fcd9b3252cbac7e4cd0d83712 to your computer and use it in GitHub Desktop.
Save orlea/ec1afd8fcd9b3252cbac7e4cd0d83712 to your computer and use it in GitHub Desktop.
last.fmのnowplayingをmastodonに投稿するposhスクリプト
$instance = "https://mstdn.example.com/api/v1/statuses"
$mastodon_api_key = "MASTODON_API_KEY"
$headers = @{"Authorization" = "Bearer " + $mastodon_api_key}
$last_fm_key = "LAST_FM_API_KEY"
$last_fm_user = "LAST_FM_USER_NAME"
$getrecenttracksurl = "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=" + $last_fm_user + "&api_key=" + $last_fm_key + "&format=json"
$raw = Invoke-WebRequest $getrecenttracksurl -Method GET
$recenttracks_json = [System.Text.Encoding]::UTF8.GetString( [System.Text.Encoding]::GetEncoding("ISO-8859-1").GetBytes($raw.Content) )
$data = ($recenttracks_json | ConvertFrom-Json)
$tracks = $data.recenttracks.track
if ($tracks[0].'@attr'.nowplaying -eq "true") {
$toot = $tracks[0].name + " / " + $tracks[0].artist.'#text' + " / " + $tracks[0].album.'#text' + " #nowplaying"
$text = @{status=$toot} | ConvertTo-Json -Compress
$body = [Text.Encoding]::UTF8.GetBytes($text)
Invoke-WebRequest $instance -Method POST -Body $body -Headers $headers -ContentType application/json
}else {
Write-Output "現在再生中の楽曲はありません"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment