Skip to content

Commit

Permalink
fix(gcores): broken links of articles and news (DIYgod#18276)
Browse files Browse the repository at this point in the history
* fix(gcores): broken links of articles and news

* fix typo

---------
  • Loading branch information
nczitzk authored Feb 10, 2025
1 parent 50e9444 commit 727b834
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
4 changes: 4 additions & 0 deletions lib/routes/gcores/articles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export const handler = async (ctx: Context): Promise<Data> => {
const apiUrl: string = new URL(`gapi/v1/articles`, baseUrl).href;

const query = {
'page[limit]': limit,
sort: '-published-at',
include: 'category,user,media',
'filter[list-all]': 1,
'filter[is-news]': 0,
};

Expand Down
4 changes: 4 additions & 0 deletions lib/routes/gcores/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export const handler = async (ctx: Context): Promise<Data> => {
const apiUrl: string = new URL('gapi/v1/articles', baseUrl).href;

const query = {
'page[limit]': limit,
sort: '-published-at',
include: 'category,user,media',
'filter[list-all]': 1,
'filter[is-news]': 1,
};

Expand Down
38 changes: 21 additions & 17 deletions lib/routes/gcores/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,45 @@ const baseUrl: string = 'https://www.gcores.com';
const imageBaseUrl: string = 'https://image.gcores.com';
const audioBaseUrl: string = 'https://alioss.gcores.com';

const baseQuery = {
sort: '-published-at',
include: 'category,user,media',
'filter[list-all]': 1,
'filter[is-news]': 1,
};
const types: Set<string> = new Set(['radios', 'articles', 'news', 'videos', 'talks']);

const processItems = async (limit: number, query: any, apiUrl: string, targetUrl: string): Promise<Data> => {
const response = await ofetch(apiUrl, {
query: {
...baseQuery,
query,
query: query ?? {
'page[limit]': limit,
sort: '-published-at',
include: 'category,user,media',
'filter[list-all]': 1,
},
});

const included = response.included;

const targetResponse = await ofetch(targetUrl);
const $: CheerioAPI = load(targetResponse);
const language = $('html').attr('lang') ?? 'zh-CN';

const included = response.included;
const data = [...response.data, ...included].filter((item) => types.has(item.type));

let items: DataItem[] = [];

items = response.data?.slice(0, limit).map((item): DataItem => {
items = data?.slice(0, limit).map((item): DataItem => {
const attributes = item.attributes;
const relationships = item.relationships;

const title: string = attributes.title;
const pubDate: number | string = attributes['published-at'];
const linkUrl: string | undefined = `${item.type}/${item.id}`;

const categoryObj = relationships?.category?.data;
const categories: string[] = categoryObj ? [included.find((i) => i.type === categoryObj.type && i.id === categoryObj.id)?.attributes?.name].filter(Boolean) : [];
const categoryObjs = [relationships?.category?.data, relationships?.tag?.data, relationships?.topic?.data].filter(Boolean);
const categories: string[] = categoryObjs
.map((obj) => {
const attributes = included.find((i) => i.type === obj.type && i.id === obj.id)?.attributes;
return attributes?.name ?? attributes?.title;
})
.filter(Boolean);

const authorObj = relationships?.user?.data;
const authorIncluded = included.find((i) => i.type === authorObj.type && i.id === authorObj.id);
const authorIncluded = authorObj ? included.find((i) => i.type === authorObj.type && i.id === authorObj.id) : undefined;
const authors: DataItem['author'] = authorIncluded
? [
{
Expand All @@ -69,7 +72,7 @@ const processItems = async (limit: number, query: any, apiUrl: string, targetUrl
let processedItem: DataItem = {
title,
pubDate: pubDate ? parseDate(pubDate) : undefined,
link: linkUrl,
link: new URL(linkUrl, baseUrl).href,
category: categories,
author: authors,
guid,
Expand All @@ -94,7 +97,7 @@ const processItems = async (limit: number, query: any, apiUrl: string, targetUrl
enclosureType = `audio/${enclosureUrl?.split(/\./).pop()}`;
} else if (mediaAttrs['original-src']) {
enclosureUrl = mediaAttrs['original-src'];
enclosureType = 'video/mpeg';
enclosureType = `video/${enclosureUrl?.split(/\?/).pop() ? (/^id=\d+$/.test(enclosureUrl?.split(/\?/).pop() as string) ? 'taptap' : enclosureUrl?.split(/\./).pop()) : ''}`;
}
}

Expand Down Expand Up @@ -145,6 +148,7 @@ const processItems = async (limit: number, query: any, apiUrl: string, targetUrl

processedItem = {
...processedItem,
title: title ?? $(description).text(),
description,
content: {
html: description,
Expand Down

0 comments on commit 727b834

Please sign in to comment.