notify_twitch 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a330c51f02391593470dd25fcbc29b75300c676c
4
+ data.tar.gz: 80ad7c8c945e024bdcef35eae3d8575e82479c93
5
+ SHA512:
6
+ metadata.gz: 85f11107937d797ffee02238ccd1039d8e204610f247ae1f8e2d37ab4c4de08b23fa6c8656edee0d6e021f23cc5e57c59c95da9ee7a7a033c50336f320a29cc1
7
+ data.tar.gz: 9625c0120202edb264f698baa9bc8ead13db6e5b8ed119dbf34725352d654fba0dd25acbec7fda2ebcd5f67bad2be97b5485cc2a4b872e547a210c97de86ffbc
data/bin/notify-twitch ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ require "notify_twitch"
3
+ require "notify_twitch/config"
4
+ config = Config.new
5
+ username = config.username
6
+ online_followers = get_live_followers(username)
7
+ online_followers.each {|f| notify f}
8
+ loop do
9
+ updated_followers = get_live_followers(username)
10
+ if updated_followers != online_followers
11
+ online = updated_followers - online_followers
12
+ offline = online_followers - updated_followers
13
+ online.each do |user|
14
+ notify user
15
+ end
16
+ offline.each do |user|
17
+ notify user,offline=true
18
+ end
19
+ end
20
+ online_followers = updated_followers
21
+ sleep 15
22
+ end
23
+
@@ -0,0 +1,23 @@
1
+ require "fileutils"
2
+ require "json"
3
+ class Config
4
+ attr_reader :client_id,:username
5
+ def initialize
6
+ if File.file? "#{ENV['HOME']}/.config/notify-twitch/config.json"
7
+ config = JSON.parse File.read("#{ENV['HOME']}/.config/notify-twitch/config.json")
8
+ @client_id = config["client-id"]
9
+ @username = config["username"]
10
+ else
11
+ puts "Can't find your config :(. Don't Worry I'll make you a new one!"
12
+ puts "Enter your client-id. Can be found at your twitch apps dashboard"
13
+ @client_id = gets.chomp
14
+ puts "Enter your usename"
15
+ @username = gets.chomp
16
+ FileUtils.mkdir_p "#{ENV['HOME']}/.config/notify-twitch" unless File.exists? "#{ENV['HOME']}/.config/notify-twitch"
17
+ File.open("#{ENV['HOME']}/.config/notify-twitch/config.json","w") do |f|
18
+ f << JSON.pretty_generate({"client-id" => @client_id,"username" => @username})
19
+ end
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,67 @@
1
+ require "json"
2
+ require "net/http"
3
+ require "notify_twitch/config"
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}"
17
+ end
18
+ 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}"
28
+ end
29
+ 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"]
43
+ sleep 1
44
+ 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
55
+ 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
+ end
63
+
64
+
65
+
66
+
67
+
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: notify_twitch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ShiftMinus
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - shift.minus.media@gmail.com
58
+ executables:
59
+ - notify-twitch
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - bin/notify-twitch
64
+ - lib/notify_twitch.rb
65
+ - lib/notify_twitch/config.rb
66
+ homepage: https://github.com/shift-minus/notify-twitch
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.5.2.1
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Desktop notifications when Twitch streams come online
90
+ test_files: []