douban.fm 0.0.1

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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in douban.fm.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 ehonlia
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ # Douban.fm
2
+
3
+ Play music from douban.fm.
4
+
5
+ __Note that this project is still on-going, so I won't promise anything.__
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'douban.fm'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install douban.fm
20
+
21
+ ## Usage
22
+
23
+ * to play your private playlist
24
+
25
+ `douban.fm <email> <password>`
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'douban.fm'
4
+
5
+ unless ARGV.length == 2
6
+ p "douban_fm.rb <email> <password>"
7
+ exit
8
+ end
9
+
10
+ email = ARGV[0]
11
+ password = ARGV[1]
12
+ douban_fm = DoubanFM::DoubanFM.new(email, password)
13
+ douban_fm.login
14
+ douban_fm.get_channels
15
+ douban_fm.select_channel(0)
16
+
17
+ # stop = false
18
+ # go_on = true
19
+ # while not stop
20
+ # if go_on
21
+ # p "================="
22
+ # go_on = false
23
+ # douban_fm.get_next_playlist
24
+ # douban_fm.play_current_playlist do |waiting|
25
+ # unless waiting
26
+ # stop = true
27
+ # break
28
+ # else
29
+ # go_on = true
30
+ # end
31
+ # end
32
+ # else
33
+ # sleep 10
34
+ # end
35
+ # end
36
+
37
+ play_proc = proc do |waiting|
38
+ if waiting
39
+ douban_fm.get_next_playlist
40
+ douban_fm.play_current_playlist do |waiting|
41
+ play_proc.call(waiting)
42
+ end
43
+ end
44
+ end
45
+
46
+ play_proc.call(true)
47
+
48
+ sleep
@@ -0,0 +1,18 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'douban.fm/version'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "douban.fm"
7
+ gem.version = DoubanFM::VERSION
8
+ gem.authors = ["honnix"]
9
+ gem.email = ["hxliang1982@gmail.com"]
10
+ gem.description = %q{douban.fm}
11
+ gem.summary = %q{douban.fm}
12
+ gem.homepage = "https://github.com/honnix/douban.fm"
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+ end
@@ -0,0 +1,2 @@
1
+ require 'douban.fm/version'
2
+ require 'douban.fm/douban_fm'
@@ -0,0 +1,94 @@
1
+ module DoubanFM
2
+ require 'net/http'
3
+ require 'json'
4
+
5
+ class DoubanFM
6
+ attr_reader :waiting
7
+
8
+ def initialize(email, password)
9
+ @email = email
10
+ @password = password
11
+ @semaphore = Mutex.new
12
+ @waiting = false # read this to determin whether to get one more playlist
13
+ end
14
+
15
+ def login
16
+ uri = URI('http://www.douban.com/j/app/login')
17
+ res = Net::HTTP.post_form(uri,
18
+ 'email' => @email,
19
+ 'password' => @password,
20
+ 'app_name' => 'radio_desktop_mac',
21
+ 'version' => '100')
22
+ @user_info = JSON.parse(res.body)
23
+ if @user_info['err'] != 'ok'
24
+ raise @user_info["err"]
25
+ end
26
+ end
27
+
28
+ def get_channels
29
+ uri = URI('http://www.douban.com/j/app/radio/channels')
30
+ res = Net::HTTP.get(URI('http://www.douban.com/j/app/radio/channels'))
31
+ @channels = JSON.parse(res)
32
+ end
33
+
34
+ def select_channel(channel_num)
35
+ @current_channel = channel_num
36
+ end
37
+
38
+ def get_next_playlist
39
+ uri = URI('http://www.douban.com/j/app/radio/people')
40
+ params = {
41
+ :app_name => 'radio_desktop_mac',
42
+ :version => "100",
43
+ :user_id => @user_info['user_id'],
44
+ :expire => @user_info['expire'],
45
+ :token => @user_info['token'],
46
+ :sid => '',
47
+ :h => '',
48
+ :channel => @current_channel,
49
+ :type => 'n'
50
+ }
51
+ uri.query = URI.encode_www_form(params)
52
+ res = Net::HTTP.get_response(uri)
53
+
54
+ @current_playlist = JSON.parse(res.body)
55
+ end
56
+
57
+ def play_current_playlist
58
+ @continue = true
59
+
60
+ Thread.new do
61
+ @waiting = false
62
+
63
+ @current_playlist['song'].each do |song|
64
+ @semaphore.lock
65
+
66
+ unless @continue
67
+ @semaphore.unlock
68
+ break
69
+ end
70
+
71
+ @player_pid = spawn("mpg123 #{song['url']}")
72
+
73
+ @semaphore.unlock
74
+
75
+ Process.wait
76
+ end
77
+
78
+ @waiting = @continue
79
+ yield @waiting if block_given?
80
+ end
81
+ end
82
+
83
+ def stop
84
+ @semaphore.synchronize do
85
+ @continue = false
86
+
87
+ begin
88
+ Process.kill(9, @player_pid)
89
+ rescue Errno::ESRCH
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,3 @@
1
+ module DoubanFM
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: douban.fm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - honnix
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: douban.fm
15
+ email:
16
+ - hxliang1982@gmail.com
17
+ executables:
18
+ - douban.fm
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/douban.fm
28
+ - douban.fm.gemspec
29
+ - lib/douban.fm.rb
30
+ - lib/douban.fm/douban_fm.rb
31
+ - lib/douban.fm/version.rb
32
+ homepage: https://github.com/honnix/douban.fm
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.15
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: douban.fm
56
+ test_files: []
57
+ has_rdoc: