diff --git a/lib/routes/voronoiapp/author.ts b/lib/routes/voronoiapp/author.ts new file mode 100644 index 000000000..9731aa638 --- /dev/null +++ b/lib/routes/voronoiapp/author.ts @@ -0,0 +1,41 @@ +import type { Data, Route } from '@/types'; +import { CommonDataProperties, CommonRouteProperties, getPostItems } from './common'; +import ofetch from '@/utils/ofetch'; +import cache from '@/utils/cache'; + +export const route: Route = { + ...CommonRouteProperties, + name: 'Author Posts', + path: '/author/:username', + radar: [ + { + source: ['www.voronoiapp.com/author/:username'], + target: '/author/:username', + }, + ], + example: '/voronoiapp/author/visualcapitalist', + parameters: { + username: 'The username of the author', + }, + handler: async (ctx) => { + const { username } = ctx.req.param(); + const uid = await getUidFromUsername(username); + const items = await getPostItems({ order: 'DESC', author: uid }); + return { + ...CommonDataProperties, + title: `Voronoi Posts by ${username}`, + link: `https://www.voronoiapp.com/author/${username}`, + item: items, + } as Data; + }, +}; +async function getUidFromUsername(username: string): Promise { + return (await cache.tryGet(`voronoiapp-author-${username}`, async () => { + const response = await ofetch<'text'>(`https://www.voronoiapp.com/author/${username}`); + const match = response.match(/\\"uid\\":\\"([\w-]+)\\"/); + if (!match) { + throw new Error(`No UID found for username: ${username}`); + } + return match[1]; + })) as string; +} diff --git a/lib/routes/voronoiapp/common.ts b/lib/routes/voronoiapp/common.ts new file mode 100644 index 000000000..9cdd56f48 --- /dev/null +++ b/lib/routes/voronoiapp/common.ts @@ -0,0 +1,247 @@ +import { ViewType, type Data, type DataItem, type Route } from '@/types'; +import { parseDate } from '@/utils/parse-date'; +import { ofetch } from 'ofetch'; +import type { Post } from './types'; + +export async function getPostItems(params: { + feed?: string; + search?: string; + swimlane?: string; + tab?: string; + time_range?: string; + category?: string; + order?: string; + author?: string; + limit?: number; + offset?: number; +}): Promise { + const baseUrl = 'https://9oyi4rk426.execute-api.ca-central-1.amazonaws.com/production/post'; + const url = new URL(baseUrl); + const finalSearchParams = Object.assign( + { + limit: 20, + offset: 0, + }, + params + ); + if (finalSearchParams.time_range !== undefined) { + finalSearchParams.time_range = finalSearchParams.time_range.toUpperCase(); + if (!TimeRangeParam.options.some((option) => option.value === finalSearchParams.time_range)) { + throw new Error(`Invalid time range: ${finalSearchParams.time_range}`); + } + // The Voronoi API doesn't support "ALL" + if (finalSearchParams.time_range === 'ALL') { + finalSearchParams.time_range = undefined; + } + } + if (finalSearchParams.category !== undefined && finalSearchParams.category !== null) { + const category = finalSearchParams.category; + finalSearchParams.category = CategoryParam.options.find((option) => option.value.toLowerCase() === category.toLowerCase())?.value; + if (finalSearchParams.category === undefined) { + throw new Error(`Invalid category: ${finalSearchParams.category}`); + } + } + if (finalSearchParams.tab !== undefined && finalSearchParams.tab !== null) { + finalSearchParams.tab = finalSearchParams.tab.toUpperCase(); + if (!Object.values(TabMap).includes(finalSearchParams.tab)) { + throw new Error(`Invalid tab: ${finalSearchParams.tab}`); + } + } + for (const key in finalSearchParams) { + if (finalSearchParams[key] !== undefined && finalSearchParams[key] !== null) { + url.searchParams.set(key, finalSearchParams[key]); + } + } + const data = await ofetch(url.toString()); + const items: DataItem[] = data.map((post) => ({ + title: post.headline, + link: `https://www.voronoiapp.com/${post.category.split(' ').join('-').toLowerCase()}/${post.link}`, + pubDate: parseDate(post.published_at), + description: ` + ${post.description}`, + image: `https://cdn.voronoiapp.com/public/${post.webp_image}`, + author: post.author.first_name + ' ' + post.author.last_name, + updated: parseDate(post.updated_at), + category: [post.category], + enclosure_url: `https://cdn.voronoiapp.com/public/${post.dataset}`, + enclosure_type: 'text/csv', + enclosure_title: post.dataset, + upvotes: post.likes, + comments: post.commented, + })); + + return items; +} + +export const CategoryParam = { + description: 'The category of the post', + default: '', + options: [ + { + value: '', + label: 'All categories', + }, + { + value: 'Automotive', + label: 'Automotive Data Insights - Explore a range of automotive data visualizations showcasing trends, innovations, and market dynamics in the automotive industry.', + }, + { + value: 'Business', + label: 'Business Visualization Trends - Discover business visualizations covering market analysis, corporate strategies, and economic impacts across global industries.', + }, + { + value: 'Climate', + label: 'Climate Data Visualized - Delve into climate change data visualizations that detail weather patterns, environmental impacts, and sustainability efforts worldwide.', + }, + { + value: 'Demographics', + label: 'Demographic Visual Insights - Explore visual demographics data showcasing population trends, societal changes, and demographic analytics across regions.', + }, + { + value: 'Economy', + label: 'Economic Visualization Insights - View economic visualizations illustrating financial markets, economic policies, and global economic health.', + }, + { + value: 'Energy', + label: 'Energy Industry Visual Data - Discover the dynamics of global energy consumption, renewable sources, and energy market trends through vivid visualizations.', + }, + { + value: 'Entertainment', + label: 'Entertainment Industry Data - Explore data visualizations in the entertainment industry, covering everything from box office trends to streaming service analytics.', + }, + { + value: 'Geopolitics', + label: 'Geopolitical Data Visualized - Understand global geopolitical shifts and international relations through comprehensive geopolitical data visualizations.', + }, + { + value: 'Healthcare', + label: 'Healthcare Insights Visualized - Analyze healthcare data visualizations spanning disease trends, healthcare services, and public health policies.', + }, + { + value: 'Innovation', + label: 'Innovation in Data - Dive into innovation data visualizations highlighting technology advancements, R&D investments, and patent trends.', + }, + { + value: 'Maps', + label: 'Cartographic Visual Insights - Discover cartographic visualizations that map everything from socio-economic data to geographical phenomena.', + }, + { + value: 'Markets', + label: 'Market Trends Visualized - Visualize market trends, financial data, and economic forecasts through comprehensive market visualizations.', + }, + { + value: 'Money', + label: 'Financial Data Visualized - Dive into financial visualizations depicting currency trends, investment flows, and banking statistics.', + }, + { + value: 'Natural Resources', + label: 'Natural Resources Data - Explore visualizations of natural resources, detailing extraction, consumption, and conservation data.', + }, + { + value: 'Politics', + label: 'Political Visual Insights - Analyze political trends, election results, and legislative impacts through detailed political visualizations.', + }, + { + value: 'Public Opinion', + label: 'Public Opinion Trends - Discover visualizations of public opinion polls, social trends, and cultural shifts across different regions.', + }, + { + value: 'Real Estate', + label: 'Real Estate Market Insights - Explore real estate market trends, property values, and urban development through targeted data visualizations.', + }, + { + value: 'Sports', + label: 'Sports Data Insights - Analyze sports data visualizations that showcase performance statistics, team rankings, and sports economics.', + }, + { + value: 'Technology', + label: 'Technology Trends Visualized - Dive into technology visualizations highlighting industry trends, tech adoption rates, and innovation impacts.', + }, + { + value: 'Wealth', + label: 'Wealth Distribution Insights - Explore wealth distribution, financial health, and economic disparities through detailed visualizations.', + }, + { + value: 'Travel', + label: 'Travel Trends Visualized - Discover travel trends, tourism statistics, and destination analytics through engaging visualizations.', + }, + { + value: 'Nature', + label: 'Nature and Conservation Data - Delve into visualizations of ecological data, wildlife statistics, and conservation efforts around the globe.', + }, + { + value: 'Space', + label: 'Space Exploration Data - Explore the universe with space data visualizations covering planetary science, space missions, and astronomical discoveries.', + }, + { + value: 'Diagram', + label: 'Diagrammatic Data Insights - Understand complex data through diagrams that simplify information across various topics and industries.', + }, + { + value: 'Other', + label: "Diverse Data Visualizations - Explore a variety of data visualizations that don't neatly fit into any single category but offer unique insights.", + }, + ], +}; + +export const TimeRangeParam = { + description: 'Time range between which the posts are popular.', + default: 'MONTH', + options: [ + { + value: 'WEEK', + label: 'Last 7 days', + }, + { + value: 'MONTH', + label: 'Last 30 days', + }, + { + value: 'YEAR', + label: 'Last 12 months', + }, + { + value: 'ALL', + label: 'All time', + }, + ], +}; + +export const TabMap = { + 'most-popular': 'POPULAR', + 'most-discussed': 'DISCUSSED', + 'most-viewed': 'VIEWED', +}; + +export const TabParam = { + description: 'The tab to get the popular posts from.', + default: 'most-popular', + options: [ + { + value: 'most-popular', + label: 'Most Liked', + }, + { + value: 'most-discussed', + label: 'Most Discussed', + }, + { + value: 'most-viewed', + label: 'Most Viewed', + }, + ], +}; + +export const CommonRouteProperties: Pick = { + url: 'voronoiapp.com', + categories: ['picture'], + view: ViewType.Pictures, + maintainers: ['Cesaryuan'], +}; + +export const CommonDataProperties: Pick | { logo: string; icon: string } = { + logo: 'https://about.voronoiapp.com/wp-content/uploads/2023/07/voronoi-icon.png', + image: 'https://about.voronoiapp.com/wp-content/uploads/2023/07/voronoi-icon.png', + icon: 'https://about.voronoiapp.com/wp-content/uploads/2023/07/voronoi-icon.png', + allowEmpty: true, +}; diff --git a/lib/routes/voronoiapp/editors-pick.ts b/lib/routes/voronoiapp/editors-pick.ts new file mode 100644 index 000000000..1907f290f --- /dev/null +++ b/lib/routes/voronoiapp/editors-pick.ts @@ -0,0 +1,28 @@ +import type { Data, Route } from '@/types'; +import { getPostItems, CategoryParam, CommonRouteProperties, CommonDataProperties } from './common'; + +export const route: Route = { + ...CommonRouteProperties, + name: "Editor's Pick Posts", + path: '/editors-pick/:category?', + radar: [ + { + source: ['www.voronoiapp.com/posts/editors-pick'], + target: '/editors-pick', + }, + ], + example: '/voronoiapp/editors-pick', + parameters: { + category: CategoryParam, + }, + handler: async (ctx) => { + const { category = '' } = ctx.req.param(); + const items = await getPostItems({ swimlane: 'CURATED', category: category === '' ? undefined : category }); + return { + ...CommonDataProperties, + title: `Voronoi Editor's Pick Posts${category ? ` - ${category}` : ''}`, + link: 'https://www.voronoiapp.com/editors-pick', + item: items, + } as Data; + }, +}; diff --git a/lib/routes/voronoiapp/home.ts b/lib/routes/voronoiapp/home.ts new file mode 100644 index 000000000..3558e1e22 --- /dev/null +++ b/lib/routes/voronoiapp/home.ts @@ -0,0 +1,29 @@ +import type { Data, Route } from '@/types'; +import { getPostItems, CategoryParam, CommonRouteProperties, CommonDataProperties } from './common'; + +export const route: Route = { + ...CommonRouteProperties, + name: 'Home Posts', + path: '/home/:category?', + description: 'This is the home page of Voronoi App', + radar: [ + { + source: ['www.voronoiapp.com', 'www.voronoiapp.com/posts/voronoi'], + target: '/home', + }, + ], + example: '/voronoiapp/home', + parameters: { + category: CategoryParam, + }, + handler: async (ctx) => { + const { category = '' } = ctx.req.param(); + const items = await getPostItems({ feed: 'VORONOI', category: category === '' ? undefined : category }); + return { + ...CommonDataProperties, + title: `Voronoi Home Posts${category ? ` - ${category}` : ''}`, + link: 'https://www.voronoiapp.com', + item: items, + } as Data; + }, +}; diff --git a/lib/routes/voronoiapp/latest.ts b/lib/routes/voronoiapp/latest.ts new file mode 100644 index 000000000..2ef90fc0d --- /dev/null +++ b/lib/routes/voronoiapp/latest.ts @@ -0,0 +1,28 @@ +import type { Data, Route } from '@/types'; +import { getPostItems, CategoryParam, CommonRouteProperties, CommonDataProperties } from './common'; + +export const route: Route = { + ...CommonRouteProperties, + name: 'Latest Posts', + path: '/latest/:category?', + radar: [ + { + source: ['www.voronoiapp.com/posts/latest'], + target: '/latest', + }, + ], + example: '/voronoiapp/latest', + parameters: { + category: CategoryParam, + }, + handler: async (ctx) => { + const { category = '' } = ctx.req.param(); + const items = await getPostItems({ swimlane: 'LATEST', category: category === '' ? undefined : category }); + return { + ...CommonDataProperties, + title: `Voronoi Latest Posts${category ? ` - ${category}` : ''}`, + link: 'https://www.voronoiapp.com/latest', + item: items, + } as Data; + }, +}; diff --git a/lib/routes/voronoiapp/namespace.ts b/lib/routes/voronoiapp/namespace.ts new file mode 100644 index 000000000..eff11a4c3 --- /dev/null +++ b/lib/routes/voronoiapp/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'Voronoi', + url: 'voronoiapp.com', +}; diff --git a/lib/routes/voronoiapp/popular.ts b/lib/routes/voronoiapp/popular.ts new file mode 100644 index 000000000..ab99de9f2 --- /dev/null +++ b/lib/routes/voronoiapp/popular.ts @@ -0,0 +1,49 @@ +import type { Data, Route } from '@/types'; +import { CategoryParam, CommonDataProperties, CommonRouteProperties, getPostItems, TabMap, TabParam, TimeRangeParam } from './common'; + +export const route: Route = { + ...CommonRouteProperties, + name: 'Popular Posts', + path: '/popular/:tab?/:time_range?/:category?', + radar: [ + { + title: 'Most Liked Posts', + source: ['www.voronoiapp.com/posts/most-popular'], + target: '/popular/most-popular', + }, + { + title: 'Most Discussed Posts', + source: ['www.voronoiapp.com/posts/most-discussed'], + target: '/popular/most-discussed', + }, + { + title: 'Most Viewed Posts', + source: ['www.voronoiapp.com/posts/most-viewed'], + target: '/popular/most-viewed', + }, + ], + parameters: { + tab: TabParam, + time_range: TimeRangeParam, + category: CategoryParam, + }, + example: '/voronoiapp/popular/most-popular/MONTH', + handler: async (ctx) => { + const { tab = 'most-popular', time_range = 'MONTH', category = '' } = ctx.req.param(); + if (!TabMap[tab.toLowerCase()]) { + throw new Error(`Invalid tab: ${tab}`); + } + const items = await getPostItems({ + swimlane: 'POPULAR', + tab: TabMap[tab.toLowerCase()], + time_range: time_range === '' ? undefined : time_range.toUpperCase(), + category: category === '' ? undefined : category, + }); + return { + ...CommonDataProperties, + title: `Voronoi ${TabParam.options.find((option) => option.value === tab.toLowerCase())?.label} Posts in ${TimeRangeParam.options.find((option) => option.value === time_range.toUpperCase())?.label}${category ? ` - ${category}` : ''}`, + link: `https://www.voronoiapp.com/posts/${tab}`, + item: items, + } as Data; + }, +}; diff --git a/lib/routes/voronoiapp/search.ts b/lib/routes/voronoiapp/search.ts new file mode 100644 index 000000000..d04a99515 --- /dev/null +++ b/lib/routes/voronoiapp/search.ts @@ -0,0 +1,33 @@ +import type { Data, Route } from '@/types'; +import { CommonDataProperties, CommonRouteProperties, getPostItems } from './common'; + +export const route: Route = { + ...CommonRouteProperties, + name: 'Search Keyword Posts', + path: '/search/:keyword', + radar: [ + { + source: ['www.voronoiapp.com/explore'], + // 新版本中貌似不再支持 function 形式的 target + target: (_, url) => { + const parsedURL = new URL(url); + const keyword = parsedURL.searchParams.get('search'); + return `/voronoiapp/search/${keyword}`; + }, + }, + ], + example: '/voronoiapp/search/china', + parameters: { + keyword: 'The keyword to search for', + }, + handler: async (ctx) => { + const { keyword } = ctx.req.param(); + const items = await getPostItems({ search: keyword }); + return { + ...CommonDataProperties, + title: `Voronoi Posts for "${keyword}"`, + link: `https://www.voronoiapp.com/explore?search=${encodeURIComponent(keyword)}`, + item: items, + } as Data; + }, +}; diff --git a/lib/routes/voronoiapp/types.d.ts b/lib/routes/voronoiapp/types.d.ts new file mode 100644 index 000000000..e1453d6c9 --- /dev/null +++ b/lib/routes/voronoiapp/types.d.ts @@ -0,0 +1,78 @@ +export interface Social { + website?: string; + x?: string; + linkedin?: string; + instagram?: string; +} + +export interface Author { + first_name: string; + last_name: string; + preferred_username: string; + web_site: string; + email: string; + picture: string; + bio: string; + sub: string; + groups: string; + badge?: string; + rcv_mail: boolean; + rcv_push: boolean; + key?: string; + bookmarks?: string; + following?: string; + followers: number; + posts: number; + views: number; + wviews: number; + eviews: number; + imps: number; + wimps: number; + eimps: number; + social: Social; +} + +export interface Post { + pid: number; + uid: string; + completed: number; + step: number; + status: string; + curated: boolean; + headline: string; + link: string; + image: string; + webp_image: string; + preview_image: string; + cropper_data: string; + description: string; + category: string; + tags: string[]; + feeds?: string[]; + dataset: string; + dataset_settings: string; + sources: string[]; + note: string; + comments: number; + views: number; + wviews: number; + eviews: number; + imps: number; + wimps: number; + eimps: number; + commented: number; + shares: number; + bookmarks: number; + likes: number; + cools: number; + loves: number; + debatables: number; + insightfuls: number; + created_at: number; + updated_at: number; + author: Author; + subscribe_vc: boolean; + sponsored: boolean; + pinned: boolean; + published_at: number; +}