notify_twitch 0.1.1 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a991b9f062f0dbafdcb14a7885bc396e02ae600b
4
- data.tar.gz: 92da9512413f6b1ac10c916aad497a2b52ca01b4
3
+ metadata.gz: 1b0f9a8529abaab91159a4c4cb035c76807589d2
4
+ data.tar.gz: eb5571deaa57b12bce591e1b1273a026d77d8bce
5
5
  SHA512:
6
- metadata.gz: 681c2fcca8c1a669f4f313e6ce33cc9929f92ae746375d47ae23024f30e81be9f47cdd0a4e341a4c1966edbff2993b3c7c4fba9e794cb9fac1fd3c91a0272f0e
7
- data.tar.gz: 8bf9a69d6448167da662f0f43f66878ddeeebc610fc09e04e14bf16c219c08b35c2e074b88e34db52d9eaf33d27832fce0f98c944ef5e4e856c2fd236d491259
6
+ metadata.gz: 69547b0663bc8e31b0ea76aeef85db256b25facf97c41da28ff17933633831b70eeed9237bfb201c97438534bbe2d7ce518a7dd880c45346ad6a0dcd6e52bf99
7
+ data.tar.gz: 77d07b493bdbe5fce1978c591842108eb55f076b778f1101638931a45ca7839fc406e4c0371778cde6b042f0775f3b713dd526dae4f1ef2e781e9f47b1c82f7e
data/bin/notify-twitch CHANGED
@@ -3,18 +3,18 @@ require "notify_twitch"
3
3
  require "notify_twitch/config"
4
4
  config = Config.new
5
5
  username = config.username
6
- online_followers = get_live_followers(username)
7
- online_followers.each {|f| notify f}
6
+ online_followers = NotifyTwitch.get_live_followers(username)
7
+ online_followers.each {|f| NotifyTwitch.notify f}
8
8
  loop do
9
- updated_followers = get_live_followers(username)
9
+ updated_followers = NotifyTwitch.get_live_followers(username)
10
10
  if updated_followers != online_followers
11
11
  online = updated_followers - online_followers
12
12
  offline = online_followers - updated_followers
13
13
  online.each do |user|
14
- notify user
14
+ NotifyTwitch.notify user
15
15
  end
16
16
  offline.each do |user|
17
- notify user,offline=true
17
+ NotifyTwitch.notify user,offline=true
18
18
  end
19
19
  end
20
20
  online_followers = updated_followers
data/lib/notify_twitch.rb CHANGED
@@ -2,66 +2,63 @@ require "json"
2
2
  require "net/http"
3
3
  require "notify_twitch/config"
4
4
  BASE_API = "https://api.twitch.tv/helix"
5
- def api_call(location,params)
6
- config = Config.new
7
- base = "#{BASE_API}#{location}?"
8
- params.each do |k,v|
9
- if base[-1] == "?"
10
- base << "#{k}=#{v}"
11
- elsif k.nil? && base[-1] == "?"
12
- base << v
13
- elsif k.nil?
14
- base << "&#{v}"
15
- else
16
- base << "&#{k}=#{v}"
5
+ module NotifyTwitch
6
+ extend self
7
+ def api_call(location,params)
8
+ config = Config.new
9
+ base = "#{BASE_API}#{location}?"
10
+ params.each do |k,v|
11
+ if base[-1] == "?"
12
+ base << "#{k}=#{v}"
13
+ elsif k.nil? && base[-1] == "?"
14
+ base << v
15
+ elsif k.nil?
16
+ base << "&#{v}"
17
+ else
18
+ base << "&#{k}=#{v}"
19
+ end
17
20
  end
21
+ url = URI.parse(base)
22
+ Net::HTTP.start(url.host,url.port,:use_ssl => url.scheme == "https") do |http|
23
+ request = Net::HTTP::Get.new(url)
24
+ request["Client-ID"] = config.client_id
25
+ response = http.request request
26
+ if response.code.to_i < 400
27
+ JSON.parse response.body
28
+ else
29
+ raise "Bad status code #{response.code} #{response.body}"
30
+ end
31
+ end
32
+ end
33
+ def get_user_id(username)
34
+ api_call("/users",{:login => username})["data"].first["id"]
35
+ end
36
+ def get_user(id)
37
+ api_call("/users",{:id => id})["data"].first
18
38
  end
19
- url = URI.parse(base)
20
- Net::HTTP.start(url.host,url.port,:use_ssl => url.scheme == "https") do |http|
21
- request = Net::HTTP::Get.new(url)
22
- request["Client-ID"] = config.client_id
23
- response = http.request request
24
- if response.code.to_i < 400
25
- JSON.parse response.body
26
- else
27
- raise "Bad status code #{response.code} #{response.body}"
39
+ def get_followers(id)
40
+ followees_ids = []
41
+ url = URI.parse "#{BASE_API}/users/follows?from_id=#{id}&first=100"
42
+ followees = api_call("/users/follows",{:from_id => id, :first => 100})["data"]
43
+ followees.each do |f|
44
+ followees_ids << f["to_id"]
45
+ sleep 1
28
46
  end
47
+ followees_ids
29
48
  end
30
- end
31
- def get_user_id(username)
32
- api_call("/users",{:login => username})["data"].first["id"]
33
- end
34
- def get_user(id)
35
- api_call("/users",{:id => id})["data"].first
36
- end
37
- def get_followers(id)
38
- followees_ids = []
39
- url = URI.parse "#{BASE_API}/users/follows?from_id=#{id}&first=100"
40
- followees = api_call("/users/follows",{:from_id => id, :first => 100})["data"]
41
- followees.each do |f|
42
- followees_ids << f["to_id"]
49
+ def get_live_followers(username)
50
+ followers_users = []
51
+ id = get_user_id(username)
52
+ followers = get_followers(id).map {|f| "user_id=#{f}"}.join("&")
53
+ api_call("/streams",{:type => "live", :first => 100, nil => followers})["data"].each do |f|
54
+ id = f["user_id"]
55
+ followers_users << get_user(id)["login"]
43
56
  sleep 1
57
+ end
58
+ followers_users
44
59
  end
45
- followees_ids
46
- end
47
- def get_live_followers(username)
48
- followers_users = []
49
- id = get_user_id(username)
50
- followers = get_followers(id).map {|f| "user_id=#{f}"}.join("&")
51
- api_call("/streams",{:type => "live", :first => 100, nil => followers})["data"].each do |f|
52
- id = f["user_id"]
53
- followers_users << get_user(id)["login"]
54
- sleep 1
60
+ def notify(username,offline=false)
61
+ status = !offline ? "online" : "offline"
62
+ %x[notify-send "#{username} is #{status}"]
55
63
  end
56
- followers_users
57
- end
58
-
59
- def notify(username,offline=false)
60
- status = !offline ? "online" : "offline"
61
- %x[notify-send "#{username} is #{status}"]
62
64
  end
63
-
64
-
65
-
66
-
67
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notify_twitch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ShiftMinus