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.
--
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.
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'
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
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."
endend
/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 -0600Processing 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)
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
--
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.
$ 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...
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
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/4f037e99-5a75-4fa7-be37-93b9ad54e182%40googlegroups.com.