Class: DoubanFM::Client

Inherits:
Object
  • Object
show all
Includes:
ActionType, Constants
Defined in:
lib/doubanfm/doubanfm.rb

Constant Summary

Constants included from ActionType

ActionType::BYE, ActionType::FINISH, ActionType::NEW_PLAYLIST, ActionType::NEXT_PLAYLIST, ActionType::RATE, ActionType::SKIP, ActionType::UNRATE

Constants included from Constants

DoubanFM::Constants::APP_NAME, DoubanFM::Constants::APP_VERSION, DoubanFM::Constants::CHANNELS_URI, DoubanFM::Constants::INVALID_CHANNEL_ID, DoubanFM::Constants::LOGIN_URI, DoubanFM::Constants::PERSONAL_CHANNEL_ID, DoubanFM::Constants::RED_HEART_CHANNEL_ID

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email = '', password = '') ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
# 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?
    (email, password)
  end

  fetch_channels
  new_playlist
end

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



10
11
12
# File 'lib/doubanfm/doubanfm.rb', line 10

def channels
  @channels
end

#current_channelObject (readonly)

Returns the value of attribute current_channel.



10
11
12
# File 'lib/doubanfm/doubanfm.rb', line 10

def current_channel
  @current_channel
end

#current_songObject (readonly)

Returns the value of attribute current_song.



10
11
12
# File 'lib/doubanfm/doubanfm.rb', line 10

def current_song
  @current_song
end

Instance Method Details

#login(email = '', password = '') ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/doubanfm/doubanfm.rb', line 26

def (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_songObject



55
56
57
58
59
60
61
62
# 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

#rateObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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) ⇒ Object



50
51
52
# File 'lib/doubanfm/doubanfm.rb', line 50

def set_channel(channel_id)
  @current_channel = channel_id
end

#unrateObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/doubanfm/doubanfm.rb', line 42

def user_logged?
  if @user_info[:user_id].empty?
    return false
  end

  return true
end