Can this be used in a Ruby on Rails web application?

53 views
Skip to first unread message

Will Carter

unread,
May 27, 2014, 6:16:36 PM5/27/14
to ruby-...@googlegroups.com
Hi,

I am new to Ruby and was wondering if this could be used in a web application. I was able to get a couple of the examples to work at the command line and tried see how far I could get to make a web based playlist utility where users could upload a playlist text file that would be parsed and created for the user on Spotify...

I have the following in my Gemfile
gem "spotify", "~> 12.5.3"

and in a controller...
username = "username"
user_link = "spotify:user:#{username}"
link = Spotify.link_create_from_string(user_link)

Here I get a segmentation fault. username is a valid Spotify premium account. I think it may have to do with the spotify_appkey.key file. Where should I put that in a RoR web app?

I am pretty much a beginner with Ruby and would like to know if what I am trying to do is possible or any advice would be appreciated.

thanks,
Will



Will Carter

unread,
May 28, 2014, 1:02:27 AM5/28/14
to ruby-...@googlegroups.com
I'm a bit further. I almost got the logger output from example-listing_playlists.rb working in a controller method, but getting to an error here when it's trying to come up with the link_string for the playlist:

#Retrieving link for playlist:
playlist_link = Spotify.link_create_from_playlist(playlist)
link_string = if playlist_link.nil?
                "(no link)"
              else
                link_length = Spotify.link_as_string(playlist_link, nil, 0)
                FFI::Buffer.alloc_out(link_length + 1) do |link_buffer|
                  Spotify.link_as_string(playlist_link, link_buffer, link_buffer.size)
                  break link_buffer.get_string(0).force_encoding("UTF-8")
                end
              end

When I run the command line example:
$ruby example-listing_playlists.rb
It works, and I see the playlist link in the output.

Any help or advice would be greatly appreciated. I am a rails newbie and groping in the dark at this point.

thanks,
Will

Kim Burgestrand

unread,
May 28, 2014, 1:52:55 AM5/28/14
to Hallon on behalf of Will Carter
I am new to Ruby and was wondering if this could be used in a web application. I was able to get a couple of the examples to work at the command line and tried see how far I could get to make a web based playlist utility where users could upload a playlist text file that would be parsed and created for the user on Spotify...
[…]
I am pretty much a beginner with Ruby and would like to know if what I am trying to do is possible or any advice would be appreciated.

Yes, it should be possible! The very first time I created the bindings to Ruby was for a reason very similar to what you are doing here.

Regarding the segmentation fault in your first e-mail:

Did you have a session created? I just want to make sure the segmentation fault is internal to libspotify, and not an oversight in the spotify gem.

Regarding the error in your second e-mail:

What error do you receive? You have to keep in mind that there are a gazillion of possible errors, so the first step of debugging this is looking at all of the output you receive when you receive the error: the name of the error, the description, and the entire call trace.

— Kim Burgestrand


--
You received this message because you are subscribed to the Hallon ruby gem mailing list.

- To view this group online, visit https://groups.google.com/d/forum/ruby-hallon
- To post to this group, send email to ruby-...@googlegroups.com
- To unsubscribe from this group, send email to ruby-hallon...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "Hallon" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-hallon...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Will Carter

unread,
May 28, 2014, 1:50:25 PM5/28/14
to ruby-...@googlegroups.com
Thank you for the reply! Here is a more detailed post of where I am stuck.

For the first issue with my spotify_appkey.key, I found the reference to it's location in the Support module code...

support.rb
DEFAULT_CONFIG = Spotify::SessionConfig.new({
      api_version: Spotify::API_VERSION.to_i,
      application_key: File.binread("./spotify_appkey.key"),
      cache_location: ".spotify/",
      settings_location: ".spotify/",
      user_agent: "spotify for ruby",
      callbacks: Spotify::SessionCallbacks.new
})

I put my key file in the application root (next to the Gemfile) and I can see now that I get a session by the logger outputs. For security reasons, this probably isn't the best place for the key file, but just trying to get a proof of concept working. I want to be see if I can import a playlist into Spotify via a RoR web app.

