|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Fetches sportsbook data from the SharpAPI and regenerates the |
| 5 | + * dynamic tables in content/en/api-reference/sportsbooks.mdx. |
| 6 | + * |
| 7 | + * Sections between {/* AUTO:START:<name> *\/} and {/* AUTO:END:<name> *\/} |
| 8 | + * markers are replaced. Everything else is left untouched. |
| 9 | + * |
| 10 | + * Usage: node scripts/generate-sportsbooks.mjs |
| 11 | + */ |
| 12 | + |
| 13 | +import { readFileSync, writeFileSync } from 'node:fs'; |
| 14 | +import { resolve, dirname } from 'node:path'; |
| 15 | +import { fileURLToPath } from 'node:url'; |
| 16 | + |
| 17 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 18 | +const MDX_PATH = resolve(__dirname, '../content/en/api-reference/sportsbooks.mdx'); |
| 19 | +const API_URL = 'https://api.sharpapi.io/api/v1/sportsbooks'; |
| 20 | + |
| 21 | +// -- Classification ---------------------------------------------------------- |
| 22 | + |
| 23 | +const EXCHANGES = new Set(['prophetx', 'betfair']); |
| 24 | +const PREDICTION_MARKETS = new Set(['polymarket', 'kalshi']); |
| 25 | + |
| 26 | +function classify(book) { |
| 27 | + if (PREDICTION_MARKETS.has(book.id)) return 'prediction'; |
| 28 | + if (EXCHANGES.has(book.id)) return 'exchange'; |
| 29 | + if (book.is_sharp) return 'sharp'; |
| 30 | + const regions = book.regions.map(r => r.toUpperCase()); |
| 31 | + if (!regions.includes('US')) return 'international'; |
| 32 | + return 'us'; |
| 33 | +} |
| 34 | + |
| 35 | +// -- Table Helpers ----------------------------------------------------------- |
| 36 | + |
| 37 | +function yn(val) { return val ? 'Yes' : 'No'; } |
| 38 | + |
| 39 | +function tierLabel(tier) { |
| 40 | + if (!tier || tier === 'free') return 'Free'; |
| 41 | + return tier.charAt(0).toUpperCase() + tier.slice(1); |
| 42 | +} |
| 43 | + |
| 44 | +function tierDisplay(tier) { |
| 45 | + const label = tierLabel(tier); |
| 46 | + return label === 'Sharp' ? '**Sharp**' : label; |
| 47 | +} |
| 48 | + |
| 49 | +function bookTable(books) { |
| 50 | + const rows = books |
| 51 | + .sort((a, b) => { |
| 52 | + // Sort by tier weight then alphabetically |
| 53 | + const tw = { free: 0, hobby: 1, pro: 2, sharp: 3, enterprise: 4 }; |
| 54 | + const ta = tw[a.requires_tier] ?? 0; |
| 55 | + const tb = tw[b.requires_tier] ?? 0; |
| 56 | + if (ta !== tb) return ta - tb; |
| 57 | + return a.display_name.localeCompare(b.display_name); |
| 58 | + }) |
| 59 | + .map(b => |
| 60 | + `| \`${b.id}\` | ${b.display_name} | ${yn(b.has_live_odds)} | ${yn(b.has_player_props)} | ${tierDisplay(b.requires_tier)} |` |
| 61 | + ); |
| 62 | + |
| 63 | + return [ |
| 64 | + '| ID | Name | Live | Props | Tier |', |
| 65 | + '|----|------|------|-------|------|', |
| 66 | + ...rows, |
| 67 | + ].join('\n'); |
| 68 | +} |
| 69 | + |
| 70 | +// -- Tier Summary ------------------------------------------------------------ |
| 71 | + |
| 72 | +function tierSummary(books) { |
| 73 | + const free = books.filter(b => !b.requires_tier || b.requires_tier === 'free'); |
| 74 | + const hobby = books.filter(b => b.requires_tier === 'hobby'); |
| 75 | + const pro = books.filter(b => b.requires_tier === 'pro'); |
| 76 | + const sharp = books.filter(b => b.requires_tier === 'sharp'); |
| 77 | + |
| 78 | + const freeCount = free.length; |
| 79 | + const hobbyCount = freeCount + hobby.length; |
| 80 | + const proCount = hobbyCount + pro.length; |
| 81 | + const totalCount = books.length; |
| 82 | + |
| 83 | + const freeNames = free.slice(0, 4).map(b => b.display_name).join(', '); |
| 84 | + const freeSuffix = free.length > 4 ? `, and ${free.length - 4} more` : ''; |
| 85 | + const hobbyNames = hobby.length ? hobby.map(b => b.display_name).join(', ') : ''; |
| 86 | + const proNames = pro.length ? pro.map(b => b.display_name).join(', ') : ''; |
| 87 | + |
| 88 | + return [ |
| 89 | + '| Tier | Books Available | Included Sportsbooks |', |
| 90 | + '|------|-----------------|----------------------|', |
| 91 | + `| **Free** | ${freeCount} | ${freeNames}${freeSuffix} |`, |
| 92 | + `| **Hobby** | ${hobbyCount} | ${hobbyNames ? `+ ${hobbyNames}` : 'Same as Free'} |`, |
| 93 | + `| **Pro** | ${proCount} | ${proNames ? `+ ${proNames}` : 'Same as Hobby'} |`, |
| 94 | + `| **Sharp** | ${totalCount} | All available sportsbooks |`, |
| 95 | + `| **Enterprise** | ${totalCount} | All available sportsbooks |`, |
| 96 | + ].join('\n'); |
| 97 | +} |
| 98 | + |
| 99 | +// -- MDX Replacement --------------------------------------------------------- |
| 100 | + |
| 101 | +function replaceSection(content, name, replacement) { |
| 102 | + // Matches {/* AUTO:START:<name> */} ... {/* AUTO:END:<name> */} |
| 103 | + const pattern = new RegExp( |
| 104 | + `(\\{/\\* AUTO:START:${name} \\*/\\})\n[\\s\\S]*?\n(\\{/\\* AUTO:END:${name} \\*/\\})`, |
| 105 | + 'm' |
| 106 | + ); |
| 107 | + if (!pattern.test(content)) { |
| 108 | + console.error(`Warning: marker AUTO:START:${name} / AUTO:END:${name} not found in MDX`); |
| 109 | + return content; |
| 110 | + } |
| 111 | + return content.replace(pattern, `$1\n${replacement}\n$2`); |
| 112 | +} |
| 113 | + |
| 114 | +// -- Main -------------------------------------------------------------------- |
| 115 | + |
| 116 | +async function main() { |
| 117 | + console.log(`Fetching sportsbooks from ${API_URL}...`); |
| 118 | + |
| 119 | + const res = await fetch(API_URL); |
| 120 | + if (!res.ok) { |
| 121 | + // Non-fatal: keep existing MDX so builds don't break if API is down |
| 122 | + console.error(`API returned ${res.status} — skipping sportsbook generation, keeping existing content.`); |
| 123 | + process.exit(0); |
| 124 | + } |
| 125 | + |
| 126 | + const { data: books } = await res.json(); |
| 127 | + console.log(`Received ${books.length} sportsbooks from API.`); |
| 128 | + |
| 129 | + // Filter out unlisted books |
| 130 | + const active = books.filter(b => !b.unlisted); |
| 131 | + |
| 132 | + // Classify |
| 133 | + const grouped = { us: [], sharp: [], international: [], exchange: [], prediction: [] }; |
| 134 | + for (const book of active) { |
| 135 | + grouped[classify(book)].push(book); |
| 136 | + } |
| 137 | + |
| 138 | + // Read existing MDX |
| 139 | + let mdx = readFileSync(MDX_PATH, 'utf-8'); |
| 140 | + |
| 141 | + // Replace tier summary |
| 142 | + mdx = replaceSection(mdx, 'tier-summary', tierSummary(active)); |
| 143 | + |
| 144 | + // Replace each category table |
| 145 | + if (grouped.us.length) mdx = replaceSection(mdx, 'us-books', bookTable(grouped.us)); |
| 146 | + if (grouped.sharp.length) mdx = replaceSection(mdx, 'sharp-books', bookTable(grouped.sharp)); |
| 147 | + if (grouped.international.length) mdx = replaceSection(mdx, 'intl-books', bookTable(grouped.international)); |
| 148 | + if (grouped.exchange.length) mdx = replaceSection(mdx, 'exchange-books', bookTable(grouped.exchange)); |
| 149 | + if (grouped.prediction.length) mdx = replaceSection(mdx, 'prediction-books', bookTable(grouped.prediction)); |
| 150 | + |
| 151 | + writeFileSync(MDX_PATH, mdx); |
| 152 | + console.log(`Updated ${MDX_PATH}`); |
| 153 | + |
| 154 | + // Summary |
| 155 | + console.log(` US: ${grouped.us.length}, Sharp: ${grouped.sharp.length}, International: ${grouped.international.length}, Exchanges: ${grouped.exchange.length}, Prediction: ${grouped.prediction.length}`); |
| 156 | +} |
| 157 | + |
| 158 | +main().catch(err => { |
| 159 | + console.error('Sportsbook generation failed (non-fatal):', err.message); |
| 160 | + process.exit(0); // Don't break the build |
| 161 | +}); |
0 commit comments