-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathustream.rb
More file actions
119 lines (106 loc) · 2.28 KB
/
ustream.rb
File metadata and controls
119 lines (106 loc) · 2.28 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
require 'timeout'
module Ustream
class Common
def get_ustream_url
return @ustream_url
end
def remove_invalid_char str
return "" if str.nil?
return str.gsub(/[^0-9a-zA-Z~!@#$\%^&*\(\)_\-=+{}:\;'"<>\/,.\?]/,"")
end
def get_cid text
text =~ /;cid=([0-9]*)/
return $1
end
def get_title(text)
text =~ /<meta\s?property="og:title"\s*content="(.*?)"\s*\/>/
return $1
end
def get_streamname(text)
text =~ /streamName(.*)liveHttp/
return remove_invalid_char $1
end
def get_viewers(text)
text =~ /viewerNumber(.*)useUmsViewerCount/
return remove_invalid_char $1
end
def get_amf(url)
begin
url =~ /http:\/\/www.ustream.tv\/([^\/]*)\/?(.*)#?/
@channel = $1
@ust_name = CGI.escape($2)
# puts "1:"+$1.to_s
# puts "2:"+$2.to_s
uri = "http://www.ustream.tv/%s/%s"%[@channel,@ust_name]
timeout(10) do
open(uri) do |file|
line = file.read
@cid = get_cid line
@title = get_title line
break unless @cid.nil? || @title.nil?
end
end
@amf_url = $amf_url%@cid
rescue Timeout::Error
puts "timeout: %s"%url
rescue => exc
puts exc.backtrace
end
end
def get_stream_url2(text)
text =~ /[cdnUrl|fmsUrl].*(rtmp:\/\/)(.*)$/
return remove_invalid_char '%s%s'%[$1,$2]
end
def get_hashtag(text)
text = remove_invalid_char text
if text =~ /[(]([^\s|^)]*)liveathttp:[^)]*/
# puts "hashtag: #{$1}"
return $1
end
return nil
end
def get_stream_url(amf_url)
begin
uri = URI.parse(amf_url)
timeout(10) do
uri.open do |file|
line = file.read
@video_url = get_stream_url2 line
@streamname = get_streamname line
@hashtag = get_hashtag line
@viewers = get_viewers line
end
end
rescue Timeout::Error
puts "timeout: %s(%s)"%[@title,@cid]
rescue => exc
puts exc
puts exc.backtrace
end
end
def getinfo(cid)
begin
uri = URI.parse($api_uri%cid)
@xml=nil
doc=''
timeout(10) do
uri.open {|file| doc.concat(file.read) }
@xml = REXML::Document.new(doc)
end
rescue Timeout::Error
puts "timeout : %s(%s)"%[@title,@cid]
rescue
raise
end
end
def getTitle
return @title
end
def isOnline?
getinfo @cid
flg = @xml.elements['xml/results/status'].text=='live'
puts "#{flg.to_s} #{@title} (#{@cid}) "
return flg
end
end
end