For the second issue, It gets to the point where it finds 2 of my public playlists, but it chokes  when trying to build link_string. Spotify.link_as_string(playlist_link, nil, 0) is null (bold below)...

link_string = if playlist_link.nil?
      "(no link)"
else
      link_length = Spotify.link_as_string(playlist_link, nil, 0)
      FFI::Buffer.alloc_out(link_length + 1) do |link_buffer|
            Spotify.link_as_string(playlist_link, link_buffer, link_buffer.size)
            break link_buffer.get_string(0).force_encoding("UTF-8")
      end
end

The error name:
TypeError in WelcomeController#index

Error description:
Spotify::Link pointers cannot be null, was #<Spotify::Link address=0x0>

Here is some info when setting breakpoint above the error line. (playlist and playlist_link variables dumped) The error occurs on line 87.

Full stack trace:
spotify (12.5.3) lib/spotify/managed_pointer.rb:53:in `to_native'
spotify (12.5.3) lib/spotify.rb:101:in `link_as_string'
spotify (12.5.3) lib/spotify.rb:101:in `block in method_missing'
/home/willcarter/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
spotify (12.5.3) lib/spotify.rb:100:in `method_missing'
app/controllers/welcome_controller.rb:85:in `block in index'
app/controllers/welcome_controller.rb:72:in `times'
app/controllers/welcome_controller.rb:72:in `index'
actionpack (4.1.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.1) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.1) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.1) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.1) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.1.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.1) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:676:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.1) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.1) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.1) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.1) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.1) lib/rails/engine.rb:514:in `call'
railties (4.1.1) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/home/willcarter/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/home/willcarter/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/home/willcarter/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

thanks!
Will

On Tuesday, May 27, 2014 11:52:55 PM UTC-6, Kim Burgestrand wrote:
I am new to Ruby and was wondering if this could be used in a web application. I was able to get a couple of the examples to work at the command line and tried see how far I could get to make a web based playlist utility where users could upload a playlist text file that would be parsed and created for the user on Spotify...
[…]
I am pretty much a beginner with Ruby and would like to know if what I am trying to do is possible or any advice would be appreciated.

Yes, it should be possible! The very first time I created the bindings to Ruby was for a reason very similar to what you are doing here.

Regarding the segmentation fault in your first e-mail:

Did you have a session created? I just want to make sure the segmentation fault is internal to libspotify, and not an oversight in the spotify gem.

Regarding the error in your second e-mail:

What error do you receive? You have to keep in mind that there are a gazillion of possible errors, so the first step of debugging this is looking at all of the output you receive when you receive the error: the name of the error, the description, and the entire call trace.

— Kim Burgestrand

Kim Burgestrand

unread,
May 29, 2014, 10:04:51 AM5/29/14
to Hallon on behalf of Will Carter
How do you create playlist_link?
--
— Kim Burgestrand

Will Carter

unread,
May 29, 2014, 10:50:55 PM5/29/14
to ruby-...@googlegroups.com
Here's my whole welcome controller with the method, index. It's basically the same as the example-listing_playlists.rb.
Where playlist_link is created is in bold below.

