import requests import pdfplumber import io import re def extract_text_from_bse_pdf(pdf_url): """ BSE ka PDF download karke text nikalta hai. Jaise photocopy machine print nikaalta hai - yeh function PDF se text nikaalta hai! Memory mein karta hai - disk pe kuch save NAHI hoga. """ headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0", # BSE ko lagega aap uske website se aa rahe ho "Referer": "https://www.bseindia.com/", "Accept": "application/pdf,*/*", } try: # PDF ko memory mein download karo (RAM mein, disk pe nahi) response = requests.get(pdf_url, headers=headers, timeout=30) response.raise_for_status() pdf_bytes = io.BytesIO(response.content) # pdfplumber se page by page text nikalo full_text = "" with pdfplumber.open(pdf_bytes) as pdf: for i, page in enumerate(pdf.pages): page_text = page.extract_text() if page_text: full_text += f"\n--- Page {i + 1} ---\n" full_text += page_text # Clean karo text - extra spaces aur lines hataao full_text = clean_text(full_text) return full_text.strip() except requests.exceptions.RequestException as e: return f"❌ PDF download nahi hua: {str(e)}" except Exception as e: return f"❌ PDF read nahi hua: {str(e)}" def clean_text(text): """ Raw PDF text ko saaf karta hai. Jaise kapde dhote hain - gandagi (extra spaces, symbols) hata do! """ # Multiple spaces ko ek space mein badlo text = re.sub(r" {2,}", " ", text) # 3 se zyada newlines ko 2 mein badlo text = re.sub(r"\n{3,}", "\n\n", text) return text def extract_key_info(text): """ Order ke important details dhundta hai text mein. Jaise highlighter se important lines mark karte hain - yeh function order amount, company, date dhundta hai! """ info = {} # Order amount dhundo (Rs./INR/Crore/Lakh ke saath number) amount_pattern = r"(?:Rs\.?|INR|₹)\s*[\d,]+(?:\.\d+)?\s*(?:crore|lakh|million|cr\.?|L\.?)?" amounts = re.findall(amount_pattern, text, re.IGNORECASE) info["amounts_found"] = amounts[:5] # Pehle 5 amounts # Date dhundo date_pattern = r"\d{1,2}[/-]\d{1,2}[/-]\d{2,4}" dates = re.findall(date_pattern, text) info["dates_found"] = dates[:3] # Order keywords dhundo order_keywords = [ "order received", "letter of award", "contract award", "work order", "purchase order", "letter of intent", "notification of award", "repeat order" ] found_keywords = [kw for kw in order_keywords if kw.lower() in text.lower()] info["order_type"] = found_keywords[0] if found_keywords else "Order" return info