-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver.rb
More file actions
233 lines (200 loc) · 6 KB
/
httpserver.rb
File metadata and controls
233 lines (200 loc) · 6 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# 異常操作について考えていない & 著作権保護のため決してポート開放しないこと
require 'rubygems'
require 'cgi'
require 'webrick'
require 'kconv'
require 'socket'
require "./common.rb"
# kill -9 `ps | grep [r]uby | awk '{print $1}'`
# サーバの生存確認用
# http://localhost:10080/hello?name=zen6kun
class HelloServlet < WEBrick::HTTPServlet::AbstractServlet
def do_GET(req, res)
if req.query['name']
res.body = "Hello #{req.query['name']}."
else
res.body = "Hello world."
end
res['Content-Type'] = 'text/plain'
end
end
# 応用ソフト用
class ClientServlet < WEBrick::HTTPServlet::AbstractServlet
def do_GET(req, res)
puts "req.query['cmd']=#{req.query['cmd']}"
if req.query['cmd'] == "getall"
# http://localhost:10080/exec?cmd=getall
# 必ずall.jsonが存在すると仮定する
res.body = File.open($mediaDir+"all.json").read
res.content_type = 'application/json; charset=UTF-8'
elsif req.query['cmd'] == "rate"
# http://localhost:10080/exec?cmd=rate&serialid=[]&videoid=[]&rate=[]
serialid = req.query['serialid']
videoid = req.query['videoid'].to_i
rate = req.query['rate'].to_f
puts "serialid=#{serialid}, videoid=#{videoid}, rate=#{rate}"
elsif req.query['image']
# http://localhost:10080/exec?image=[id]
name = $mediaDir + "#{req.query['image']}.png"
if File.exist?(name)
res.body = File.open(name).binmode.read
res.content_type = 'image/png'
else
res.status = 404 # Not Found
end
else
res.body = "Hello world."
res.content_type = 'text/plain'
end
end
end
# Web Interface UI
# http://localhost:10080/ui
class UIServlet < WEBrick::HTTPServlet::AbstractServlet
def convertAllToHTML(sid, search)
begin
all = JSON.parse(File.open("../all.json", "r").read.toutf8)
content = File.open("content.htm", "r").read.toutf8
frame = File.open("frame.htm", "r").read.toutf8
rescue
puts "error"
return
end
all.sort!{|a, b| b["id"].to_s[4..-1] <=> a["id"].to_s[4..-1]} # 新しい順
if search != nil
words = search.split(" ")
exwords = words.select{|w| w =~ /^-/}
words -= exwords
exwords.map!{|w| w.gsub(/^-/, "")}
#
puts "words=#{words}"
puts "exwords=#{exwords}"
end
lis = []
all.each do |tv|
# search
if search != nil
if words.size > 0
hit = false
words.each do |w|
break if hit
hit = tv["title"].include?(w) || tv["genre"].include?(w) || tv["description"].include?(w) || tv["contents"].include?(w)
end
next if !hit
end
if exwords.size > 0
neghit = false
exwords.each do |w|
break if neghit
neghit = tv["title"].include?(w) || tv["genre"].include?(w) || tv["description"].include?(w) || tv["contents"].include?(w)
end
next if neghit
end
end
# sid
if sid != nil
next if tv["id"].to_s[0..3] != sid.to_s
end
# 番組ごとのHTML
li = ""
li << content.gsub("%IMGURL%", "/exec?image=" + tv["id"].to_s).gsub("%TITLE%", tv["title"]).gsub("%ABSTRACT%", tv["description"]).gsub("%WATCHURL%", "/ui?watch=#{tv["id"]}")
li << "\r\n"
lis.push li
break if lis.size >= 4 * 39 if DEBUG
end
# 番組ごとのHTMLを組み合わせる
ret = ""
i = 0
while i < lis.size
if i%4 == 0
inner = ""
end
inner << lis[i]
inner << "\r\n"
if i%4 == 3
ret << frame.gsub("%LIS%", inner)
ret << "\r\n"
end
i += 1
end
if (i-1)%4 != 3
ret << frame.gsub("%LIS%", inner)
ret << "\r\n"
end
return ret
end
def createWeb(sid, search)
puts "createWeb"
Dir.chdir($mediaDir)
Dir.mkdir("web") if !Dir.exist?("web")
Dir.chdir("web")
ret = ""
nav = ""
$tvnumbers.keys.each do |key|
nav << "<li><a href=\"ui?sid=#{$tvnumbers[key]}\">#{key}</a></li>"
end
ret << File.open("head.htm", "r").read.gsub("%NAV%", nav).gsub("%INPUT_TEXT%", search == nil ? "" : search)
html = convertAllToHTML(sid, search)
ret << html
ret << File.open("tail.htm", "r").read
return ret
end
def do_GET(req, res)
# http://localhost:10080/ui?sid=[1024,1032,1040,1048,1056,1064,1072]&q=[URL encoded query words]
if req.query['watch'] != nil
puts "watch"
videoid = req.query['watch']
cmd = "#{$vlcpath} file:///#{$mediaDir}#{videoid}.ts --quiet" # 現在はVLCのみに対応
puts cmd
t = Thread.new{ system(cmd) }
res.content_type = 'text/html'
res.body = "<html><body onLoad='window.close();'></body></html>"
else
sid = req.query['sid'].to_i
sid = nil unless $tvnumbers.values.include?(sid)
puts "sid = #{sid}"
search = req.query['q']
search = CGI.unescape(search) if search != nil
search = nil if search == ""
puts "search = #{search}"
res.content_type = 'text/html'
res.body = createWeb(sid, search)
res.chunked = true
end
end
end
addressList = Socket.getaddrinfo(Socket::gethostname, nil, Socket::AF_INET)
addressList.each_index do |i|
puts "index:#{i}"
p addressList[i]
end
if (defined?(Ocra))
index = 0
else
if addressList.size <= 0
puts "Unknown error."
exit
end
if addressList.size >= 2
puts "select interface index[default:0]: "
index = STDIN.gets.to_i
else
index = 0
end
end
#
myIP = Socket.getaddrinfo(Socket::gethostname, nil, Socket::AF_INET)[index][3]
puts "myIP = #{myIP}"
server = WEBrick::HTTPServer.new({:DoNotReverseLookup => true, :Port => 10080, :BindAddress => myIP, :DocumentRoot => './'})
server.mount('/hello', HelloServlet)
server.mount('/exec', ClientServlet)
server.mount('/ui', UIServlet)
trap('INT') { server.shutdown }
if (defined?(Ocra))
Thread.new {
sleep 3
puts "server.stop"
server.stop
}
end
server.start