shoutcast_status 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,73 @@
1
+ require "open-uri"
2
+ require "johnson"
3
+
4
+ class ShoutcastStatus
5
+ DEFAULT_INFO_URL = "http://%s/cast/js.php/%s/streaminfo"
6
+
7
+ DUMMY_CALLBACK = <<-END
8
+ function cc_streaminfo_get_callback(details){
9
+ return details;
10
+ }
11
+ END
12
+
13
+ # Create a ShoutcastStatus instance for a given station.
14
+ # If one parameter is given, it is taken as the url to the now playing JSONP
15
+ # callback. If two parameters are given, they are interpreted as the station
16
+ # host and station name; the information URL will be inferred from this.
17
+ #
18
+ def initialize(url_or_host, station=nil)
19
+ if station
20
+ @info_url = DEFAULT_INFO_URL % [url_or_host, station]
21
+ else
22
+ @info_url = url_or_host
23
+ end
24
+ end
25
+
26
+ # Return a StreamInfo object containing the current state of the stream.
27
+ #
28
+ def stream_info
29
+ open @info_url do |f|
30
+ js = f.read
31
+ return StreamInfo.new(Johnson.evaluate(DUMMY_CALLBACK + js))
32
+ end
33
+ end
34
+
35
+ class StreamInfo
36
+ def initialize(object)
37
+ @object = object
38
+ end
39
+
40
+ def artist
41
+ artist_and_title.first
42
+ end
43
+
44
+ def title
45
+ artist_and_title.last
46
+ end
47
+
48
+ def listeners
49
+ @object["listeners"]
50
+ end
51
+
52
+ def max_listeners
53
+ @object["maxlisteners"]
54
+ end
55
+
56
+ def station
57
+ @object["title"]
58
+ end
59
+
60
+ def bitrate
61
+ @object["bitrate"]
62
+ end
63
+
64
+ def url
65
+ @object["url"]
66
+ end
67
+
68
+ private
69
+ def artist_and_title
70
+ @object["song"].split(/ - /, 2)
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,46 @@
1
+ $:.unshift(File.expand_path("../../lib", __FILE__))
2
+ require "test/unit"
3
+ require "mocha"
4
+ require "stringio"
5
+ require "shoutcast_status"
6
+
7
+ class ShoutcastStatusTest < Test::Unit::TestCase
8
+
9
+ def test_should_make_url_from_host_and_station_name
10
+ io = StringIO.new("cc_streaminfo_get_callback({});")
11
+ sc = ShoutcastStatus.new("example.com", "punkrock")
12
+ sc.expects(:open).
13
+ with("http://example.com/cast/js.php/punkrock/streaminfo").
14
+ yields(io)
15
+ sc.stream_info
16
+ end
17
+
18
+ def test_should_use_url_as_supplied
19
+ io = StringIO.new("cc_streaminfo_get_callback({});")
20
+ sc = ShoutcastStatus.new("http://example.com/some/path")
21
+ sc.expects(:open).
22
+ with("http://example.com/some/path").
23
+ yields(io)
24
+ sc.stream_info
25
+ end
26
+
27
+ def test_should_extract_details_from_sample
28
+ sample = <<'END'
29
+ var arr952a05ca = { 'title': 'Radio Punk','song': 'Sex Pistols - Anarchy in the UK','bitrate': 128,'server': 'Online','source': 'Online','offline': '','summary': '\x3Ca href=\"http://example.com/cast/tunein.php/punkrock/tunein.pls\"\x3ERadio Punk - Sex Pistols - Anarchy in the UK\x3C/a\x3E','url': 'http://example.com/cast/js.php/punkrock/streaminfo','listeners': 2,'maxlisteners': 25,'reseller': 0 };
30
+
31
+ cc_streaminfo_get_callback(arr952a05ca);
32
+ END
33
+
34
+ io = StringIO.new(sample)
35
+ sc = ShoutcastStatus.new("example.com", "punkrock")
36
+ sc.stubs(:open).yields(io)
37
+ info = sc.stream_info
38
+
39
+ assert_equal "Radio Punk", info.station
40
+ assert_equal "Sex Pistols", info.artist
41
+ assert_equal "Anarchy in the UK", info.title
42
+ assert_equal 128, info.bitrate
43
+ assert_equal 2, info.listeners
44
+ assert_equal 25, info.max_listeners
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shoutcast_status
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paul Battley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-07-09 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: johnson
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: mocha
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description:
36
+ email: pbattley@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - lib/shoutcast_status.rb
45
+ - test/shoutcast_status_test.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/threedaymonk/shoutcast_status
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Get station info from a Shoutcast server
74
+ test_files: []
75
+