Skip to content

Commit

Permalink
完成房间信息和头像接口
Browse files Browse the repository at this point in the history
  • Loading branch information
xfgryujk committed Sep 13, 2020
1 parent 6a64f3c commit 6e2f3b1
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 9 deletions.
91 changes: 91 additions & 0 deletions api/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import aiohttp
import tornado.websocket

import api.base
import blivedm.blivedm as blivedm
import config
import models.avatar
Expand Down Expand Up @@ -512,3 +513,93 @@ async def send_test_message(self):
gift_data['totalCoin'] = 1245000
gift_data['giftName'] = '小电视飞船'
self.send_message(Command.ADD_GIFT, gift_data)


# noinspection PyAbstractClass
class RoomInfoHandler(api.base.ApiHandler):
_host_server_list_cache = [
{'host': "broadcastlv.chat.bilibili.com", 'port': 2243, 'wss_port': 443, 'ws_port': 2244}
]

async def get(self):
room_id = int(self.get_query_argument('roomId'))
room_id, owner_uid = await self._get_room_info(room_id)
host_server_list = await self._get_server_host_list(room_id)
if owner_uid == 0:
# 缓存3分钟
self.set_header('Cache-Control', 'private, max-age=180')
else:
# 缓存1天
self.set_header('Cache-Control', 'private, max-age=86400')
self.write({
'roomId': room_id,
'ownerUid': owner_uid,
'hostServerList': host_server_list
})

@staticmethod
async def _get_room_info(room_id):
try:
async with _http_session.get(blivedm.ROOM_INIT_URL, params={'room_id': room_id}
) as res:
if res.status != 200:
logger.warning('room %d _get_room_info failed: %d %s', room_id,
res.status, res.reason)
return room_id, 0
data = await res.json()
except aiohttp.ClientConnectionError:
logger.exception('room %d _get_room_info failed', room_id)
return room_id, 0

if data['code'] != 0:
logger.warning('room %d _get_room_info failed: %s', room_id, data['msg'])
return room_id, 0

room_info = data['data']['room_info']
return room_info['room_id'], room_info['uid']

@classmethod
async def _get_server_host_list(cls, _room_id):
return cls._host_server_list_cache

# 连接其他host必须要key
# try:
# async with _http_session.get(blivedm.DANMAKU_SERVER_CONF_URL, params={'id': room_id, 'type': 0}
# ) as res:
# if res.status != 200:
# logger.warning('room %d _get_server_host_list failed: %d %s', room_id,
# res.status, res.reason)
# return cls._host_server_list_cache
# data = await res.json()
# except aiohttp.ClientConnectionError:
# logger.exception('room %d _get_server_host_list failed', room_id)
# return cls._host_server_list_cache
#
# if data['code'] != 0:
# logger.warning('room %d _get_server_host_list failed: %s', room_id, data['msg'])
# return cls._host_server_list_cache
#
# host_server_list = data['data']['host_list']
# if not host_server_list:
# logger.warning('room %d _get_server_host_list failed: host_server_list is empty')
# return cls._host_server_list_cache
#
# cls._host_server_list_cache = host_server_list
# return host_server_list


# noinspection PyAbstractClass
class AvatarHandler(api.base.ApiHandler):
async def get(self):
uid = int(self.get_query_argument('uid'))
avatar_url = await models.avatar.get_avatar_url_or_none(uid)
if avatar_url is None:
avatar_url = models.avatar.DEFAULT_AVATAR_URL
# 缓存3分钟
self.set_header('Cache-Control', 'private, max-age=180')
else:
# 缓存1天
self.set_header('Cache-Control', 'private, max-age=86400')
self.write({
'avatarUrl': avatar_url
})
1 change: 1 addition & 0 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import update


# noinspection PyAbstractClass
class MainHandler(tornado.web.StaticFileHandler):
"""为了使用Vue Router的history模式,把不存在的文件请求转发到index.html"""
async def get(self, path, include_body=True):
Expand Down
18 changes: 16 additions & 2 deletions frontend/src/api/chat/ChatClientDirect.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import axios from 'axios'
import {inflate} from 'pako'

import {getUuid4Hex} from '@/utils'
import * as avatar from './avatar'

Expand Down Expand Up @@ -68,7 +70,19 @@ export default class ChatClientDirect {
}

async initRoom () {
// TODO 请求后端
let res
try {
res = (await axios.get('/api/room_info', {params: {
roomId: this.roomId
}})).data
} catch {
return
}
this.roomId = res.roomId
this.roomOwnerUid = res.ownerUid
if (res.hostServerList.length !== 0) {
this.hostServerList = res.hostServerList
}
}

makePacket (data, operation) {
Expand Down Expand Up @@ -296,7 +310,7 @@ export default class ChatClientDirect {

let data = command.data
data = {
id: data.id,
id: data.id.toString(),
avatarUrl: avatar.processAvatarUrl(data.user_info.face),
timestamp: data.start_time,
authorName: data.user_info.uname,
Expand Down
15 changes: 12 additions & 3 deletions frontend/src/api/chat/avatar.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import axios from 'axios'

export const DEFAULT_AVATAR_URL = '//static.hdslb.com/images/member/noface.gif'

export function processAvatarUrl (avatarUrl) {
Expand All @@ -13,7 +15,14 @@ export function processAvatarUrl (avatarUrl) {
return avatarUrl
}

export async function getAvatarUrl () {
// TODO 请求后端
return DEFAULT_AVATAR_URL
export async function getAvatarUrl (uid) {
let res
try {
res = (await axios.get('/api/avatar_url', {params: {
uid: uid
}})).data
} catch {
return DEFAULT_AVATAR_URL
}
return res.avatarUrl
}
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
routes = [
(r'/api/server_info', api.main.ServerInfoHandler),
(r'/api/chat', api.chat.ChatHandler),
(r'/api/room_info', api.chat.RoomInfoHandler),
(r'/api/avatar_url', api.chat.AvatarHandler),

# TODO 兼容旧版,下版本移除
(r'/server_info', api.main.ServerInfoHandler),
Expand Down
12 changes: 8 additions & 4 deletions models/avatar.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,20 @@ def init():


async def get_avatar_url(user_id):
avatar_url = await get_avatar_url_or_none(user_id)
if avatar_url is None:
avatar_url = DEFAULT_AVATAR_URL
return avatar_url


async def get_avatar_url_or_none(user_id):
avatar_url = get_avatar_url_from_memory(user_id)
if avatar_url is not None:
return avatar_url
avatar_url = await get_avatar_url_from_database(user_id)
if avatar_url is not None:
return avatar_url
avatar_url = await get_avatar_url_from_web(user_id)
if avatar_url is not None:
return avatar_url
return DEFAULT_AVATAR_URL
return await get_avatar_url_from_web(user_id)


def get_avatar_url_from_memory(user_id):
Expand Down

0 comments on commit 6e2f3b1

Please sign in to comment.