File size: 3,292 Bytes
6bcb5d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a0fba4
6bcb5d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from datetime import datetime
from bs4 import BeautifulSoup
from scrapers.base_scraper import BaseScraper
from loguru import logger


class CPPPScraper(BaseScraper):

    BASE_URL   = "https://eprocure.gov.in"
    TENDERS_URL = "https://eprocure.gov.in/eprocure/app"
    API_URL    = "https://api.data.gov.in/resource/40cce240-bdce-4c45-b498-e2b70ab27b3e"

    def __init__(self):
        super().__init__(name="cppp", delay=2.0)
        self.api_key = os.getenv("DATAGOV_API_KEY", "")
        self.session.headers.update({
            "User-Agent": (
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                "AppleWebKit/537.36 Chrome/120.0.0.0 Safari/537.36"
            ),
        })

    def fetch_tenders(self, save: bool = True) -> list:
        logger.info("[CPPP] Fetching central procurement tenders...")
        params = {
            "api-key": self.api_key,
            "format":  "json",
            "limit":   50,
        }
        data    = self.get_json(self.API_URL, params=params)
        tenders = []
        if data and isinstance(data, dict):
            tenders = self._parse_response(data)
        if not tenders:
            logger.warning("[CPPP] Could not fetch live data -- using sample")
            tenders = self._get_sample_tenders()
        if save and tenders:
            ts       = datetime.now().strftime("%Y%m%d_%H%M%S")
            filepath = f"data/samples/cppp_tenders_{ts}.json"
            self.save_json(tenders, filepath)
            logger.success(f"[CPPP] Saved {len(tenders)} tenders")
        return tenders

    def _parse_response(self, data: dict) -> list:
        raw = (data.get("records") or data.get("data") or [])
        return [
            {**r,
             "source":      "Central Public Procurement Portal",
             "source_url":  self.BASE_URL,
             "scraped_at":  datetime.now().isoformat(),
             "entity_type": "tender"}
            for r in raw[:50]
        ]

    def _get_sample_tenders(self) -> list:
        return [
            {
                "tender_id":        "CPPP/2024/001234",
                "title":            "Construction of Rural Roads under PMGSY",
                "ministry":         "Ministry of Rural Development",
                "department":       "National Rural Roads Development Agency",
                "estimated_value":  45000000,
                "estimated_crore":  4.5,
                "tender_type":      "Open Tender",
                "bid_submission_end":"2024-08-30",
                "status":           "Awarded",
                "awarded_to":       "XYZ Infrastructure Pvt Ltd",
                "awarded_value":    42500000,
                "single_bid":       False,
                "source":           "CPPP (sample)",
                "source_url":       self.BASE_URL,
                "scraped_at":       datetime.now().isoformat(),
                "entity_type":      "tender",
            },
        ]


if __name__ == "__main__":
    print("=" * 55)
    print("BharatGraph - CPPP Scraper")
    print("=" * 55)
    scraper = CPPPScraper()
    tenders = scraper.fetch_tenders(save=True)
    print(f"\n  Records: {len(tenders)}")
    if tenders:
        print(f"  Example: {tenders[0].get('title', '')[:60]}")
    print("\nDone!")