Finally we play around with JavaScript on Jupyter: the
Executing JavaScript commands
To execute JavaScript in jupyter is simple, directly using the Javascript command to execute.
from IPython.display import Javascript
Javascript("alert('A piece of JavaScript was executed')")
You can see that after execution, the prompt window pops up.
Watching live video on Jupyter
What about the following, we are going to implement watching live B-stream directly on Jupyter. To do both studying and learning (for fun).
The final result to be achieved.
Get a list of live room ids for a given partition
This time we are looking at the live area of
Then we have the list of live ids for that partition.
import requests
from lxml import etree
def get_room_ids(room_type="learning"):
urls = {"study": "https://live.bilibili.com/p/eden/area-tags?visit_id=9ynmsmaiie80&areaId=377&parentAreaId=11& quot;,
"faceArea": "https://live.bilibili.com/show/yzly?visit_id=3g19a7bxnb60"}
res = requests.get(urls[room_type])
res.encoding = res.apparent_encoding
html = etree.HTML(res.text)
room_ids = {}
for a in html.xpath("//ul/li/a"):
url = a.xpath(". /@href")[0]
tags = a.xpath(". //text()")
room_ids[tags[1]] = url[1:url.find(""? ")]
return room_ids
room_ids = get_room_ids()
room_ids
{'Uncle who teaches modeling': '22590752',
'
Get the source address of the live feed for the specified live room
After some packet capture analysis, we know that the interface address of the live signal source is
https://api.live.bilibili.com/xlive/web-room/v1/playUrl/playUrl
To get the address of the video source, simply pass 3 parameters to the above connection, which are
cid : cid serial number
qn : Quality of the video played
platform : The format in which the video is played
import requests
def get_live_url(cid, platform='h5'):
playUrl = 'https://api.live.bilibili.com/xlive/web-room/v1/playUrl/playUrl'
params = {
'cid': cid, # cid serial number
'qn': 10000, # the quality of the played video
'platform': platform, # the format of the video to be played
'ptype': 16
}
response = requests.get(playUrl, params=params).json()
text = response['data']['durl']
url = text[-1]['url']
return url
可以传入参数web,可以获取flv的流媒体格式:
def get_real_url(url):
r = requests.get(url, headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"})
for line in r.text.splitlines():
if line.startswith("http"):
url = line
break
return url
real_url = get_real_url(url)
print(real_url)
https://236809363.cloudvdn.com/a.m3u8?cdn=cn-gotcha03&domain=d1--cn-gotcha103.bilivideo.com&expires=1624885067&len=0&oi=1947748628&order=4&player=oMgAAAUu2rUWvYwW&pt=h5&ptype=0&qn=10000&secondToken=secondToken%3A3pgIk9iJHnT4GCnoFdgLimMFp-I&sign=dd58d00836aa06776e6f2b0d933a3aee&sigparams=cdn%2Cexpires%2Clen%2Coi%2Cpt%2Cqn%2Ctrid&sk=87af7c4a512d23a&sl=1&src=8&streamid=live-qn%3Alive-qn%2Flive_8086004_8646306&trid=1003e246434329364ba685ff4d5ecff153a0&v3=1
import requests
def get_live_url(cid, platform='h5'):
playUrl = 'https://api.live.bilibili.com/xlive/web-room/v1/playUrl/playUrl'
params = {
'cid': cid, # cid serial number
'qn': 10000, # the quality of the played video
'platform': platform, # the format of the video to be played
'ptype': 16
}
response = requests.get(playUrl, params=params).json()
text = response['data']['durl']
url = text[-1]['url']
return url