Created
June 28, 2021 06:50
-
-
Save orlea/ec1afd8fcd9b3252cbac7e4cd0d83712 to your computer and use it in GitHub Desktop.
last.fmのnowplayingをmastodonに投稿するposhスクリプト
This file contains 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
$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