Read r/couponcodes into stores and coupons every five minutes - #59
Merged
Merged
Conversation
c0upons only filled itself from nichedb.dev's deals collection. The
subreddit where people paste the code they were just handed was not read
at all, so its listings never reached the site. This adds a second source.
lib/reddit-sync.ts reads the subreddit's Atom feed (new.rss, 25 posts),
maps each post that carries a code or a link to the store into a coupon
row under a store it names, and upserts on (source 'reddit', source_id
'couponcodes:<post id>'). Requests for a code are declined, and so is any
post that links back to c0upons.com, or the site would echo itself. A null
code never overwrites one the browser reveal already found.
Reddit answers 429 to a plain fetch from a datacenter address, so the
feed is fetched directly first and through the Obscura relay's fetch_page
(format original) when Reddit refuses; the run reports which path
answered. The store comes from the title ("$10 off at Wonder", "Sideline
Swap Coupon Code") or from the linked host when the title is a sentence,
and the linked host wins when the title merely contains it, so "Deutscher
Starlink" and "Starlink" are one store. Codes come from "code: X" prose,
long numeric promo codes, or a link's promoCode/applyCode style parameter.
Five minutes is finer than GitHub's scheduler keeps, and the app has no
worker, so instrumentation.ts starts an in-process poller
(lib/reddit-poller.ts, REDDIT_SYNC_INTERVAL_MINUTES, 0 disables) at boot.
POST /api/sync/reddit is the keyless trigger, throttled to one run per
four minutes; the keepalive workflow calls it as a backstop, both CLIs'
sync commands call it, and the MCP server gains sync_reddit.
Verified: 33 tests (test/reddit-sync.test.mjs runs the real migration
against a local libSQL file with a fake Reddit answering 429 and a fake
relay), tsc, eslint on the touched files, next build, and a dry run of the
mapper over the live feed: 21 of 25 posts taken, 4 declined as intended.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01R63WmerrXfwcHkFRc61J2B
| /* ------------------------------------------------------------------------ */ | ||
|
|
||
| async function getState(db: SqlDb, key: string): Promise<string | null> { | ||
| const rows = await db.sql`SELECT value FROM sync_state WHERE key = ${key} LIMIT 1`; |
| website = COALESCE(stores.website, excluded.website), | ||
| logo_url = COALESCE(stores.logo_url, excluded.logo_url) | ||
| `; | ||
| const rows = await db.sql`SELECT id FROM stores WHERE slug = ${store.slug} LIMIT 1`; |
|
|
||
| /** Insert or update one row; says which. A found code is never replaced by a null one. */ | ||
| async function upsertCoupon(db: SqlDb, storeId: number, row: RedditCouponRow): Promise<'inserted' | 'updated'> { | ||
| const existing = await db.sql`SELECT id FROM coupons WHERE source = ${row.source} AND source_id = ${row.source_id} LIMIT 1`; |
ThreatCrush Security Scan27 finding(s) MEDIUM: 25 | LOW: 2
Snippets are redacted; ThreatCrush never prints matched credential material. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
c0upons now reads r/couponcodes (
https://www.reddit.com/r/couponcodes/new.rss) into stores and coupons, every five minutes, for new listings. Until now the only source was nichedb.dev's deals collection.How
apps/web/lib/reddit-sync.ts: fetches the Atom feed, maps each post that carries a code or a link to the store into a coupon under a store it names, and upserts on(source 'reddit', source_id 'couponcodes:<post id>'). Requests for a code are declined, and so is any post linking back to c0upons.com (our own coupons get reposted there, so this stops the echo). A null code never overwrites a code the browser reveal found.fetch_pagewithformat: original; the result saysvia: direct | relay.OBSCURA_RELAY_URLoverrides the relay,REDDIT_COUPONCODES_FEEDthe feed.promoCode/applyCode/auto_applied_promo_codeparameter.apps/web/instrumentation.tsstarts an in-process poller (lib/reddit-poller.ts) at server boot.REDDIT_SYNC_INTERVAL_MINUTESsets the cadence,0disables. GitHub's scheduler is too coarse and unreliable for this, and the app has no worker.POST /api/sync/reddit: keyless trigger, throttled to one run per four minutes. Added to the keepalive workflow as a backstop, to both CLIs'sync, to the docs page, and to the MCP server assync_reddit(descriptor updated).Verified
node --test test/*.test.mjs: 33 pass.test/reddit-sync.test.mjsruns the real migration against a local libSQL file, a fake Reddit that answers 429, and a fake relay; it covers parsing, mapping, the echo skip, the relay fallback, the throttle, idempotent re-runs and code preservation.tsc --noEmitclean,eslintclean on the touched files,next buildsucceeds.After merge
Railway deploys; the poller starts on boot and the first read happens 20 seconds later. Check with:
A
skippedanswer means the poller just ran. New stores appear at/stores; rows carrysource = 'reddit'.🤖 Generated with Claude Code
https://claude.ai/code/session_01R63WmerrXfwcHkFRc61J2B