File size: 2,877 Bytes
1bb5ccb | 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 | 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
|