class WelcomeController < ApplicationController
  def index

    # We use a logger to print some information on when things are happening.
    $stderr.sync = true
    $logger = Logger.new($stderr)
    $logger.level = Logger::INFO
    $logger.formatter = proc do |severity, datetime, progname, msg|
      progname = if progname
                   " (#{progname}) "
                 else
                   " "
                 end
      "\n[#{severity} @ #{datetime.strftime("%H:%M:%S")}]#{progname}#{msg}"
    end

    require_relative "support"

    session = Support.initialize_spotify!

    username = "zippyferguson"
    user_link = "spotify:user:#{username}"
    link = Spotify.link_create_from_string(user_link)

    if link.null?
      $logger.error "#{user_link} was apparently not parseable as a Spotify URI. Aborting."
      abort
    end

    user = Spotify.link_as_user(link)
    $logger.info "Attempting to load #{user_link}. Waiting forever until successful."
    Support.poll(session) { Spotify.user_is_loaded(user) }

    display_name = Spotify.user_display_name(user)
    canonical_name = Spotify.user_canonical_name(user)
    $logger.info "User loaded: #{display_name}."

    $logger.info "Loading user playlists by loading their published container: #{canonical_name}."
    container = Spotify.session_publishedcontainer_for_user_create(session, canonical_name)

    $logger.info "Attempting to load container. Waiting forever until successful."
    Support.poll(session) { Spotify.playlistcontainer_is_loaded(container) }

    $logger.info "Container loaded. Loading playlists until no more are loaded for three tries!"

    container_size = 0
    previous_container_size = 0
    break_counter = 0

    loop do
      container_size = Spotify.playlistcontainer_num_playlists(container)
      new_playlists = container_size - previous_container_size
      previous_container_size = container_size
      $logger.info "Loaded #{new_playlists} more playlists."

      # If we have loaded no new playlists for 4 tries, we assume we are done.
      if new_playlists == 0
        break_counter += 1
        if break_counter >= 4
          break
        end
      end

      $logger.info "Loading…"
      5.times do
        Support.process_events(session)
        sleep 0.2
      end
    end

    $logger.info "#{container_size} published playlists for #{display_name} found. Loading each playlists and printing it."
    container_size.times do |index|
      type = Spotify.playlistcontainer_playlist_type(container, index)
      playlist = Spotify.playlistcontainer_playlist(container, index)
      Support.poll(session) { Spotify.playlist_is_loaded(playlist) }

      playlist_name = Spotify.playlist_name(playlist)

      num_tracks = Spotify.playlist_num_tracks(playlist)

      #Retrieving link for playlist:
      playlist_link = Spotify.link_create_from_playlist(playlist)

      link_string = if playlist_link.nil?
                      "(no link)"
                    else
                      link_length = Spotify.link_as_string(playlist_link, nil, 0)
                      FFI::Buffer.alloc_out(link_length + 1) do |link_buffer|
                        Spotify.link_as_string(playlist_link, link_buffer, link_buffer.size)
                        break link_buffer.get_string(0).force_encoding("UTF-8")
                      end
                    end

      $logger.info "  (#{type}) #{playlist_name}: #{link_string} (#{num_tracks} tracks)"
    end

    $logger.info "All done."



  end
end

here is what is showing in my console:

/home/willcarter/.rvm/rubies/ruby-2.1.1/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /home/willcarter/rubyonrails/websites/spotifyplaylistimport/bin/rails server -b 0.0.0.0 -p 3000 -e development
=> Booting WEBrick
=> Rails 4.1.1 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
[2014-05-29 20:41:43] INFO  WEBrick 1.3.1
[2014-05-29 20:41:43] INFO  ruby 2.1.1 (2014-02-24) [x86_64-linux]
[2014-05-29 20:41:43] INFO  WEBrick::HTTPServer#start: pid=32469 port=3000


Started GET "/" for 127.0.0.1 at 2014-05-29 20:41:59 -0600
Processing by WelcomeController#index as HTML

[INFO @ 20:41:59] Using remembered login for: zippyferguson.
[INFO @ 20:41:59] Log in requested. Waiting forever until logged in..........
[INFO @ 20:42:00] Attempting to load spotify:user:zippyferguson. Waiting forever until successful.
[INFO @ 20:42:00] User loaded: zippyferguson.
[INFO @ 20:42:00] Loading user playlists by loading their published container: zippyferguson.
[INFO @ 20:42:00] Attempting to load container. Waiting forever until successful.
[INFO @ 20:42:00] Container loaded. Loading playlists until no more are loaded for three tries!
[INFO @ 20:42:00] Loaded 2 more playlists.
[INFO @ 20:42:00] Loading…
[INFO @ 20:42:01] Loaded 0 more playlists.
[INFO @ 20:42:01] Loading…
[INFO @ 20:42:02] Loaded 0 more playlists.
[INFO @ 20:42:02] Loading…
[INFO @ 20:42:03] Loaded 0 more playlists.
[INFO @ 20:42:03] Loading…
[INFO @ 20:42:04] Loaded 0 more playlists.
[INFO @ 20:42:04] 2 published playlists for zippyferguson found. Loading each playlists and printing it.Completed 500 Internal Server Error in 4544ms

