twitchus 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/TODO ADDED
@@ -0,0 +1 @@
1
+ Figure out why there is a Service Unavailable exception and handle it accordingly. It should probably notify a exception logging service.
@@ -3,9 +3,12 @@ require "json"
3
3
 
4
4
  module Twitchus
5
5
  class Checker
6
+ def initialize(config = nil)
7
+ @config = config
8
+ end
6
9
 
7
- def fetch_all(channels, timeout = 0)
8
- channels.map { |channel| check(channel); sleep timeout if timeout }.select { |c| !c.nil? }
10
+ def fetch_all(channels)
11
+ channels.map { |channel| check(channel) }.select { |c| !c.nil? }
9
12
  end
10
13
 
11
14
  # Return a list of online channel names
@@ -15,11 +18,12 @@ module Twitchus
15
18
 
16
19
  def check(channel)
17
20
  raise ArgumentError, "Channel is required." unless channel
18
- response = JSON.parse(RestClient.get(base_url + channel))
21
+ response = RestClient.get(base_url + channel)
22
+ response = JSON.parse(response)
19
23
 
20
- response.first
24
+ response["stream"]
21
25
  rescue RestClient::BadRequest => e
22
- $stderr.puts "RestClient::BadRequest for #{channel}, #{e}"
26
+ $stderr.puts "Request failed due to rate limit, channel: #{channel}, #{e}"
23
27
  nil
24
28
  end
25
29
 
@@ -27,8 +31,14 @@ module Twitchus
27
31
  !check(channel).nil?
28
32
  end
29
33
 
34
+ def url_for(channel)
35
+ url = base_url + channel
36
+ url += "?client_key=#{@config.client_key}" if @config
37
+ url
38
+ end
39
+
30
40
  def base_url
31
- "http://api.justin.tv/api/stream/list.json?channel="
41
+ "https://api.twitch.tv/kraken/streams/"
32
42
  end
33
43
  end
34
44
  end
data/lib/twitchus/cli.rb CHANGED
@@ -2,7 +2,6 @@ require "optparse"
2
2
 
3
3
  module Twitchus
4
4
  class Cli
5
-
6
5
  attr_reader :options
7
6
 
8
7
  def initialize
@@ -58,5 +57,4 @@ module Twitchus
58
57
  end
59
58
 
60
59
  end
61
-
62
60
  end
@@ -21,9 +21,8 @@ module Twitchus
21
21
  @data["key"]
22
22
  end
23
23
 
24
- # TODO - remove duplication
25
- def channels
26
- @streams
24
+ def client_key
25
+ @data["client_key"]
27
26
  end
28
27
 
29
28
  end
@@ -1,3 +1,3 @@
1
1
  module Twitchus
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -3,22 +3,22 @@ module Twitchus
3
3
  def initialize(config_file)
4
4
  @config = Twitchus::Config.new
5
5
  @config.load(config_file)
6
- @checker = Twitchus::Checker.new
6
+ @checker = Twitchus::Checker.new(@config)
7
7
  end
8
8
 
9
9
  def run
10
10
  @storage = Twitchus::Storage.new(@config.host, @config.port, @config.key)
11
- # Empty the online channel list first
11
+ # Since this can run periodically, clean the list first
12
12
  @storage.clear
13
13
 
14
- online_channels = @checker.fetch_all(@config.channels, 0.5)
15
- online_channels.each do |channel|
16
- next if Integer === channel
17
-
14
+ online_streams = @checker.fetch_all(@config.streams)
15
+ online_streams.each do |data|
16
+ channel = data["channel"]
18
17
  data = {
19
- name: channel["title"],
20
- channel: c["login"],
21
- image: c["image_url_huge"]
18
+ title: channel["status"],
19
+ name: channel["name"],
20
+ image: channel["banner"],
21
+ preview: data["preview"]
22
22
  }
23
23
 
24
24
  @storage.push data.to_json
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,5 @@
1
1
  require "twitchus"
2
2
  require "pry"
3
- require "vcr"
4
3
 
5
- VCR.configure do |c|
6
- c.cassette_library_dir = "spec/fixtures/vcr"
7
- c.hook_into :webmock
8
- end
4
+ require "webmock"
5
+ include WebMock::API
@@ -3,30 +3,41 @@ require "spec_helper"
3
3
  module Twitchus
4
4
 
5
5
  describe Checker do
6
+ context "without a config" do
7
+ let(:checker) { Checker.new }
6
8
 
7
- let(:c) { Checker.new }
9
+ it "can tell if a channel is online" do
10
+ stub_request(:get, "https://api.twitch.tv/kraken/streams/scvrush1")
11
+ .to_return(status: 200, body: { "stream" => {} }.to_json)
8
12
 
9
- it "can tell if a channel is online" do
10
- VCR.use_cassette "justin.tv", record: :new_episodes do
11
- c.should be_online("dreamhacktv")
13
+ checker.should be_online("scvrush1")
12
14
  end
13
- end
14
15
 
15
- it "can tell if a channel is offline" do
16
- VCR.use_cassette "justin.tv", record: :new_episodes do
17
- c.should_not be_online("scvrush1")
16
+ it "can tell if a channel is offline" do
17
+ stub_request(:get, "https://api.twitch.tv/kraken/streams/scvrush1")
18
+ .to_return(status: 200, body: { "stream" => nil }.to_json)
19
+
20
+ checker.should_not be_online("scvrush1")
21
+ end
22
+
23
+ it "selects online streams from a list" do
24
+ stub_request(:get, "https://api.twitch.tv/kraken/streams/online")
25
+ .to_return(status: 200, body: { "stream" => { "name" => "online"} }.to_json)
26
+ stub_request(:get, "https://api.twitch.tv/kraken/streams/offline")
27
+ .to_return(status: 200, body: { "stream" => nil }.to_json)
28
+
29
+ checker.fetch_all(%w(online offline)).should == [ { "name" => "online" } ]
18
30
  end
19
31
  end
20
32
 
21
- describe "#check_all" do
33
+ context "with a config" do
34
+ let(:config) { Struct.new(:client_key).new("foobarkey") }
35
+ let(:checker) { Checker.new(config) }
22
36
 
23
- it "selects only those channels which are online" do
24
- VCR.use_cassette "justin.tv", record: :new_episodes do
25
- c.check_all(%w{dreamhacktv scvrush1}).should == ["dreamhacktv"]
26
- end
37
+ it "appends the client key to base_url" do
38
+ checker.url_for("scvrush1").should == "https://api.twitch.tv/kraken/streams/scvrush1?client_key=foobarkey"
27
39
  end
28
40
  end
29
41
 
30
42
  end
31
-
32
43
  end
@@ -6,7 +6,7 @@ module Twitchus
6
6
 
7
7
  let(:config) { Config.new }
8
8
 
9
- it "can load config with multiple streams" do
9
+ it "load config with multiple streams" do
10
10
  file =<<YAML
11
11
  streams:
12
12
  - scvrush1
@@ -17,5 +17,14 @@ YAML
17
17
  config.streams.should == %w{scvrush1 scvrush2}
18
18
  end
19
19
 
20
+ it "loads client key" do
21
+ file =<<YAML
22
+ client_key: foo
23
+ YAML
24
+ File.write("./tmp/config.yml", file)
25
+ config.load("./tmp/config.yml")
26
+ config.client_key.should == "foo"
27
+ end
28
+
20
29
  end
21
30
  end
data/twitchus.gemspec CHANGED
@@ -21,7 +21,6 @@ Gem::Specification.new do |gem|
21
21
  gem.add_dependency "redis", "~> 3.0.1"
22
22
 
23
23
  gem.add_development_dependency "webmock", "~> 1.8.10"
24
- gem.add_development_dependency "vcr", "~> 2.2.5"
25
24
  gem.add_development_dependency "pry", "~> 0.9.10"
26
25
  gem.add_development_dependency "rake", "~> 0.9.2.2"
27
26
  gem.add_development_dependency "rspec", "~> 2.11.0"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitchus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-09 00:00:00.000000000 Z
12
+ date: 2012-11-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -59,22 +59,6 @@ dependencies:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: 1.8.10
62
- - !ruby/object:Gem::Dependency
63
- name: vcr
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ~>
68
- - !ruby/object:Gem::Version
69
- version: 2.2.5
70
- type: :development
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ~>
76
- - !ruby/object:Gem::Version
77
- version: 2.2.5
78
62
  - !ruby/object:Gem::Dependency
79
63
  name: pry
80
64
  requirement: !ruby/object:Gem::Requirement
@@ -169,6 +153,7 @@ files:
169
153
  - LICENSE.txt
170
154
  - README.md
171
155
  - Rakefile
156
+ - TODO
172
157
  - bin/twitchus
173
158
  - config.example.yml
174
159
  - features/config.feature
@@ -180,7 +165,6 @@ files:
180
165
  - lib/twitchus/storage.rb
181
166
  - lib/twitchus/version.rb
182
167
  - lib/twitchus/worker.rb
183
- - spec/fixtures/vcr/justin_tv.yml
184
168
  - spec/spec_helper.rb
185
169
  - spec/twitchus/checker_spec.rb
186
170
  - spec/twitchus/cli_spec.rb
@@ -203,7 +187,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
203
187
  version: '0'
204
188
  segments:
205
189
  - 0
206
- hash: -1490566209112504012
190
+ hash: -2009003698380875512
207
191
  required_rubygems_version: !ruby/object:Gem::Requirement
208
192
  none: false
209
193
  requirements:
@@ -212,7 +196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
212
196
  version: '0'
213
197
  segments:
214
198
  - 0
215
- hash: -1490566209112504012
199
+ hash: -2009003698380875512
216
200
  requirements: []
217
201
  rubyforge_project:
218
202
  rubygems_version: 1.8.23
@@ -222,7 +206,6 @@ summary: Twitchus is a gem for managing a list of Twitch.tv streams
222
206
  test_files:
223
207
  - features/config.feature
224
208
  - features/support/env.rb
225
- - spec/fixtures/vcr/justin_tv.yml
226
209
  - spec/spec_helper.rb
227
210
  - spec/twitchus/checker_spec.rb
228
211
  - spec/twitchus/cli_spec.rb
