Class: Video
- Inherits:
-
Object
- Object
- Video
- Defined in:
- lib/bilibili.rb
Instance Attribute Summary collapse
-
#aid ⇒ Object
视频aid.
-
#cid ⇒ Object
视频cid.
-
#commentURL ⇒ Object
弹幕URL.
-
#time ⇒ Object
视频发布时间.
-
#title ⇒ Object
视频标题.
-
#up ⇒ Object
up主的用户id.
-
#videos ⇒ Object
分p.
Class Method Summary collapse
- .av(aid = nil) ⇒ Object
-
.avToCID(aid) ⇒ Object
通过av号获取cid.
-
.commentWithAID(aid) ⇒ Object
视频弹幕(aid).
-
.commentWithCID(cid) ⇒ Object
视频弹幕(cid).
-
.downloadURLWithAID(aid, quality = 5) ⇒ Object
获取下载链接(aid).
-
.downloadURLWithCID(cid, quality = 5) ⇒ Object
获取下载链接(cid).
-
.shareCount(aid) ⇒ Object
视频分享量.
Instance Method Summary collapse
-
#comment ⇒ Object
视频弹幕.
-
#downloadURL(quality = 5) ⇒ Object
获取下载链接.
-
#refresh ⇒ Object
刷新视频信息.
-
#shareCount ⇒ Object
视频分享量.
Instance Attribute Details
#aid ⇒ Object
视频aid
133 134 135 |
# File 'lib/bilibili.rb', line 133 def aid @aid end |
#cid ⇒ Object
视频cid
132 133 134 |
# File 'lib/bilibili.rb', line 132 def cid @cid end |
#commentURL ⇒ Object
弹幕URL
134 135 136 |
# File 'lib/bilibili.rb', line 134 def commentURL @commentURL end |
#time ⇒ Object
视频发布时间
129 130 131 |
# File 'lib/bilibili.rb', line 129 def time @time end |
#title ⇒ Object
视频标题
128 129 130 |
# File 'lib/bilibili.rb', line 128 def title @title end |
#up ⇒ Object
up主的用户id
130 131 132 |
# File 'lib/bilibili.rb', line 130 def up @up end |
#videos ⇒ Object
分p
131 132 133 |
# File 'lib/bilibili.rb', line 131 def videos @videos end |
Class Method Details
.av(aid = nil) ⇒ Object
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 |
# File 'lib/bilibili.rb', line 136 def Video.av(aid = nil) response = Net::HTTP.new(K_BILIBILI_API_MAIN_URL).get('/video/av' + aid.to_s + '/') video = Video.new video.videos = Array.new video.aid = aid response.body.each_line { |line| t = /<div class="v-title"><h1 title="(.*?)">/.match(line); if t != nil video.title = t[1] end t = /<i>(\d{4}-\d{2}-\d{2} \d{2}:\d{2})<\/i><\/time>/.match(line); if t != nil video.time = t[1] end t = /<div class="usname"><a class="name" href="http:\/\/space.bilibili.com\/(\d+)"/.match(line); if t != nil video.up = t[1] end t = /<script type='text\/javascript'>EmbedPlayer\('player', "http:\/\/static.hdslb.com\/play.swf", "cid=(\d+)(?:.*?)"/.match(line); if t != nil video.cid = t[1] video.commentURL = K_BILIBILI_COMMENT_HOST + video.cid + '.xml' end t = /<option value='(.*?)'>(.*?)<\/option>/.match(line); if t != nil sp = Hash.new sp['URL'] = 'http://www.bilibili.com' + t[1] sp['title'] = t[2] video.videos.push(sp) end } return video end |
.avToCID(aid) ⇒ Object
通过av号获取cid
186 187 188 189 190 191 192 193 194 |
# File 'lib/bilibili.rb', line 186 def Video.avToCID(aid) response = Net::HTTP.new(K_BILIBILI_API_MAIN_URL).get('/video/av' + aid.to_s + '/') response.body.each_line { |line| t = /<script type='text\/javascript'>EmbedPlayer\('player', "http:\/\/static.hdslb.com\/play.swf", "cid=(\d+)(?:.*?)"/.match(line); if t != nil return t[1] end } end |
.commentWithAID(aid) ⇒ Object
视频弹幕(aid)
197 198 199 |
# File 'lib/bilibili.rb', line 197 def Video.commentWithAID(aid) Video.commentWithCID(Video.avToCID(aid)) end |
.commentWithCID(cid) ⇒ Object
视频弹幕(cid)
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 |
# File 'lib/bilibili.rb', line 202 def Video.commentWithCID(cid) uri = URI(K_BILIBILI_COMMENT_HOST + cid.to_s + '.xml') req = Net::HTTP::Get.new(uri) req['Accept-Encoding'] = 'deflate' res = Net::HTTP.start(uri.hostname, uri.port) {|http| http.request(req) } comments = Array.new Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(res.body).split("\n").each do |line| t = /<d p="(.*?),(.*?),(.*?),(.*?),(.*?),(.*?),(.*?),(.*?)">(.*)<\/d>/.match(line) if t != nil comment = Hash.new comment['time'] = t[1] comment['mode'] = t[2] comment['fontsize'] = t[3] comment['fontcolor'] = t[4] comment['timestamp'] = t[5] comment['pool'] = t[6] comment['sender'] = t[7] comment['rowID'] = t[8] comment['content'] = t[9] comments.push(comment) end end comments end |
.downloadURLWithAID(aid, quality = 5) ⇒ Object
获取下载链接(aid)
230 231 232 |
# File 'lib/bilibili.rb', line 230 def Video.downloadURLWithAID(aid, quality = 5) Video.downloadURLWithCID(Video.avToCID(aid), quality) end |
.downloadURLWithCID(cid, quality = 5) ⇒ Object
获取下载链接(cid)
235 236 237 238 |
# File 'lib/bilibili.rb', line 235 def Video.downloadURLWithCID(cid, quality = 5) response = Net::HTTP.new(K_BILIBILI_API_INTERFACE_URL).get(K_BILIBILI_API_VIDEO_DOWNLOAD + quality.to_s + '&cid=' + cid.to_s) JSON.parse(response.body) end |
.shareCount(aid) ⇒ Object
视频分享量
177 178 179 180 181 182 183 |
# File 'lib/bilibili.rb', line 177 def Video.shareCount(aid) response = Net::HTTP.new(K_BILIBILI_API_URL).get(K_BILIBILI_API_VIDEO_SHARE + aid.to_s) result = JSON.parse(response.body) if result['code'] == 0 return result['data'] end end |
Instance Method Details
#comment ⇒ Object
视频弹幕
241 242 243 |
# File 'lib/bilibili.rb', line 241 def comment Video.commentWithCID(@cid) end |
#downloadURL(quality = 5) ⇒ Object
获取下载链接
260 261 262 |
# File 'lib/bilibili.rb', line 260 def downloadURL(quality = 5) Video.downloadURLWithCID(@cid, quality) end |
#refresh ⇒ Object
刷新视频信息
255 256 257 |
# File 'lib/bilibili.rb', line 255 def refresh return Video.av(@aid) end |
#shareCount ⇒ Object
视频分享量
246 247 248 249 250 251 252 |
# File 'lib/bilibili.rb', line 246 def shareCount response = Net::HTTP.new(K_BILIBILI_API_URL).get(K_BILIBILI_API_VIDEO_SHARE + @aid.to_s) result = JSON.parse(response.body) if result['code'] == 0 return result['data'] end end |