-
Notifications
You must be signed in to change notification settings - Fork 1
/
xbmc.rb
164 lines (140 loc) · 3.18 KB
/
xbmc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
require 'cinch'
require "net/http"
require "json"
class YouTube
include Cinch::Plugin
listen_to :message, :method => :handler
@@host = 'openelec'
@@port = 80
@@path = '/jsonrpc'
@@user = "xbmc"
@@pass = 'xbmc'
@@ask = nil;
@@videos = []
def send_xbmc_rpc(msg)
req = Net::HTTP::Post.new(@@path, initheader = {'Content-Type' => 'application/json'})
req.basic_auth @@user, @@pass
req.body = msg
result = Net::HTTP.new(@@host, @@port).start {|http| http.request(req)}
if (result.code == "200")
return JSON.parse(result.body)
end
return nil
end
def play(y_id)
msg ={
"jsonrpc" => "2.0",
"method" => "Player.open",
"params" => {
"item" => {
"file" => "plugin://plugin.video.youtube/?action=play_video&videoid=#{y_id}"
}
},
"id" => 1
}.to_json
return send_xbmc_rpc(msg)
end
def pause()
msg ={
"jsonrpc" => "2.0",
"method" => "Player.PlayPause",
"params" => {
"playerid" => 1
},
"id" => 1
}.to_json
send_xbmc_rpc(msg)
end
def stop()
msg ={
"jsonrpc" => "2.0",
"method" => "Player.close",
"params" => {
"playerid" => 1
},
"id" => 1
}.to_json
send_xbmc_rpc(msg)
end
def progress()
msg ={
"jsonrpc" => "2.0",
"method" => "Player.GetProperties",
"params" => {
"playerid" => 1,
"properties" => ["percentage"]
},
"id" => 1
}.to_json
res = send_xbmc_rpc(msg)
return "%.2f \%" % res["result"]["percentage"]
end
def get_item()
msg ={
"jsonrpc" => "2.0",
"method" => "Player.GetItem",
"params" => {
"playerid" => 1,
},
"id" => 1
}.to_json
res = send_xbmc_rpc(msg)
return res["result"]["item"]
end
def handler(m)
# asked for some action
unless (@@ask.nil?)
if (m.message == 'yes')
m.reply "Alright, will do!"
@@ask.call()
@@ask = nil
elsif (m.message == 'no')
m.reply "Ok, I'll forget about it!"
@@ask = nil
end
end
if m.message == "play"
if @@videos.length > 1
y_id = @@videos.pop
play(y_id)
end
end
if m.message == "pause"
pause
m.reply "Type 'pause' again to unpause!"
end
if m.message == "stop"
stop
m.reply "Playback stopped!"
end
if m.message == "status"
perc = progress
item = get_item
puts item.to_s
unless perc.nil? or item.nil?
m.reply "Currently plaing: #{perc} - #{item["label"]} (#{item["type"]})"
end
end
res = /http:\/\/www.youtube.com\/watch\?v=([^&]*)/.match(m.message)
unless (res.nil? or res.length != 2)
y_id = res[1]
m.reply "#{y_id}"
@@ask = lambda { play(y_id) }
m.reply "Shall i play this Video?"
end
end
end
bot = Cinch::Bot.new do
configure do |c|
c.user = "xbmc_youtube"
c.server = "irc.teranetworks.de"
c.port = 6697
c.channels = ["#test"]
c.ssl.use = true
c.plugins.plugins = [YouTube]
end
# on :message, "hello" do |m|
# m.reply "Hello, #{m.user.nick}"
# end
end
bot.start