douban.fm 0.2.6 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -225,8 +225,22 @@ class DoubanFMCLI
225
225
 
226
226
  # there are a lot more chances to get stack overflow in this mode,
227
227
  # so can't use the proc way
228
+ count = 0
228
229
  while true
229
- @douban_fm.add_to_mpd(options.remote_host, options.remote_port)
230
+ if not @douban_fm.add_to_mpd(options.remote_host, options.remote_port)
231
+ count += 1
232
+
233
+ logger.log("playlist not updated, wait for 10 seconds, total waiting time #{count * 10} seconds")
234
+
235
+ if count == 360
236
+ logger.log('playlist has been updated for around one hour, go clear it')
237
+
238
+ @douban_fmn.clear_mpd_playlist(options.remote_host, options.remote_port)
239
+ count = 0
240
+ end
241
+ else
242
+ count = 0
243
+ end
230
244
  sleep 10
231
245
  end
232
246
  end
@@ -2,11 +2,14 @@ module DoubanFM
2
2
  require 'net/http'
3
3
  require 'json'
4
4
  require 'ruby-mpd'
5
+ require 'date'
5
6
 
6
7
  class DoubanFM
7
8
  # DOUBAN_FM_MPD_PLAYLIST = 'douban.fm'
8
9
  MIN_SONGS_IN_DOUBAN_FM_MPD_PLAYLIST = 10
9
10
 
11
+ RANDOM_CHANNEL_ID = -1
12
+
10
13
  attr_reader :waiting, :channels, :current_channel
11
14
 
12
15
  def initialize(logger = DummyLogger.new, email = '', password = '')
@@ -32,9 +35,16 @@ module DoubanFM
32
35
  end
33
36
 
34
37
  def fetch_channels
35
- uri = URI('http://www.douban.com/j/app/radio/channels')
36
- res = Net::HTTP.get(uri)
37
- @channels = JSON.parse(res)
38
+ today = Date.new
39
+ if today != @last_fetching_channels_date
40
+ uri = URI('http://www.douban.com/j/app/radio/channels')
41
+ res = Net::HTTP.get(uri)
42
+ @channels = JSON.parse(res)
43
+
44
+ @last_fetching_channels_date = today
45
+ else
46
+ @logger.log('use channels in cache')
47
+ end
38
48
 
39
49
  @logger.log("raw channel list #{channels}")
40
50
  end
@@ -44,6 +54,14 @@ module DoubanFM
44
54
  end
45
55
 
46
56
  def fetch_next_playlist
57
+ if @current_channel == RANDOM_CHANNEL_ID
58
+ channel_id = select_random_channel
59
+ else
60
+ channel_id = @current_channel
61
+ end
62
+
63
+ @logger.log("now fetch next playlist from channel #{channel_id}")
64
+
47
65
  uri = URI('http://www.douban.com/j/app/radio/people')
48
66
  params = {
49
67
  :app_name => 'radio_desktop_mac',
@@ -53,7 +71,7 @@ module DoubanFM
53
71
  :token => @user_info['token'],
54
72
  :sid => '',
55
73
  :h => '',
56
- :channel => @current_channel,
74
+ :channel => channel_id,
57
75
  :type => 'n'
58
76
  }
59
77
  uri.query = URI.encode_www_form(params)
@@ -136,6 +154,8 @@ module DoubanFM
136
154
 
137
155
  @logger.log("current total number of songs in mpd #{total}")
138
156
 
157
+ added = false
158
+
139
159
  if total < MIN_SONGS_IN_DOUBAN_FM_MPD_PLAYLIST
140
160
  # douban_fm_playlist = MPD::Playlist.new(mpd, {:playlist => DOUBAN_FM_MPD_PLAYLIST})
141
161
 
@@ -154,9 +174,13 @@ module DoubanFM
154
174
  end
155
175
 
156
176
  add_current_playlist_to_mpd(mpd)
177
+
178
+ added = true
157
179
  end
158
180
 
159
181
  mpd.disconnect
182
+
183
+ added
160
184
  end
161
185
 
162
186
  def clear_mpd_playlist(host = 'localhost', port = 6600)
@@ -223,5 +247,13 @@ module DoubanFM
223
247
  mpd.delete(i)
224
248
  end
225
249
  end
250
+
251
+ def select_random_channel
252
+ fetch_channels
253
+
254
+ channels = @channels['channels']
255
+ which = Random.new.rand(0 ... channels.size)
256
+ channels[which]['channel_id']
257
+ end
226
258
  end
227
259
  end
@@ -1,3 +1,3 @@
1
1
  module DoubanFM
2
- VERSION = '0.2.6'
2
+ VERSION = '0.2.7'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: douban.fm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-19 00:00:00.000000000 Z
12
+ date: 2013-01-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ruby-mpd
16
- requirement: &70282342118460 !ruby/object:Gem::Requirement
16
+ requirement: &70283801171180 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - =
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.1.5
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70282342118460
24
+ version_requirements: *70283801171180
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: highline
27
- requirement: &70282342117600 !ruby/object:Gem::Requirement
27
+ requirement: &70283801170340 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - =
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 1.6.15
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70282342117600
35
+ version_requirements: *70283801170340
36
36
  description: douban.fm
37
37
  email:
38
38
  - hxliang1982@gmail.com