Skip to content

Commit

Permalink
feat(route): douyu room api update with fallback (DIYgod#15401)
Browse files Browse the repository at this point in the history
* feat(route): douyu room api update with fallback

* style: auto format

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ChaosTong and github-actions[bot] authored Apr 29, 2024
1 parent cc044ac commit e54a5f1
Showing 1 changed file with 58 additions and 18 deletions.
76 changes: 58 additions & 18 deletions lib/routes/douyu/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,77 @@ export const route: Route = {
},
],
name: '直播间开播',
maintainers: ['DIYgod'],
maintainers: ['DIYgod', 'ChaosTong'],
handler,
};

async function handler(ctx) {
const id = ctx.req.param('id');

const response = await got({
method: 'get',
url: `http://open.douyucdn.cn/api/RoomApi/room/${id}`,
headers: {
Referer: `https://www.douyu.com/${id}`,
},
});
let data;
let item;
let room_thumb;
try {
const response = await got({
method: 'get',
url: `https://www.douyu.com/betard/${id}`,
});

const data = response.data.data;
if (!response.data.room) {
throw new Error('Invalid response');
}

let item;
if (data.online !== 0) {
item = [
{
title: `开播: ${data.room_name}`,
pubDate: new Date(data.start_time).toUTCString(),
guid: data.start_time,
link: `https://www.douyu.com/${id}`,
data = response.data.room;
room_thumb = data.room_pic;

if (data.show_status === 1) {
item =
data.videoLoop === 1
? [
{
title: `视频轮播: ${data.room_name}`,
pubDate: new Date(data.show_time * 1000).toUTCString(),
guid: data.show_time,
link: `https://www.douyu.com/${id}`,
},
]
: [
{
title: `开播: ${data.room_name}`,
pubDate: new Date(data.show_time * 1000).toUTCString(),
guid: data.show_time,
link: `https://www.douyu.com/${id}`,
},
];
}
// make a fallback to the old api
} catch {
const response = await got({
method: 'get',
url: `http://open.douyucdn.cn/api/RoomApi/room/${id}`,
headers: {
Referer: `https://www.douyu.com/${id}`,
},
];
});

data = response.data.data;
room_thumb = data.room_thumb;

if (data.online !== 0) {
item = [
{
title: `开播: ${data.room_name}`,
pubDate: new Date(data.start_time).toUTCString(),
guid: data.start_time,
link: `https://www.douyu.com/${id}`,
},
];
}
}

return {
title: `${data.owner_name}的斗鱼直播间`,
image: room_thumb,
link: `https://www.douyu.com/${id}`,
item,
allowEmpty: true,
Expand Down

0 comments on commit e54a5f1

Please sign in to comment.