fix(route/trending): correctly label new items in trending-all-in-one & add English docs (#10506)
* enhance(route): label new items in trending-all-in-one * docs(route): English docs for trending-all-in-one * fix(route/trending): error when data is empty * docs: fix tag fix: use colon as separator for cache key
This commit is contained in:
+14
-1
@@ -215,7 +215,19 @@ Refer to [the list of supported currencies](https://transferwise.com/tools/excha
|
||||
|
||||
</RouteEn>
|
||||
|
||||
## TSSstatus(iOS downgrade channel)
|
||||
## Trending Search Keyword Aggregator
|
||||
|
||||
### Aggregated Keyword Tracker
|
||||
|
||||
Track entries containing specific keywords on major social media platforms.
|
||||
|
||||
Current listings: *Weibo Search*、*Toutiao Search*、*Zhihu Search*、*Zhihu Videos*、*Zhihu Topics*。
|
||||
|
||||
Data Source: [trending-in-one](https://github.com/huqi-pr/trending-in-one)
|
||||
|
||||
<RouteEn author="Jkker" example="/trending/taiwan/3" path="/trending/:keywords/:numberOfDays?" radar="1" :paramsDesc="['List of keywords separated by commas', 'Number of previous days (defaults to 3)']"/>
|
||||
|
||||
## TSSstatus (iOS downgrade channel)
|
||||
|
||||
### Status
|
||||
|
||||
@@ -254,3 +266,4 @@ Type
|
||||
### User
|
||||
|
||||
<RouteEn author="SettingDust Halcao" example="/uraaka-joshi/_rrwq" path="/uraaka-joshi/:id" :paramsDesc="['User ID']" radar="1" rssbud="1" puppeteer="1"/>
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ const config = require('@/config').value;
|
||||
const md5 = require('@/utils/md5');
|
||||
|
||||
// Constants
|
||||
const CACHE_KEY = 'trending-all-in-one';
|
||||
const DATA_REPO_BASE_URL = 'https://raw.githubusercontent.com/huqi-pr/trending-in-one/master/raw';
|
||||
const DATE_FORMAT = 'YYYY-MM-DD';
|
||||
// TODO: support custom data repo urls
|
||||
@@ -35,7 +36,6 @@ const CHANNELS = {
|
||||
};
|
||||
|
||||
// Helper Functions
|
||||
// TODO: integrate into CHANNELS
|
||||
const processRawDataByChannel = {
|
||||
'toutiao-search': ({ word: title }) => ({
|
||||
// 源 url 存在 encoding 问题,暂时不使用
|
||||
@@ -61,38 +61,63 @@ const processRawDataByChannel = {
|
||||
|
||||
const hasKeyword = (str, keywordList) => keywordList.some((keyword) => str.includes(keyword));
|
||||
const toShanghaiTimezone = (date) => dayjs.tz(date, 'Asia/Shanghai');
|
||||
const processRawData = (channel) => (res) => res.map((item) => processRawDataByChannel[channel](item));
|
||||
const filterKeyword = (keywordList) => (res) => res.filter(({ title }) => hasKeyword(title, keywordList));
|
||||
|
||||
// Data Fetcher
|
||||
// TODO: support channel selection
|
||||
const fetchAllData = async (keywordList = [], dateList = [], cache) => {
|
||||
const cachedGetData = (url, dataMappingFn) =>
|
||||
cache.tryGet(
|
||||
url,
|
||||
() =>
|
||||
got(url)
|
||||
.json()
|
||||
// Normalize data to { title, url } format
|
||||
.then((res) => res.map((item) => dataMappingFn(item))),
|
||||
config.cache.contentExpire,
|
||||
false
|
||||
);
|
||||
const cachedGetData = (url) => cache.tryGet(url, () => got(url).json(), config.cache.contentExpire, false);
|
||||
|
||||
const data = await Promise.all(
|
||||
let data = await Promise.all(
|
||||
dateList.map(async (dateTime) => ({
|
||||
dateTime,
|
||||
data: await Promise.all(
|
||||
Object.keys(CHANNELS).map(async (channel) => ({
|
||||
name: CHANNELS[channel].name,
|
||||
data: await cachedGetData(`${DATA_REPO_BASE_URL}/${channel}/${dateTime.format(DATE_FORMAT)}.json`, processRawDataByChannel[channel]).then((res) => res.filter(({ title }) => hasKeyword(title, keywordList))),
|
||||
data: await cachedGetData(`${DATA_REPO_BASE_URL}/${channel}/${dateTime.format(DATE_FORMAT)}.json`)
|
||||
.then(processRawData(channel))
|
||||
.then(filterKeyword(keywordList)),
|
||||
}))
|
||||
),
|
||||
}))
|
||||
);
|
||||
|
||||
for (const i of data) {
|
||||
i.count = i.data.reduce((acc, { data }) => acc + data.length, 0);
|
||||
}
|
||||
|
||||
return data.filter(({ count }) => count > 0);
|
||||
data = data.filter(({ count }) => count > 0);
|
||||
|
||||
if (data.length === 0) {
|
||||
return data;
|
||||
}
|
||||
|
||||
const prev = cache.get(CACHE_KEY + ':latest-items');
|
||||
|
||||
const latest = data[0];
|
||||
latest.newItemCount = 0;
|
||||
if (latest.count > 0 && prev) {
|
||||
// Mark new items in latest
|
||||
for (const channel in latest.data) {
|
||||
for (const i of latest.data[channel].data) {
|
||||
i.new = !(i.url in prev);
|
||||
latest.newItemCount += i.new ? 1 : 0;
|
||||
}
|
||||
}
|
||||
// Save latest data to cache
|
||||
const cachedItems = latest.data.reduce((acc, { data: channel }) => {
|
||||
for (const item of channel) {
|
||||
acc[item.url] = true;
|
||||
}
|
||||
return acc;
|
||||
}, {});
|
||||
cache.set(CACHE_KEY + ':latest-items', cachedItems, config.cache.contentExpire);
|
||||
|
||||
latest.count = Object.keys(cachedItems).length;
|
||||
}
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
// Generate Feed Items
|
||||
@@ -108,11 +133,13 @@ const searchLinkUrls = (keyword) => [
|
||||
|
||||
const searchLinkNames = ['热榜', '百度', '谷歌', '知乎', '微博', '抖音', '头条'];
|
||||
|
||||
const createItem = ({ dateTime, data, count }, keywords, isToday) => {
|
||||
const createItem = ({ dateTime, data, count, newItemCount }, keywords, isToday) => {
|
||||
const EOD = dateTime.endOf('day');
|
||||
const pubDate = isToday ? new Date() : EOD.toDate();
|
||||
const countStr = isToday && newItemCount ? newItemCount + '🆕' : count;
|
||||
|
||||
return {
|
||||
title: `${keywords.join(', ')} | ${dateTime.format(DATE_FORMAT)} 热点追踪 (${count})`,
|
||||
title: `${keywords.join(', ')} | ${dateTime.format(DATE_FORMAT)} 热点追踪 (${countStr})`,
|
||||
author: 'Trending All In One',
|
||||
pubDate,
|
||||
description: art(path.join(__dirname, 'templates/content.art'), {
|
||||
@@ -147,7 +174,7 @@ module.exports = async (ctx) => {
|
||||
.map((keyword) => keyword.trim());
|
||||
const keywordStr = keywordList.join(', ');
|
||||
|
||||
const data = await fetchAllData(keywordList, dateList, ctx.cache);
|
||||
const data = await fetchAllData(keywordList, dateList, ctx.cache).catch(() => []);
|
||||
const item =
|
||||
data.length > 0
|
||||
? data.map((i, index) => createItem(i, keywordList, index === 0))
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
{{each channel.data item}}
|
||||
<li>
|
||||
<a href="{{item.url}}" rel="noopener noreferrer" target="_blank">{{item.title}}</a>
|
||||
{{if item.new}}🆕{{/if}}
|
||||
</li>
|
||||
{{/each}}
|
||||
</ul>
|
||||
|
||||
Reference in New Issue
Block a user