From 972421fbbe980b33e2b77bdc85f85a64efa8d286 Mon Sep 17 00:00:00 2001 From: martbost Date: Wed, 30 Sep 2026 07:57:47 -0500 Subject: [PATCH] PassiveDrip PTC bridge: visit campaigns listed on PassiveDrip's paid-to-click page (signed sync every 5 min), delivered visits read back and applied to the pack (read, never derived); admin status + run routes Co-Authored-By: Claude Fable 5.1 --- ads.js | Bin 118027 -> 119705 bytes pdsync.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ server.js | 7 +++++- 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 pdsync.js diff --git a/ads.js b/ads.js index cebbc0073107893dec0e33af6b66ce2aab95cfdb..28b025931e2ca3351cae17b48de8f58ed5711400 100644 GIT binary patch delta 1098 zcmb7DO>5Lp6y*t099mGMRQxCx6*6IBS`iUC9S5}oEwtL!stXbNn)hbrVdf>iWIEP% zsJhgZ1dOfP)|I$&Q}9m+F8l+E3zz-@rQWVtSwJNN$MpiP zi#~O3L#xguh!Cp6bpsnNdzrw{1I1fZF{tB+qJGf-S^TuJI)*Pi4vwG+%r{0XTA0lMOF6P9z3Leb}w{bzL1gq0N-Hj5yt zkk<%|mJmQ?VN*$1)*OPf5g3Mc^x(^h=+pNLaeJN29!c2Kik0q+O;zgR?(Jw}Wv~xD zPJC3J*ML%NZlVQgUQZJ0+B0iiUN&A!I7X(yz@T3I{Dw!udqq za`fuq@o1-+J6whv?a=ptc_sync {campaigns:[{id, title, desc, url, visitsLeft}]} (HMAC-signed) -> PD upserts rows by +// our campaign id, pauses any row we no longer list, keeps ONE live row per target URL. +// 2. GET ?a=ptc_served -> [{iapId, received}] (cumulative visits PD delivered per campaign). Delivery is READ +// from PD, never derived: the delta since our last read is applied through ads.applyExternalVisits, which bumps +// imps and closes the pack when the ordered count is reached. Our last-read counters live in DATA_DIR/pdsync.json. +// +// FEATURE-FLAGGED: inert unless PD_BRIDGE_URL and PD_BRIDGE_SECRET are set. Geo-restricted campaigns are not sent +// (PassiveDrip's audience is worldwide). +'use strict'; +const fs = require('fs'), path = require('path'), http = require('http'), https = require('https'), crypto = require('crypto'); + +let X = null, FILE = null, S = { served: {}, last: null }, running = false; +function enabled() { return !!(process.env.PD_BRIDGE_URL && process.env.PD_BRIDGE_SECRET); } +function init(opts) { X = opts; FILE = path.join(opts.dataDir, 'pdsync.json'); try { S = Object.assign(S, JSON.parse(fs.readFileSync(FILE, 'utf8'))); } catch (e) {} } +function save() { try { fs.writeFileSync(FILE, JSON.stringify(S)); } catch (e) {} } + +function bridge(action, method, body) { + return new Promise((resolve, reject) => { + const raw = body ? JSON.stringify(body) : ''; + const ts = Math.floor(Date.now() / 1000); + const sig = crypto.createHmac('sha256', process.env.PD_BRIDGE_SECRET.trim()).update(ts + '\n' + method + '\n' + action + '\n' + raw).digest('hex'); + const u = new URL(process.env.PD_BRIDGE_URL.trim()); u.searchParams.set('a', action); + const req = (u.protocol === 'http:' ? http : https).request(u, { method, headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(raw), 'X-PD-Ts': String(ts), 'X-PD-Sig': sig, 'User-Agent': 'instantadpay-pdsync' }, timeout: 25000 }, res => { + let d = ''; res.on('data', c => d += c); res.on('end', () => { try { const j = JSON.parse(d); if (res.statusCode !== 200 || j.error) return reject(new Error('bridge ' + action + ': HTTP ' + res.statusCode + ' ' + (j.error || d.slice(0, 120)))); resolve(j); } catch (e) { reject(new Error('bridge ' + action + ': bad response ' + d.slice(0, 120))); } }); + }); + req.on('error', reject); req.on('timeout', () => req.destroy(new Error('bridge timeout'))); req.end(raw); + }); +} + +// what PassiveDrip should list: every active, worldwide visits campaign with visits still to deliver +async function listing() { + const all = await X.ads.adminList(2000); + return all.filter(c => c.type === 'visits' && c.status === 'active' && !c.geo && !(c.starts && c.starts > Date.now()) && /^https?:\/\//.test(c.targetUrl || '')) + .map(c => ({ id: c.id, title: String(c.name || c.title || 'Sponsor').slice(0, 64), desc: String((c.title && String(c.title).toLowerCase() !== String(c.name || '').toLowerCase()) ? c.title : '').slice(0, 128), url: c.targetUrl, visitsLeft: Math.max(0, Number(c.body || 0) - Number(c.imps || 0)) })) + .filter(c => c.visitsLeft > 0); +} + +async function sync() { + if (!enabled() || running) return { skipped: true }; + running = true; + const t = { at: Date.now(), pushed: 0, applied: 0, closed: 0, note: '' }; + try { + const camps = await listing(); t.pushed = camps.length; + const r = await bridge('ptc_sync', 'POST', { campaigns: camps }); + t.pdLive = r.live; t.pdPaused = r.paused; + const served = await bridge('ptc_served', 'GET'); + for (const row of (served.rows || [])) { + const id = String(row.iapId); const now = Number(row.received) || 0; const prev = Number(S.served[id] || 0); + if (now <= prev) { if (now < prev) S.served[id] = now; continue; } // PD's counter reset (row recreated): re-baseline, never charge backwards + try { const res = await X.ads.applyExternalVisits(Number(id), now - prev, 'PassiveDrip'); S.served[id] = now; t.applied += res.applied || 0; if (res.closed) t.closed++; } + catch (e) { console.error('pdsync apply', id, e.message); } // one bad row never stops the others; its delta is retried next sync + } + save(); + } catch (e) { t.note = String(e.message).slice(0, 160); console.error('pdsync', e.message); } + finally { S.last = t; save(); running = false; } + return t; +} +function status() { return { enabled: enabled(), bridge: enabled() ? process.env.PD_BRIDGE_URL.replace(/\?.*$/, '') : null, last: S.last, tracked: Object.keys(S.served).length }; } +module.exports = { enabled, init, sync, status, listing }; diff --git a/server.js b/server.js index c9074a6..582ae55 100644 --- a/server.js +++ b/server.js @@ -47,7 +47,8 @@ const badge = require('./badge'); // achievement badge image drawn o const friday = require('./friday'); const wednesday = require('./wednesday'); // Wired Wednesday: +25% on $10+ packs, wired into a visits campaign (2026-09-29) const fridaybank = require('./fridaybank'); // Friday Bonus Hunts: days banked Sat-Thu by solo + video (2026-09-26) -const refill = require('./refill'); // campaign running-low / ran-out notices, and what came of them // Five Dollar Friday: +20% credits on Friday packs, the wave, the badge (Marty, 2026-09-20) +const refill = require('./refill'); +const pdsync = require('./pdsync'); // PassiveDrip PTC bridge: our visit campaigns on their paid-to-click page (2026-09-30) // campaign running-low / ran-out notices, and what came of them // Five Dollar Friday: +20% credits on Friday packs, the wave, the badge (Marty, 2026-09-20) const snapshot = require('./snapshot'); // daily growth snapshot -> Telegram payments feed (Marty, 2026-09-15) const pipeline = require('./pipeline'); // sponsor follow-up board (coming soon until site setting pipelineMode = on) (Marty, 2026-09-15) const videomaker = require('./videomaker'); // Circuit tool: promo videos with the member's own end card (ffmpeg in the image) // badge-gated promo toolkit + AI Copy Engine (Surge and up) (Marty, 2026-09-14) // referral contest: /leaderboard, Overview card, weekly + monthly winners (Marty, 2026-09-14) // release notes + roadmap: /whats-new, Overview card, Admin > Releases (Marty, 2026-09-14) // blog -> Blotato -> X + Instagram on publish (Marty, 2026-09-13) // admin member card: search, drilldown, edits (Marty, 2026-09-13) // admin-written coaching articles, server-rendered public /blog with SEO metadata (Marty, 2026-09-12) @@ -573,6 +574,8 @@ async function boot() { if (ads.nasEnabled()) { console.log('NAS syndication enabled'); setTimeout(() => ads.reconcileNas().catch(e => console.error('nas reconcile', e.message)), 90 * 1000); + pdsync.init({ dataDir: DATA_DIR, ads }); + if (pdsync.enabled()) { const every = Math.max(2, Number(process.env.PD_SYNC_MIN || 5)) * 60 * 1000; setTimeout(() => pdsync.sync().catch(e => console.error('pdsync', e.message)), 90 * 1000); setInterval(() => pdsync.sync().catch(e => console.error('pdsync', e.message)), every); console.log('PassiveDrip PTC bridge: on, every', every / 60000, 'min'); } else console.log('PassiveDrip PTC bridge: off (no PD_BRIDGE_URL/SECRET)'); setInterval(() => ads.reconcileNas().catch(e => console.error('nas reconcile', e.message)), 5 * 60 * 1000); } } @@ -1331,6 +1334,8 @@ const server = http.createServer(async (req, res) => { // -- public API if (p === '/api/friday' && req.method === 'GET') return json(res, 200, friday.view(), { 'Cache-Control': 'public, max-age=60' }); if (p === '/api/wednesday' && req.method === 'GET') return json(res, 200, wednesday.view(), { 'Cache-Control': 'public, max-age=60' }); + if (p === '/api/admin/pdsync' && req.method === 'GET') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); return json(res, 200, pdsync.status()); } + if (p === '/api/admin/pdsync/run' && req.method === 'POST') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); return json(res, 200, await pdsync.sync()); } if (p === '/api/admin/wednesday-previews' && req.method === 'GET') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); return json(res, 200, wednesday.previews()); } if (p === '/api/admin/wednesday/kick' && req.method === 'POST') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); const r = await wednesday.kickNow(); return json(res, r.error ? 400 : 200, r); } // -- the pages a member can turn into a viral link (promo tools > Viral links)