Created
October 31, 2023 02:41
-
-
Save dkam/12746460d29a75ed9adb8c91737a946e to your computer and use it in GitHub Desktop.
Lookup albums on song.link for sharing
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
#!/usr/bin/env ruby | |
require 'cgi' | |
require 'net/http' | |
require 'json' | |
abort("songlink <url>") if ARGV[0].nil? | |
def copy_to_clipboard(text) = IO.popen('pbcopy', 'w') { |clipboard| clipboard.puts text } | |
def open_local(url) = url.include?('music.apple') ? system("open -b 'com.apple.Music' '#{url}'") : system("open '#{url}'") | |
link = CGI.escape(ARGV[0]) | |
uri = URI("https://api.song.link/v1-alpha.1/links?url=#{link}&userCountry=AU") | |
res = Net::HTTP.get(uri) | |
doc = JSON.parse(res) | |
if link.include?("music.apple") | |
page_url = doc['pageUrl'] | |
elsif link.include?("album.link") || link.include?("song.link") | |
page_url = doc.dig("linksByPlatform", "appleMusic", "url") | |
end | |
open_local(page_url) | |
copy_to_clipboard page_url | |
puts page_url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment