Class: SpotiphyPlayer

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/terminal_player/spotiphy/spotiphy_player.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ SpotiphyPlayer

Returns a new instance of SpotiphyPlayer.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/terminal_player/spotiphy/spotiphy_player.rb', line 7

def initialize(options)
  @options = options

  @plaything = Plaything.new
  setup_session_callbacks

  # HACK not sure this is copacetic
  path = File.expand_path File.dirname(__FILE__)
  path = "#{path}/../../../"
  appkey = IO.read("#{path}spotify_appkey.key", encoding: "BINARY")
  config = Spotify::SessionConfig.new({
    api_version: Spotify::API_VERSION.to_i,
    application_key: appkey,
    cache_location: "#{path}.spotify/",
    settings_location: "#{path}.spotify/",
    tracefile: "#{path}spotify_tracefile.txt",
    user_agent: "terminal_player gem",
    callbacks: Spotify::SessionCallbacks.new($session_callbacks),
  })
  @session = create_session(config)

  (get_env("SPOTIFY_USERNAME"), get_env("SPOTIFY_PASSWORD"))
end

Instance Method Details

#get_track(uri) ⇒ Object



48
49
50
51
52
# File 'lib/terminal_player/spotiphy/spotiphy_player.rb', line 48

def get_track(uri)
  link = Spotify.link_create_from_string(uri)
  track = Spotify.link_as_track(link)
  track
end

#nextObject



44
45
46
# File 'lib/terminal_player/spotiphy/spotiphy_player.rb', line 44

def next
  $end_of_track = true
end

#playObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/terminal_player/spotiphy/spotiphy_player.rb', line 31

def play
  u = @options[:url]
  if u[':track:']
    play_track_from_uri u
  elsif u[':playlist:']
    play_playlist u
  elsif u[':album:']
    play_album u
  else
    puts "unsupported URI #{u}"
  end
end

#play_album(uri) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/terminal_player/spotiphy/spotiphy_player.rb', line 85

def play_album(uri)
  link = Spotify.link_create_from_string(uri)
  browser = Spotify.albumbrowse_create(@session, Spotify.link_as_album(link), proc { }, nil)
  poll(@session) { Spotify.albumbrowse_is_loaded(browser) }
#    album = Spotify.albumbrowse_album(browser)
  num_tracks = Spotify.albumbrowse_num_tracks(browser)
# TODO this should reset channel
#    puts "\nPlaying #{Spotify.album_name(album)} (#{Spotify.album_year(album)}), #{num_tracks} tracks"
  0.upto(num_tracks - 1) do |i|
    track = Spotify.albumbrowse_track(browser, i)
    play_track track
  end
end

#play_playlist(uri) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/terminal_player/spotiphy/spotiphy_player.rb', line 71

def play_playlist(uri)
  link = Spotify.link_create_from_string(uri)
  plist = Spotify.playlist_create(@session, link)
  poll(@session) { Spotify.playlist_is_loaded(plist) }
  num_tracks = Spotify.playlist_num_tracks(plist)
# TODO this should reset channel
#    puts "\nPlaying #{Spotify.playlist_name(plist)}, #{num_tracks} tracks, " +
#         "#{Spotify.playlist_num_subscribers(plist)} subscribers"
  0.upto(num_tracks - 1) do |i|
    track = Spotify.playlist_track(plist, i)
    play_track track
  end
end

#play_track(track) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/terminal_player/spotiphy/spotiphy_player.rb', line 59

def play_track(track)
  wait_for_track_to_load track
  artist = Spotify.track_artist(track, 0)
  notify "SPOTTY #{Spotify.track_name(track)} - #{Spotify.artist_name(artist)}"
  begin
    play_track_raw track
    wait_for_track_to_end
  rescue => e
    puts "play_track: error playing track: #{e}"
  end
end

#play_track_from_uri(uri) ⇒ Object



54
55
56
57
# File 'lib/terminal_player/spotiphy/spotiphy_player.rb', line 54

def play_track_from_uri(uri)
  track = get_track(uri)
  play_track track
end

#play_track_raw(track) ⇒ Object



99
100
101
102
103
# File 'lib/terminal_player/spotiphy/spotiphy_player.rb', line 99

def play_track_raw(track)
  Spotify.try(:session_player_play, @session, false)
  Spotify.try(:session_player_load, @session, track)
  Spotify.try(:session_player_play, @session, true)
end

#wait_for_track_to_endObject



105
106
107
108
# File 'lib/terminal_player/spotiphy/spotiphy_player.rb', line 105

def wait_for_track_to_end
  poll(@session) { $end_of_track }
  $end_of_track = false
end

#wait_for_track_to_load(track) ⇒ Object



110
111
112
# File 'lib/terminal_player/spotiphy/spotiphy_player.rb', line 110

def wait_for_track_to_load(track)
  poll(@session) { Spotify.track_is_loaded(track) }
end