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 +4 -4
- data/bin/notify-twitch +5 -5
- data/lib/notify_twitch.rb +52 -55
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1b0f9a8529abaab91159a4c4cb035c76807589d2
|
|
4
|
+
data.tar.gz: eb5571deaa57b12bce591e1b1273a026d77d8bce
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|