diff --git a/docs/en/other.md b/docs/en/other.md index 17f2d717c..1b238ff73 100644 --- a/docs/en/other.md +++ b/docs/en/other.md @@ -215,7 +215,19 @@ Refer to [the list of supported currencies](https://transferwise.com/tools/excha -## 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) + + + +## TSSstatus (iOS downgrade channel) ### Status @@ -254,3 +266,4 @@ Type ### User + diff --git a/lib/v2/trending/allTrending.js b/lib/v2/trending/allTrending.js index f0bb46e18..c9556af13 100644 --- a/lib/v2/trending/allTrending.js +++ b/lib/v2/trending/allTrending.js @@ -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)) diff --git a/lib/v2/trending/templates/content.art b/lib/v2/trending/templates/content.art index 443d76b85..047463438 100644 --- a/lib/v2/trending/templates/content.art +++ b/lib/v2/trending/templates/content.art @@ -7,6 +7,7 @@ {{each channel.data item}}
  • {{item.title}} + {{if item.new}}🆕{{/if}}
  • {{/each}}