class DoubanFM::Client

Attributes

channels[R]
current_channel[R]
current_song[R]

Public Class Methods

new(email='', password='') click to toggle source
# File lib/doubanfm/doubanfm.rb, line 12
def initialize(email='', password='')
  @client = Net::HTTP
  @current_channel = INVALID_CHANNEL_ID
  @current_song = nil
  @user_info = {user_id: '', expire: '', token: ''}

  if !email.empty? && !password.empty?
    login(email, password)
  end

  fetch_channels
  new_playlist
end

Public Instance Methods

login(email='', password='') click to toggle source
# File lib/doubanfm/doubanfm.rb, line 26
def login(email='', password='')
  resp = @client.post_form(LOGIN_URI,
                            app_name: APP_NAME,
                            version: APP_VERSION,
                            email: email,
                            password: password)
  @user_info = JSON.parse(
    resp.body,
    symbolize_names: true
  )

  if @user_info[:r] == 1
    raise @user_info[:err]
  end
end
next_song() click to toggle source
# File lib/doubanfm/doubanfm.rb, line 55
def next_song
  if @current_song == @playlist.last
    next_playlist
  end

  next_song_pos = @playlist.index(@current_song) + 1
  @current_song = @playlist[next_song_pos]
end
rate() click to toggle source
# File lib/doubanfm/doubanfm.rb, line 64
def rate
  return unless user_logged?
  return if like?

  params = {
    app_name: APP_NAME,
    version: APP_VERSION,
    user_id: @user_info[:user_id],
    expire: @user_info[:expire],
    token: @user_info[:token],
    channel: @current_channel,
    sid: @current_song[:sid],
    type: RATE
  }

  rate_uri = URI('http://www.douban.com/j/app/radio/people')
  rate_uri.query = URI.encode_www_form(params)

  @playlist = JSON.parse(
    @client.get(rate_uri),
    symbolize_names: true
  )[:song]
  @current_song = @playlist.first
end
set_channel(channel_id) click to toggle source
# File lib/doubanfm/doubanfm.rb, line 50
def set_channel(channel_id)
  @current_channel = channel_id
end
unrate() click to toggle source
# File lib/doubanfm/doubanfm.rb, line 89
def unrate
  return unless user_logged?
  return unless like?

  params = {
    app_name: APP_NAME,
    version: APP_VERSION,
    user_id: @user_info[:user_id],
    expire: @user_info[:expire],
    token: @user_info[:token],
    channel: @current_channel,
    sid: @current_song[:sid],
    type: UNRATE
  }

  rate_uri = URI('http://www.douban.com/j/app/radio/people')
  rate_uri.query = URI.encode_www_form(params)

  @playlist = JSON.parse(
    @client.get(rate_uri),
    symbolize_names: true
  )[:song]
  @current_song = @playlist.first
end
user_logged?() click to toggle source
# File lib/doubanfm/doubanfm.rb, line 42
def user_logged?
  if @user_info[:user_id].empty?
    return false
  end

  return true
end

Private Instance Methods

fetch_channels() click to toggle source
# File lib/doubanfm/doubanfm.rb, line 115
def fetch_channels
  @channels = JSON.parse(
    @client.get(CHANNELS_URI),
    symbolize_names: true
  )[:channels]
end
fetch_playlist(type) click to toggle source
# File lib/doubanfm/doubanfm.rb, line 122
def fetch_playlist(type)
  if @current_channel == INVALID_CHANNEL_ID
    channel_id = select_random_channel
    @current_channel = channel_id
  else
    channel_id = @current_channel
  end

  sid = @current_song ? @current_song[:sid] : ''
  params = {
    app_name: APP_NAME,
    version: APP_VERSION,
    user_id: @user_info[:user_id],
    expire: @user_info[:expire],
    token: @user_info[:token],
    channel: channel_id,
    sid: sid,
    type: type
  }

  fetch_playlist_uri = URI('http://www.douban.com/j/app/radio/people')
  fetch_playlist_uri.query = URI.encode_www_form(params)
  @playlist = JSON.parse(
    @client.get(fetch_playlist_uri),
    symbolize_names: true
  )[:song]

  @current_song = @playlist.first
end
like?() click to toggle source
# File lib/doubanfm/doubanfm.rb, line 167
def like?
  return @current_song[:like] == 1 ? true : false;
end
new_playlist() click to toggle source
# File lib/doubanfm/doubanfm.rb, line 159
def new_playlist
  fetch_playlist(NEW_PLAYLIST)
end
next_playlist() click to toggle source
# File lib/doubanfm/doubanfm.rb, line 163
def next_playlist
  fetch_playlist(NEXT_PLAYLIST)
end
select_random_channel() click to toggle source
# File lib/doubanfm/doubanfm.rb, line 152
def select_random_channel
  first_channel = user_logged? ? 1 : 0

  which = Random.new.rand(first_channel ... @channels.size)
  @channels[which][:channel_id]
end