Spaces:
Build error
Build error
Commit Β·
a2a0f68
1
Parent(s): 1901386
feat(scrapers): add DataGov scraper for govt open datasets
Browse files- Fetches datasets from India's data.gov.in portal
- Supports MGNREGA and PM-KISAN state-level data
- Auto-saves JSON to data/samples/ with timestamp
- Inherits rate limiting from BaseScraper (polite 2s delay)
- Uses public API key (no registration needed)
- scrapers/datagov_scraper.py +111 -0
scrapers/datagov_scraper.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
BharatGraph - Data.gov.in Scraper
|
| 3 |
+
Fetches public datasets from India's Open Government Data portal.
|
| 4 |
+
Free API key already included (public key).
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import json
|
| 8 |
+
import os
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
from scrapers.base_scraper import BaseScraper
|
| 11 |
+
from loguru import logger
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class DataGovScraper(BaseScraper):
|
| 15 |
+
"""
|
| 16 |
+
Scrapes data from data.gov.in - India's official open data portal.
|
| 17 |
+
Datasets: government schemes, beneficiary lists, budget data.
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
# Public datasets we want (resource IDs from data.gov.in)
|
| 21 |
+
DATASETS = {
|
| 22 |
+
"mgnrega_states": "9ef84268-d588-465a-a308-a864a43d0070",
|
| 23 |
+
"pm_kisan_states": "c1e45527-0de1-4afe-90c0-8e70c9f9dd9d",
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
def __init__(self):
|
| 27 |
+
super().__init__(name="datagov", delay=2.0)
|
| 28 |
+
self.base_url = "https://api.data.gov.in/resource/"
|
| 29 |
+
# This is a public key from data.gov.in - works for basic access
|
| 30 |
+
self.api_key = "579b464db66ec23d9960025070515804"
|
| 31 |
+
|
| 32 |
+
def fetch_dataset(self, resource_id: str, limit: int = 100) -> dict:
|
| 33 |
+
"""
|
| 34 |
+
Fetch a dataset from data.gov.in by resource ID.
|
| 35 |
+
Returns raw JSON data.
|
| 36 |
+
"""
|
| 37 |
+
url = f"{self.base_url}{resource_id}"
|
| 38 |
+
params = {
|
| 39 |
+
"api-key": self.api_key,
|
| 40 |
+
"format": "json",
|
| 41 |
+
"limit": limit,
|
| 42 |
+
"offset": 0,
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
logger.info(f"[DataGov] Fetching dataset: {resource_id}")
|
| 46 |
+
data = self.get_json(url, params=params)
|
| 47 |
+
|
| 48 |
+
if data:
|
| 49 |
+
logger.info(f"[DataGov] Got {data.get('total', '?')} total records")
|
| 50 |
+
return data
|
| 51 |
+
|
| 52 |
+
def fetch_all_datasets(self, save: bool = True) -> dict:
|
| 53 |
+
"""
|
| 54 |
+
Fetch all configured datasets and optionally save to disk.
|
| 55 |
+
Returns dict of {name: data}.
|
| 56 |
+
"""
|
| 57 |
+
results = {}
|
| 58 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 59 |
+
|
| 60 |
+
for name, resource_id in self.DATASETS.items():
|
| 61 |
+
logger.info(f"[DataGov] Processing: {name}")
|
| 62 |
+
data = self.fetch_dataset(resource_id, limit=50)
|
| 63 |
+
|
| 64 |
+
if data:
|
| 65 |
+
results[name] = data
|
| 66 |
+
|
| 67 |
+
if save:
|
| 68 |
+
filepath = f"data/samples/datagov_{name}_{timestamp}.json"
|
| 69 |
+
self.save_json(data, filepath)
|
| 70 |
+
logger.success(f"[DataGov] Saved: {filepath}")
|
| 71 |
+
else:
|
| 72 |
+
logger.warning(f"[DataGov] Failed to fetch: {name}")
|
| 73 |
+
|
| 74 |
+
return results
|
| 75 |
+
|
| 76 |
+
def get_dataset_info(self, resource_id: str) -> dict:
|
| 77 |
+
"""
|
| 78 |
+
Get metadata/info about a dataset.
|
| 79 |
+
"""
|
| 80 |
+
url = f"https://api.data.gov.in/resource/{resource_id}"
|
| 81 |
+
params = {
|
| 82 |
+
"api-key": self.api_key,
|
| 83 |
+
"format": "json",
|
| 84 |
+
"limit": 1,
|
| 85 |
+
}
|
| 86 |
+
return self.get_json(url, params=params)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# ββ Run directly to test ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
print("=" * 60)
|
| 92 |
+
print("BharatGraph - DataGov Scraper Test")
|
| 93 |
+
print("=" * 60)
|
| 94 |
+
|
| 95 |
+
scraper = DataGovScraper()
|
| 96 |
+
|
| 97 |
+
print("\n[1] Fetching MGNREGA dataset (50 records)...")
|
| 98 |
+
data = scraper.fetch_dataset(
|
| 99 |
+
"9ef84268-d588-465a-a308-a864a43d0070", limit=10
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
if data:
|
| 103 |
+
print(f" β
Success! Total records available: {data.get('total', '?')}")
|
| 104 |
+
print(f" Fields: {list(data.get('fields', [{}])[0].keys()) if data.get('fields') else 'N/A'}")
|
| 105 |
+
else:
|
| 106 |
+
print(" β Failed (check internet connection)")
|
| 107 |
+
|
| 108 |
+
print("\n[2] Fetching all configured datasets...")
|
| 109 |
+
all_data = scraper.fetch_all_datasets(save=True)
|
| 110 |
+
print(f" β
Fetched {len(all_data)} datasets")
|
| 111 |
+
print("\nDone! Check data/samples/ folder for output files.")
|