Last active
January 16, 2026 15:26
-
-
Save ipenywis/94e561a1a8c4735fc99a57394be4b284 to your computer and use it in GitHub Desktop.
Perigon - Trending Stories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Fetch Trending Stories from Perigon API | |
| * Tailored for UK market | |
| */ | |
| async function fetchTrendingStories(options = {}) { | |
| const { | |
| apiKey, | |
| country = 'gb', // UK-focused | |
| size = 75, // Number of stories | |
| page = 0, // Pagination | |
| category = null, // Optional: Politics, Business, Tech, etc. | |
| personName = null, // Optional: Filter by person | |
| companyName = null, // Optional: Filter by company | |
| } = options; | |
| // Calculate date range (default: last 24 hours) | |
| const hoursAgo = category ? 24 * 30 : 24; // 30 days for categories, 24h for trending | |
| const fromDate = new Date(Date.now() - hoursAgo * 60 * 60 * 1000).toISOString(); | |
| // Build API URL | |
| const url = new URL('https://api.perigon.io/v1/stories/all'); | |
| url.searchParams.set('apiKey', apiKey); | |
| url.searchParams.set('from', fromDate); | |
| url.searchParams.set('sortBy', 'count'); // Sort by popularity | |
| url.searchParams.set('size', size.toString()); | |
| url.searchParams.set('page', page.toString()); | |
| url.searchParams.set('showStoryPageInfo', 'true'); // Include images | |
| url.searchParams.set('expandArticles', 'true'); // Include article samples | |
| url.searchParams.set('minUniqueSources', '10'); // Quality threshold | |
| url.searchParams.set('minClusterSize', '5'); // Min articles per story | |
| url.searchParams.set('country', country); | |
| // Add optional filters | |
| if (category) url.searchParams.set('category', category); | |
| if (personName) url.searchParams.set('personName', personName); | |
| if (companyName) url.searchParams.set('companyName', companyName); | |
| // Fetch stories | |
| const response = await fetch(url.toString()); | |
| if (!response.ok) { | |
| throw new Error(`API Error: ${response.status}`); | |
| } | |
| return response.json(); | |
| } | |
| // ============ USAGE EXAMPLES ============ | |
| // 1. Fetch UK trending stories | |
| const trending = await fetchTrendingStories({ | |
| apiKey: 'YOUR_API_KEY', | |
| country: 'gb' | |
| }); | |
| console.log(trending.results); // Array of story objects | |
| // 2. Fetch UK Politics stories | |
| const politics = await fetchTrendingStories({ | |
| apiKey: 'YOUR_API_KEY', | |
| country: 'gb', | |
| category: 'Politics' | |
| }); | |
| // 3. Fetch stories about a specific person | |
| const personStories = await fetchTrendingStories({ | |
| apiKey: 'YOUR_API_KEY', | |
| country: 'gb', | |
| personName: 'Keir Starmer' | |
| }); | |
| // 4. Paginate results | |
| const page2 = await fetchTrendingStories({ | |
| apiKey: 'YOUR_API_KEY', | |
| country: 'gb', | |
| page: 1 // Second page (0-indexed) | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment