Skip to content

Commit

Permalink
fix(route): fix twitch (#14238)
Browse files Browse the repository at this point in the history
* fix(route): fix twitch

* fix docs
  • Loading branch information
JimenezLi authored Jan 14, 2024
1 parent 418e68a commit 1a49f93
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/v2/twitch/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ module.exports = async (ctx) => {

const displayName = channelShellData.userOrError.displayName;

const out = streamScheduleData.user.channel.schedule.segments.map((item) => ({
// schedule segments may be null
const out = streamScheduleData.user.channel.schedule.segments?.map((item) => ({
title: item.title,
guid: item.id,
link: `https://www.twitch.tv/${login}`,
Expand Down
21 changes: 14 additions & 7 deletions lib/v2/twitch/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ const { parseDate } = require('@/utils/parse-date');
// https://github.com/streamlink/streamlink/blob/master/src/streamlink/plugins/twitch.py#L286
const TWITCH_CLIENT_ID = 'kimne78kx3ncx6brgo4mv6wki5h1ko';

const FILTER_CURSOR_MAP = {
archive: 0,
highlights: 1,
all: 2,
const FILTER_NODE_TYPE_MAP = {
archive: 'LATEST_BROADCASTS',
highlights: 'LATEST_NON_BROADCASTS',
all: 'ALL_VIDEOS',
};

module.exports = async (ctx) => {
const { login, filter = 'all' } = ctx.params;
const login = ctx.params.login;
const filter = ctx.params.filter?.toLowerCase() || 'all';
if (!FILTER_NODE_TYPE_MAP[filter]) {
throw Error(`Unsupported filter type "${filter}", please choose from { ${Object.keys(FILTER_NODE_TYPE_MAP).join(', ')} }`);
}

const response = await got({
method: 'post',
Expand Down Expand Up @@ -45,15 +49,18 @@ module.exports = async (ctx) => {

const displayName = channelVideoShelvesQueryData.user.displayName;

const videoShelvesEdge = channelVideoShelvesQueryData.user.videoShelves.edges[FILTER_CURSOR_MAP[filter] || FILTER_CURSOR_MAP.all];
const videoShelvesEdge = channelVideoShelvesQueryData.user.videoShelves.edges.find((edge) => edge.node.type === FILTER_NODE_TYPE_MAP[filter]);
if (!videoShelvesEdge) {
throw Error(`No video under filter type "${filter}"`);
}

const out = videoShelvesEdge.node.items.map((item) => ({
title: item.title,
link: `https://www.twitch.tv/videos/${item.id}`,
author: displayName,
pubDate: parseDate(item.publishedAt),
description: `<img style="max-width: 100%;" src="${item.previewThumbnailURL}"><br/><img style="max-width: 100%;" src="${item.animatedPreviewURL}">`,
category: [item.game.displayName],
category: item.game && [item.game.displayName], // item.game may be null
}));

ctx.state.data = {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/routes/live.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

### Channel Video {#twitch-channel-video}

<Route author="hoilc" path="/video/:login/:filter?" example="/twitch/video/riotgames/highlights" paramsDesc={['Twitch username', 'Video type, Default to all']} radar="1" />
<Route author="hoilc" path="/twitch/video/:login/:filter?" example="/twitch/video/riotgames/highlights" paramsDesc={['Twitch username', 'Video type, Default to all']} radar="1" />

| archive | highlights | all |
| ----------------- | ----------------------------- | ---------- |
Expand Down

0 comments on commit 1a49f93

Please sign in to comment.