@@ -1,136 +0,0 @@
1
- ---
2
- http_interactions:
3
- - request:
4
- method: get
5
- uri: http://api.justin.tv/api/stream/list.json?channel=dreamhacktv
6
- body:
7
- encoding: US-ASCII
8
- string: ''
9
- headers:
10
- Accept:
11
- - ! '*/*; q=0.5, application/xml'
12
- Accept-Encoding:
13
- - gzip, deflate
14
- User-Agent:
15
- - Ruby
16
- response:
17
- status:
18
- code: 200
19
- message: !binary |-
20
- T0s=
21
- headers:
22
- !binary "U2VydmVy":
23
- - !binary |-
24
- bmdpbng=
25
- !binary "RGF0ZQ==":
26
- - !binary |-
27
- U3VuLCAyMyBTZXAgMjAxMiAxNTo1MDozMiBHTVQ=
28
- !binary "Q29udGVudC1UeXBl":
29
- - !binary |-
30
- YXBwbGljYXRpb24vanNvbg==
31
- !binary "VHJhbnNmZXItRW5jb2Rpbmc=":
32
- - !binary |-
33
- Y2h1bmtlZA==
34
- !binary "Q29ubmVjdGlvbg==":
35
- - !binary |-
36
- Y2xvc2U=
37
- !binary "U3RhdHVz":
38
- - !binary |-
39
- MjAwIE9L
40
- !binary "Vmlh":
41
- - !binary |-
42
- VHdpY2UgMC4yIHdlYjExOjMzNDc=
43
- !binary "WC1SdW50aW1l":
44
- - !binary |-
45
- MC4wMjQ4NjA=
46
- !binary "WC1VYS1Db21wYXRpYmxl":
47
- - !binary |-
48
- SUU9RWRnZSxjaHJvbWU9MQ==
49
- !binary "Q2FjaGUtQ29udHJvbA==":
50
- - !binary |-
51
- bWF4LWFnZT0wLCBwcml2YXRlLCBtdXN0LXJldmFsaWRhdGU=
52
- !binary "WC1HZW8=":
53
- - !binary |-
54
- Q1o=
55
- !binary "RnJvbnQtRW5kLUh0dHBz":
56
- - !binary |-
57
- b2Zm
58
- !binary "Q29udGVudC1FbmNvZGluZw==":
59
- - !binary |-
60
- Z3ppcA==
61
- body:
62
- encoding: ASCII-8BIT
63
- string: !binary |-
64
- H4sIAAAAAAAAA7VWbW/bNhD+KwIH9JNtvdmWLTQdgr5sAYrlg7EO2zIIlHSS
65
- mFKkQJGWvaH/fUcpipQsWFMENSDAlO6Od8899/LnPwTqFPIkk0ZoEpOtt16T
66
- BRG0BjxxdoTEtKCSXAGtK5p91kf83Gp7vFfyg2gb4OuMaiilOqNmSWsmSnxX
67
- SFVTa9nasjIVFQL4qLvZ+n60IJppbi98Z+3+jNc476/+uHauGxCx84lyEBmj
68
- zoerXy4/Ou8uf7d2gWqjICexVgbQJaZhiiLydyhDU/Q9UdBIpa1kQXmLoncu
69
- kHgMHgRN+cxUpgBEktEmMYonNeTM1OhcpXUTu26rqWbZMsvF6lYfRbcSoN1G
70
- wZFB17pPQrb0N97J98PVbWMxGaO9LgqGcXFnCvvQI4syCBqGR2JhOJ+QTUbV
71
- n0Z8W5NOsD8UnqdhzHJuUXbw91qmt5BpR58buLghtGk4Q0NMCve0bCuZfe7o
72
- EZYFp211Q5wKWFlpFAz2Gzx2LNcVnsJNiCeW498+8OGahtMzcmbUzammKHAH
73
- X9d1q1vTaiZW+uiioRL0HWxz7VXbFT/eZepixj68Li0zyaVCkz94/e+GvHnd
74
- UEVrx/LWRsO57D4gcoc+lahzpNzYL5YseHSfUmgzxRpNswzadqZCeUfP9sVj
75
- pVpiymeC3x7gE0Z70I5UPfDAaJlYUC96Br96ApZXyEqlk6PkBhEIbI7QXXdI
76
- 8psbgYzC8Oa0bmtE6WWsjrzTZuQ0q2mJvQLr5TmGsXCGvtKwzPK8dWcpXjZK
77
- FoxD0ttcbmC/XYdB5EOR5Wm4X+K1kYelBLaWbDWa9ptah2Y1/C2FrYT3RskG
78
- 3INGvleS28KbFdRYbUNZTRFWprTK/9cPXhTg1vNO+IwhIhy5yUCNrW5yBIvI
79
- Ntvv5gh2rc29Gww7aOCH3hZbwIJMTnCqvi8cIcKBzwjHOEGQa1PsD7vKo2n1
80
- kPjPyN5Xuvk29E7raGQ+p6I0SFR0Bmyd9XNgmo17LwiitY8fJsieNVJeRKF+
81
- 4Ey5e1T7z0nZVzAIA+8UrG1S+olGS6zBoU64LJlAMOZJ+GJhyUEmwxwhcRR4
82
- C/Jf5KxIyrTCRQLJtg3DVeB7+zCyhEP6kXC3Cfz1Pth5eGkNmia4aFjkD9j+
83
- 3ipaaOfqKnZ+w92jdWThfGQpKH1G4VRJmme01baOSIFKs3cJTg/cUUJcGUzO
84
- JCYvhwzF6iZEMdNgIxhuMcI5QOMEoeNt4rUXr/dO4Pl29xnC6+ciiX10cEFK
85
- kGjj1wN+HTlruTFSw99tvV24w+1n/IqtZ5hBOIXvt5VhKD5eUYYFzA5vvOJu
86
- tRpcGH2//PSWfPnrX1m4nUXhCQAA
87
- http_version:
88
- recorded_at: Sun, 23 Sep 2012 15:50:32 GMT
89
- - request:
90
- method: get
91
- uri: http://api.justin.tv/api/stream/list.json?channel=scvrush1
92
- body:
93
- encoding: US-ASCII
94
- string: ''
95
- headers:
96
- Accept:
97
- - ! '*/*; q=0.5, application/xml'
98
- Accept-Encoding:
99
- - gzip, deflate
100
- User-Agent:
101
- - Ruby
102
- response:
103
- status:
104
- code: 200
105
- message: OK
106
- headers:
107
- Server:
108
- - nginx
109
- Date:
110
- - Sun, 23 Sep 2012 15:51:43 GMT
111
- Content-Type:
112
- - application/json
113
- Content-Length:
114
- - '2'
115
- Connection:
116
- - close
117
- Status:
118
- - 200 OK
119
- Via:
120
- - Twice 0.2 web9:3347
121
- X-Runtime:
122
- - '0.014319'
123
- X-Ua-Compatible:
124
- - IE=Edge,chrome=1
125
- Cache-Control:
126
- - max-age=0, private, must-revalidate
127
- X-Geo:
128
- - CZ
129
- Front-End-Https:
130
- - 'off'
131
- body:
132
- encoding: US-ASCII
133
- string: ! '[]'
134
- http_version:
135
- recorded_at: Sun, 23 Sep 2012 15:51:44 GMT
136
- recorded_with: VCR 2.2.5