commit 75ee4ef88b8757a6881b260f129a132f0d5ea8cf
parent b2b0c8c5dd7f1485e620d0cd2e529e1e5c62ba03
Author: Alex Balgavy <alex@balgavy.eu>
Date: Tue, 23 Jun 2026 21:42:32 +0200
libspotify: update
Diffstat:
1 file changed, 24 insertions(+), 9 deletions(-)
diff --git a/scripts/libspotify.rb b/scripts/libspotify.rb
@@ -23,7 +23,7 @@ require "base64"
# Client to access Spotify
class SpotifyClient
CLIENT_ID = "c747e580651248da8e1035c88b3d2065"
- REDIRECT_URI = "http://localhost:4815/callback"
+ REDIRECT_URI = "http://127.0.0.1:4815/callback"
# OAUTH functions
def self.generate_random_string(length)
@@ -136,11 +136,11 @@ class SpotifyClient
end
def get_refresh_token
- `security find-generic-password -a spotify -s spotify_refresh_token -w`
+ `security find-generic-password -a spotify -s spotify_refresh_token -w`.strip
end
def store_refresh_token(token)
- system("security add-generic-password -a spotify -s spotify_refresh_token -w \"#{token}\"")
+ system("security add-generic-password -U -a spotify -s spotify_refresh_token -w \"#{token}\"")
end
def auth
@@ -153,14 +153,18 @@ class SpotifyClient
end
end
- def initialize
- auth
- @base_url = URI("https://api.spotify.com/v1/")
+ def initialize_http
@http = Net::HTTP.new(@base_url.host, @base_url.port)
@http.use_ssl = true
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
+ def initialize
+ auth
+ @base_url = URI("https://api.spotify.com/v1/")
+ initialize_http
+ end
+
def api_call_get(endpoint, params = {})
url = @base_url + endpoint
url.query = URI.encode_www_form(params)
@@ -183,9 +187,12 @@ class SpotifyClient
request["Authorization"] = "Bearer #{@token}"
begin
resp = @http.request(request)
- rescue
- puts("Connection broke, retrying request to #{url}")
+ rescue Exception => e
+ puts("Connection broke (#{e}), retrying request to #{url}")
+ binding.irb if File.exist?("/tmp/rubydebug")
+
sleep(2)
+ initialize_http
return url_call_get(url)
end
@@ -213,7 +220,15 @@ class SpotifyClient
request["Authorization"] = "Bearer #{@token}"
request.body = JSON.dump(body)
request.content_type = "application/json"
- JSON.parse(@http.request(request).read_body)
+ begin
+ JSON.parse(@http.request(request).read_body)
+ rescue Exception => e
+ puts("Connection broke (#{e}), retrying request to #{url}")
+ binding.irb if File.exist?("/tmp/rubydebug")
+ sleep(2)
+ initialize_http
+ return url_call_post(url, body)
+ end
end
def url_call_put(url, body)