-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
397 lines (345 loc) · 14 KB
/
Copy pathmain.py
File metadata and controls
397 lines (345 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
from fastapi import FastAPI, HTTPException, Query, Request, Depends
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import Response
from typing import Optional
import os
import httpx
import logging
import json
import sqlite3
import time
from pathlib import Path
from datetime import datetime, timezone
from collections import defaultdict
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# API Key + Rate Limiting
# ---------------------------------------------------------------------------
DB_PATH = Path(__file__).parent / "api_keys.db"
TIER_LIMITS = {
"free": 100,
"startup": 10_000,
"business": 100_000,
"enterprise": None, # unlimited
}
# In-memory rate-limit counters: {api_key: {"count": N, "window_start": epoch}}
_rate_counters: dict = defaultdict(lambda: {"count": 0, "window_start": time.time()})
def _get_db():
conn = sqlite3.connect(str(DB_PATH))
conn.row_factory = sqlite3.Row
conn.execute("""
CREATE TABLE IF NOT EXISTS api_keys (
key TEXT PRIMARY KEY,
tier TEXT NOT NULL DEFAULT 'free',
owner TEXT,
created_at TEXT DEFAULT (datetime('now'))
)
""")
conn.commit()
return conn
def _seed_free_key(key: str):
"""Auto-register unknown keys as free tier (frictionless onboarding)."""
with _get_db() as conn:
conn.execute(
"INSERT OR IGNORE INTO api_keys (key, tier, owner) VALUES (?, 'free', 'self-registered')",
(key,)
)
def _lookup_key(key: str):
with _get_db() as conn:
row = conn.execute("SELECT * FROM api_keys WHERE key = ?", (key,)).fetchone()
return row
def validate_api_key(request: Request):
"""FastAPI dependency — validates X-API-Key header and enforces rate limits."""
api_key = request.headers.get("X-API-Key") or request.query_params.get("api_key")
# No key → free tier, limited
if not api_key:
raise HTTPException(
status_code=401,
detail="API key required. Add header: X-API-Key: <your-key>. Get a free key at https://rapidapi.com/search/government-contracts"
)
row = _lookup_key(api_key)
if not row:
# Auto-register as free tier for frictionless onboarding
_seed_free_key(api_key)
row = _lookup_key(api_key)
tier = row["tier"]
daily_limit = TIER_LIMITS.get(tier)
if daily_limit is not None:
now = time.time()
counter = _rate_counters[api_key]
# Reset counter if 24-hour window has elapsed
if now - counter["window_start"] > 86_400:
counter["count"] = 0
counter["window_start"] = now
counter["count"] += 1
if counter["count"] > daily_limit:
raise HTTPException(
status_code=429,
detail=f"Rate limit exceeded for {tier} tier ({daily_limit:,} req/day). Upgrade at https://rapidapi.com/search/government-contracts"
)
return {"key": api_key, "tier": tier}
# ---------------------------------------------------------------------------
app = FastAPI(
title="Government Contracts & Grants API",
description="Real-time access to US Government contracts, grants, and agency spending data from USASpending.gov",
version="2.1.0",
docs_url="/docs",
redoc_url="/redoc"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=False,
allow_methods=["GET"],
allow_headers=["*"],
)
USASPENDING_BASE = "https://api.usaspending.gov/api/v2"
@app.api_route("/", methods=["GET", "HEAD"])
async def root():
return {
"message": "Government Contracts & Grants API",
"version": "2.0.0",
"data_source": "USASpending.gov (live)",
"docs": "/docs",
"endpoints": {
"contracts": "/contracts",
"grants": "/grants",
"agencies": "/agencies",
"health": "/health"
}
}
@app.get("/ping")
async def ping():
return {"status": "ok"}
@app.get("/health")
async def health():
try:
async with httpx.AsyncClient(timeout=10.0) as client:
r = await client.get(f"{USASPENDING_BASE}/references/toptier_agencies/?limit=1")
upstream = "ok" if r.status_code == 200 else f"status {r.status_code}"
except Exception as e:
upstream = f"error: {str(e)}"
return {"status": "healthy", "upstream_usaspending": upstream, "version": "2.0.0"}
@app.get("/contracts")
async def get_contracts(
agency: Optional[str] = Query(None, description="Filter by awarding agency name (e.g. 'NASA', 'Department of Defense')"),
amount_min: Optional[float] = Query(None, description="Minimum award amount in USD"),
amount_max: Optional[float] = Query(None, description="Maximum award amount in USD"),
keyword: Optional[str] = Query(None, description="Keyword search in contract description"),
limit: int = Query(10, ge=1, le=100, description="Number of results (max 100)"),
auth: dict = Depends(validate_api_key)
):
"""
Fetch real US government contracts from USASpending.gov.
Returns the largest contracts by default, sorted by award amount descending.
Filter by agency, dollar amount, or keyword.
"""
filters = {
"award_type_codes": ["A", "B", "C", "D"] # Contract types only
}
if agency:
filters["agencies"] = [{"type": "awarding", "tier": "toptier", "name": agency}]
if amount_min or amount_max:
amt = {}
if amount_min:
amt["lower_bound"] = amount_min
if amount_max:
amt["upper_bound"] = amount_max
filters["award_amounts"] = [amt]
if keyword:
filters["keywords"] = [keyword]
payload = {
"filters": filters,
"fields": [
"Award ID",
"Recipient Name",
"Award Amount",
"Awarding Agency",
"Description",
"Start Date",
"End Date",
"Place of Performance City Name",
"Place of Performance State Code"
],
"page": 1,
"limit": limit,
"sort": "Award Amount",
"order": "desc"
}
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(
f"{USASPENDING_BASE}/search/spending_by_award/",
json=payload
)
response.raise_for_status()
data = response.json()
results = []
for r in data.get("results", []):
results.append({
"contract_id": r.get("Award ID", ""),
"recipient": r.get("Recipient Name", ""),
"agency": r.get("Awarding Agency", ""),
"amount_usd": r.get("Award Amount", 0),
"description": r.get("Description", ""),
"start_date": r.get("Start Date", ""),
"end_date": r.get("End Date", ""),
"location": f"{r.get('Place of Performance City Name', '')}, {r.get('Place of Performance State Code', '')}".strip(", ")
})
return {
"count": len(results),
"source": "USASpending.gov",
"contracts": results
}
except httpx.HTTPStatusError as e:
raise HTTPException(status_code=502, detail=f"USASpending.gov error: {e.response.status_code}")
except Exception as e:
logger.error(f"Contracts error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/grants")
async def get_grants(
agency: Optional[str] = Query(None, description="Filter by awarding agency name"),
amount_min: Optional[float] = Query(None, description="Minimum grant amount in USD"),
keyword: Optional[str] = Query(None, description="Keyword search in grant description"),
limit: int = Query(10, ge=1, le=100, description="Number of results (max 100)"),
auth: dict = Depends(validate_api_key)
):
"""
Fetch real US government grants from USASpending.gov.
Returns the largest grants by default, sorted by award amount descending.
"""
filters = {
"award_type_codes": ["02", "03", "04", "05"] # Grant type codes
}
if agency:
filters["agencies"] = [{"type": "awarding", "tier": "toptier", "name": agency}]
if amount_min:
filters["award_amounts"] = [{"lower_bound": amount_min}]
if keyword:
filters["keywords"] = [keyword]
payload = {
"filters": filters,
"fields": [
"Award ID",
"Recipient Name",
"Award Amount",
"Awarding Agency",
"Description",
"Start Date",
"End Date"
],
"page": 1,
"limit": limit,
"sort": "Award Amount",
"order": "desc"
}
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(
f"{USASPENDING_BASE}/search/spending_by_award/",
json=payload
)
response.raise_for_status()
data = response.json()
results = []
for r in data.get("results", []):
results.append({
"grant_id": r.get("Award ID", ""),
"recipient": r.get("Recipient Name", ""),
"agency": r.get("Awarding Agency", ""),
"amount_usd": r.get("Award Amount", 0),
"description": r.get("Description", ""),
"start_date": r.get("Start Date", ""),
"end_date": r.get("End Date", "")
})
return {
"count": len(results),
"source": "USASpending.gov",
"grants": results
}
except httpx.HTTPStatusError as e:
raise HTTPException(status_code=502, detail=f"USASpending.gov error: {e.response.status_code}")
except Exception as e:
logger.error(f"Grants error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/agencies")
async def get_agencies(
limit: int = Query(50, ge=1, le=100, description="Number of agencies to return"),
auth: dict = Depends(validate_api_key)
):
"""
Fetch list of US government agencies from USASpending.gov.
Returns agency names, codes, and abbreviations.
"""
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.get(f"{USASPENDING_BASE}/references/toptier_agencies/")
response.raise_for_status()
data = response.json()
agencies = data.get("results", [])[:limit]
results = []
for a in agencies:
results.append({
"agency_code": a.get("toptier_code", ""),
"name": a.get("agency_name", ""),
"abbreviation": a.get("abbreviation", ""),
"active_fy": a.get("active_fy", ""),
"budget_authority_amount": a.get("budget_authority_amount", 0),
"obligated_amount": a.get("obligated_amount", 0),
})
return {
"count": len(results),
"source": "USASpending.gov",
"agencies": results
}
except httpx.HTTPStatusError as e:
raise HTTPException(status_code=502, detail=f"USASpending.gov error: {e.response.status_code}")
except Exception as e:
logger.error(f"Agencies error: {e}")
raise HTTPException(status_code=500, detail=str(e))
NEWSLETTER_FILE = Path(__file__).parent / "newsletter_posts.json"
def get_posts():
if NEWSLETTER_FILE.exists():
return json.loads(NEWSLETTER_FILE.read_text())
return [{
"title": "Polymarket Edge: How to Read Market Signals Before the Crowd",
"preview": "The 3 patterns that consistently predict market moves on Polymarket",
"content": "<h1>Polymarket Edge: How to Read Market Signals Before the Crowd</h1><p>Most Polymarket traders react to news. The edge is in anticipating it.</p><h2>The 3 Patterns That Work</h2><p><strong>1. Volume Spike Before Price Move</strong><br>When trading volume on a market spikes 3x+ without a price move, a big move is coming.</p><p><strong>2. Bid-Ask Spread Compression</strong><br>When spreads tighten dramatically, market makers are confident in the outcome.</p><p><strong>3. Late Resolution Drift</strong><br>Markets close to resolution date with prices far from 0 or 100 often drift fast in the final 48 hours.</p><p>Stay sharp,<br><strong>Polymarket Edge</strong></p>",
"slug": "market-signals-before-the-crowd",
"pub_date": "Mon, 02 Jun 2026 17:00:00 +0000"
}]
@app.get("/newsletter/feed.xml", include_in_schema=False)
async def rss_feed():
"""RSS feed for Polymarket Edge Newsletter — auto-imported by Beehiiv"""
posts = get_posts()
items = ""
for post in posts:
items += f"""
<item>
<title><![CDATA[{post['title']}]]></title>
<link>https://government-data-api.onrender.com/newsletter/{post['slug']}</link>
<guid>https://government-data-api.onrender.com/newsletter/{post['slug']}</guid>
<pubDate>{post['pub_date']}</pubDate>
<description><![CDATA[{post['preview']}]]></description>
<content:encoded><![CDATA[{post['content']}]]></content:encoded>
</item>"""
rss = f"""<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>Polymarket Edge Newsletter</title>
<link>https://government-data-api.onrender.com/newsletter</link>
<description>Trading signals, market analysis, and Polymarket edge strategies</description>
<language>en-us</language>
<atom:link href="https://government-data-api.onrender.com/newsletter/feed.xml" rel="self" type="application/rss+xml"/>
{items}
</channel>
</rss>"""
return Response(content=rss, media_type="application/rss+xml")
if __name__ == "__main__":
import uvicorn
uvicorn.run(
app,
host=os.getenv("API_HOST", "0.0.0.0"),
port=int(os.getenv("PORT", 8000))
)