TypeError (Spotify::Link pointers cannot be null, was #<Spotify::Link address=0x0>):
  app/controllers/welcome_controller.rb:87:in `block in index'
  app/controllers/welcome_controller.rb:72:in `times'
  app/controllers/welcome_controller.rb:72:in `index'


  Rendered /home/willcarter/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.9ms)
  Rendered /home/willcarter/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.6ms)
  Rendered /home/willcarter/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.7ms)
  Rendered /home/willcarter/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (22.4ms)

Kim Burgestrand

unread,
May 30, 2014, 4:51:33 PM5/30/14
to Hallon on behalf of Will Carter
There are some major problems with how you've approached this.

The example code in the repository is just examples. It's not meant to be re-used or copied, only to create an understanding on what working with the API could look like.

A majority of the code in your controller was never meant to run more than once. More critically, you can never create more than one Spotify session in the same process. Attempting to do so will at best cause segfaults. Your code creates a new session every time the index action is hit.

Either way, regarding the specific error you're receiving: it appears playlist_link = Spotify.link_create_from_playlist(playlist) is returning a null pointer. According to the documentation at https://developer.spotify.com/docs/libspotify/12.1.51/group__link.html#ga805392bfa8e526eebedc1cf5a7bcae0f this will happen if the playlist is not loaded. It does however look like you're loading the playlist a few lines earlier, but I don't have any other ideas so I'd make sure the playlist is indeed loaded as a debugging step.

You mentioned getting the example to work. Can you run the example to the end without it raising the same error?

— Kim Burgestrand


Gary Pinkham

unread,
Jun 1, 2014, 8:47:51 AM6/1/14
to ruby-s...@googlegroups.com
I hadn't noticed the mailing list when I first started using the gem a couple of days ago and had mistakenly posted a "question/issue" on the github issues list..    I'm doing a similar thing to Will.     I do have it working as a single test but not in the actual app and your comment that things shouldn't happen more than once is clueing me into that. . I was releasing and then creating the session everytime..      I will fix that up so I only create the session once.. 

thanks!
Gary



On Friday, May 30, 2014 4:51:33 PM UTC-4, Kim Burgestrand wrote:
There are some major problems with how you've approached this.

The example code in the repository is just examples. It's not meant to be re-used or copied, only to create an understanding on what working with the API could look like.

A majority of the code in your controller was never meant to run more than once. More critically, you can never create more than one Spotify session in the same process. Attempting to do so will at best cause segfaults. Your code creates a new session every time the index action is hit.

Either way, regarding the specific error you're receiving: it appears playlist_link = Spotify.link_create_from_playlist(playlist) is returning a null pointer. According to the documentation at https://developer.spotify.com/docs/libspotify/12.1.51/group__link.html#ga805392bfa8e526eebedc1cf5a7bcae0f this will happen if the playlist is not loaded. It does however look like you're loading the playlist a few lines earlier, but I don't have any other ideas so I'd make sure the playlist is indeed loaded as a debugging step.

You mentioned getting the example to work. Can you run the example to the end without it raising the same error?

— Kim Burgestrand

Kim Burgestrand

unread,
Jun 1, 2014, 4:07:12 PM6/1/14
to Spotify for Ruby on behalf of Gary Pinkham
Gary, please try to not mix topics in the same e-mail thread. The added noise could make the discussion harder to follow.

— Kim Burgestrand


--
You received this message because you are subscribed to the Google Groups "Spotify for Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-spotify...@googlegroups.com.
Visit this group at http://groups.google.com/group/ruby-spotify.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-spotify/be3cd0ea-3c67-49e9-ac53-6d0a5b5e99d7%40googlegroups.com.

Will Carter

unread,
Jun 2, 2014, 9:14:47 PM6/2/14
to ruby-s...@googlegroups.com
Thank you for your reply. The code I posted was a just simple first test to see if I could get your Spotify to work in a Ruby on Rails web application, and wasn't meant for anything more than that. I am currently learning RoR development, and a playlist manager web app would be an interesting one to create. Making query to Spotify to get playlist information seemed a harmless one to try, which is why my index method is basically the same as what is in the example:
example-listing_playlists.rb
The example-listing_playlists.rb does run at the command line. Here is my console output:

$ ruby example-listing_playlists.rb

[INFO @ 18:18:39] Using remembered login for: zippyferguson.
[INFO @ 18:18:39] Log in requested. Waiting forever until logged in...........
Please enter a username [burgestrand]: zippyferguson

[INFO @ 18:18:47] Attempting to load spotify:user:zippyferguson. Waiting forever until successful.
[INFO @ 18:18:47] User loaded: zippyferguson.
[INFO @ 18:18:47] Loading user playlists by loading their published container: zippyferguson.
[INFO @ 18:18:47] Attempting to load container. Waiting forever until successful.
[INFO @ 18:18:47] Container loaded. Loading playlists until no more are loaded for three tries!
[INFO @ 18:18:47] Loaded 2 more playlists.
[INFO @ 18:18:47] Loading
[INFO @ 18:18:48] Loaded 0 more playlists.
[INFO @ 18:18:48] Loading
[INFO @ 18:18:49] Loaded 0 more playlists.
[INFO @ 18:18:49] Loading
[INFO @ 18:18:50] Loaded 0 more playlists.
[INFO @ 18:18:50] Loading
[INFO @ 18:18:51] Loaded 0 more playlists.
[INFO @ 18:18:51] 2 published playlists for zippyferguson found. Loading each playlists and printing it.
[INFO @ 18:18:51]   (playlist) : (no link) (10 tracks)
[INFO @ 18:18:51]   (playlist) Workout: spotify:user:zippyferguson:playlist:4dTBRotMCktYTgWhHGg2Cr (10 tracks)
[INFO @ 18:18:51] All done.
[INFO @ 18:18:51] Logging out...

Here is a comparison example-listing_playlists.rb vs. my welcome_controller.rb. Basically the same. Maybe this gives an idea though.

thanks,
Will

On Friday, May 30, 2014 2:51:33 PM UTC-6, Kim Burgestrand wrote:
There are some major problems with how you've approached this.

The example code in the repository is just examples. It's not meant to be re-used or copied, only to create an understanding on what working with the API could look like.

A majority of the code in your controller was never meant to run more than once. More critically, you can never create more than one Spotify session in the same process. Attempting to do so will at best cause segfaults. Your code creates a new session every time the index action is hit.

Either way, regarding the specific error you're receiving: it appears playlist_link = Spotify.link_create_from_playlist(playlist) is returning a null pointer. According to the documentation at https://developer.spotify.com/docs/libspotify/12.1.51/group__link.html#ga805392bfa8e526eebedc1cf5a7bcae0f this will happen if the playlist is not loaded. It does however look like you're loading the playlist a few lines earlier, but I don't have any other ideas so I'd make sure the playlist is indeed loaded as a debugging step.

You mentioned getting the example to work. Can you run the example to the end without it raising the same error?

— Kim Burgestrand

Kim Burgestrand

unread,
Jun 6, 2014, 3:44:58 PM6/6/14
to Spotify for Ruby on behalf of Will Carter
Hi! It's been a while. I've been busy.

I'm not sure why your code is behaving as it is, but the first thing to do would be to make the other parts correct first.

So, a few guidelines:

- Create your session once, and never again. Since you use Rails, perhaps do it in an initializer.
- It is important your web server does not fork — Unicorn and Passenger both fork. Even though I believe it is possible to use libspotify in a fork, it is extra trickery you don't need before you make the most basic case work.
- Make sure you check out the links at https://github.com/Burgestrand/spotify#links-to-keep-close-at-hand-when-using-libspotify — especially the libspotify FAQ. You need to call #session_process_events regularly or libspotify might disconnect you, and I don't think it'll be enough to do it as a response to requests.
- Make sure you are logged in before trying to use… pretty much everything of the API. You use Rails, so I'd suggest a before filter.

You have the example code that works, and your own code that doesn't. Make the problem space smaller — can you get it to work with your own code outside of a Rails setting?

— Kim Burgestrand


You received this message because you are subscribed to the Google Groups "Spotify for Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-spotify...@googlegroups.com.
Visit this group at http://groups.google.com/group/ruby-spotify.
Reply all
Reply to author
Forward
0 new messages