Spaces:
Sleeping
Sleeping
adding more sites in bot
Browse files- __pycache__/api.cpython-312.pyc +0 -0
- __pycache__/scraper.cpython-312.pyc +0 -0
- scraper.py +135 -33
__pycache__/api.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/api.cpython-312.pyc and b/__pycache__/api.cpython-312.pyc differ
|
|
|
__pycache__/scraper.cpython-312.pyc
ADDED
|
Binary file (9.22 kB). View file
|
|
|
scraper.py
CHANGED
|
@@ -2,6 +2,11 @@ import os
|
|
| 2 |
import asyncio
|
| 3 |
from playwright.async_api import async_playwright
|
| 4 |
from supabase import create_client, Client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# --- 1. SETUP YOUR SUPABASE VAULT HERE ---
|
| 7 |
SUPABASE_URL = os.environ.get("SUPABASE_URL")
|
|
@@ -10,8 +15,119 @@ SUPABASE_KEY = os.environ.get("SUPABASE_KEY")
|
|
| 10 |
# Connect to the database
|
| 11 |
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
async def scrape_and_upload():
|
| 14 |
-
print("π₯· Booting up GovBridge Bot (Upload Mode)...")
|
| 15 |
|
| 16 |
async with async_playwright() as p:
|
| 17 |
browser = await p.chromium.launch(
|
|
@@ -26,42 +142,28 @@ async def scrape_and_upload():
|
|
| 26 |
# Apply the stealth mask to hide from the firewall
|
| 27 |
await context.add_init_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
|
| 28 |
page = await context.new_page()
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
await page.goto("https://www.myscheme.gov.in/search")
|
| 32 |
-
await page.wait_for_timeout(5000)
|
| 33 |
-
|
| 34 |
-
print("π― Target acquired! Ripping the text...")
|
| 35 |
-
titles = await page.locator("h2").all_inner_texts()
|
| 36 |
-
schemes = [t.strip() for t in titles if len(t.strip()) > 5 and "Filter By" not in t]
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
first_scheme_title = schemes[0]
|
| 41 |
-
first_scheme_ministry = schemes[1]
|
| 42 |
-
|
| 43 |
-
print(f"\n--- INITIATING DATABASE UPLOAD ---")
|
| 44 |
-
print(f"π€ Uploading: {first_scheme_title}...")
|
| 45 |
-
|
| 46 |
-
# Package the data to match our official_documents table perfectly
|
| 47 |
-
data_to_save = {
|
| 48 |
-
"title": first_scheme_title,
|
| 49 |
-
"department": first_scheme_ministry,
|
| 50 |
-
"document_type": "Auto-Scraped Scheme",
|
| 51 |
-
"file_url": "https://www.myscheme.gov.in/search",
|
| 52 |
-
"authentic_source_url": "https://www.myscheme.gov.in"
|
| 53 |
-
}
|
| 54 |
-
|
| 55 |
try:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
print(f"β Upload failed: {e}")
|
| 61 |
-
|
| 62 |
-
else:
|
| 63 |
-
print("β Didn't find enough data on the page to upload.")
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
print("\nShutting down bot.")
|
| 66 |
await browser.close()
|
| 67 |
|
|
|
|
| 2 |
import asyncio
|
| 3 |
from playwright.async_api import async_playwright
|
| 4 |
from supabase import create_client, Client
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
# Setup logging
|
| 8 |
+
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
# --- 1. SETUP YOUR SUPABASE VAULT HERE ---
|
| 12 |
SUPABASE_URL = os.environ.get("SUPABASE_URL")
|
|
|
|
| 15 |
# Connect to the database
|
| 16 |
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 17 |
|
| 18 |
+
# --- MULTI-SITE CONFIGURATION ---
|
| 19 |
+
GOVERNMENT_SITES = [
|
| 20 |
+
{
|
| 21 |
+
"name": "MyScheme.gov.in",
|
| 22 |
+
"url": "https://www.myscheme.gov.in/search",
|
| 23 |
+
"source": "MyScheme Portal",
|
| 24 |
+
"selector": "h2",
|
| 25 |
+
"extract_type": "myscheme"
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"name": "Ministry of Education - Schemes",
|
| 29 |
+
"url": "https://www.education.gov.in/",
|
| 30 |
+
"source": "Ministry of Education",
|
| 31 |
+
"selector": "h3, h2",
|
| 32 |
+
"extract_type": "generic"
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"name": "AICTE Schemes",
|
| 36 |
+
"url": "https://portal.aicte-india.org/",
|
| 37 |
+
"source": "AICTE",
|
| 38 |
+
"selector": "h2, .scheme-title",
|
| 39 |
+
"extract_type": "generic"
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"name": "Ministry of Rural Development",
|
| 43 |
+
"url": "https://pmay-urb.gov.in/",
|
| 44 |
+
"source": "Ministry of Rural Development",
|
| 45 |
+
"selector": "h2, h3",
|
| 46 |
+
"extract_type": "generic"
|
| 47 |
+
}
|
| 48 |
+
]
|
| 49 |
+
|
| 50 |
+
async def scrape_myscheme(page, site_config):
|
| 51 |
+
"""Specialized scraper for MyScheme portal"""
|
| 52 |
+
try:
|
| 53 |
+
logger.info(f"π― Scraping {site_config['name']}...")
|
| 54 |
+
await page.goto(site_config["url"], timeout=15000)
|
| 55 |
+
await page.wait_for_timeout(3000)
|
| 56 |
+
|
| 57 |
+
titles = await page.locator("h2").all_inner_texts()
|
| 58 |
+
schemes = [t.strip() for t in titles if len(t.strip()) > 5 and "Filter By" not in t]
|
| 59 |
+
|
| 60 |
+
results = []
|
| 61 |
+
if len(schemes) >= 2:
|
| 62 |
+
for i in range(0, len(schemes) - 1, 2):
|
| 63 |
+
try:
|
| 64 |
+
data_to_save = {
|
| 65 |
+
"title": schemes[i],
|
| 66 |
+
"department": schemes[i + 1] if i + 1 < len(schemes) else "Unknown",
|
| 67 |
+
"document_type": "Government Scheme",
|
| 68 |
+
"file_url": site_config["url"],
|
| 69 |
+
"authentic_source_url": site_config["source"],
|
| 70 |
+
"scrape_source": site_config["name"]
|
| 71 |
+
}
|
| 72 |
+
supabase.table("official_documents").insert(data_to_save).execute()
|
| 73 |
+
logger.info(f"β
Uploaded: {schemes[i]}")
|
| 74 |
+
results.append(True)
|
| 75 |
+
except Exception as e:
|
| 76 |
+
logger.error(f"β Failed to upload {schemes[i]}: {str(e)}")
|
| 77 |
+
results.append(False)
|
| 78 |
+
return results
|
| 79 |
+
else:
|
| 80 |
+
logger.warning(f"β οΈ Not enough data found on {site_config['name']}")
|
| 81 |
+
return []
|
| 82 |
+
except Exception as e:
|
| 83 |
+
logger.error(f"β Error scraping {site_config['name']}: {str(e)}")
|
| 84 |
+
return []
|
| 85 |
+
|
| 86 |
+
async def scrape_generic(page, site_config):
|
| 87 |
+
"""Generic scraper for government websites"""
|
| 88 |
+
try:
|
| 89 |
+
logger.info(f"π― Scraping {site_config['name']}...")
|
| 90 |
+
await page.goto(site_config["url"], timeout=15000)
|
| 91 |
+
await page.wait_for_timeout(3000)
|
| 92 |
+
|
| 93 |
+
selectors = site_config["selector"].split(", ")
|
| 94 |
+
all_text = []
|
| 95 |
+
|
| 96 |
+
for selector in selectors:
|
| 97 |
+
try:
|
| 98 |
+
elements = await page.locator(selector).all_inner_texts()
|
| 99 |
+
all_text.extend([t.strip() for t in elements if len(t.strip()) > 5])
|
| 100 |
+
except:
|
| 101 |
+
pass
|
| 102 |
+
|
| 103 |
+
results = []
|
| 104 |
+
if all_text:
|
| 105 |
+
for i, text in enumerate(all_text[:10]): # Limit to first 10 items
|
| 106 |
+
try:
|
| 107 |
+
data_to_save = {
|
| 108 |
+
"title": text,
|
| 109 |
+
"department": site_config["source"],
|
| 110 |
+
"document_type": "Government Content",
|
| 111 |
+
"file_url": site_config["url"],
|
| 112 |
+
"authentic_source_url": site_config["source"],
|
| 113 |
+
"scrape_source": site_config["name"]
|
| 114 |
+
}
|
| 115 |
+
supabase.table("official_documents").insert(data_to_save).execute()
|
| 116 |
+
logger.info(f"β
Uploaded from {site_config['name']}: {text[:50]}...")
|
| 117 |
+
results.append(True)
|
| 118 |
+
except Exception as e:
|
| 119 |
+
logger.warning(f"β οΈ Skipped duplicate or error: {str(e)[:50]}")
|
| 120 |
+
results.append(False)
|
| 121 |
+
return results
|
| 122 |
+
else:
|
| 123 |
+
logger.warning(f"β οΈ No data found on {site_config['name']}")
|
| 124 |
+
return []
|
| 125 |
+
except Exception as e:
|
| 126 |
+
logger.error(f"β Error scraping {site_config['name']}: {str(e)}")
|
| 127 |
+
return []
|
| 128 |
+
|
| 129 |
async def scrape_and_upload():
|
| 130 |
+
print("π₯· Booting up GovBridge Multi-Site Bot (Upload Mode)...\n")
|
| 131 |
|
| 132 |
async with async_playwright() as p:
|
| 133 |
browser = await p.chromium.launch(
|
|
|
|
| 142 |
# Apply the stealth mask to hide from the firewall
|
| 143 |
await context.add_init_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
|
| 144 |
page = await context.new_page()
|
| 145 |
+
page.set_default_timeout(15000)
|
| 146 |
|
| 147 |
+
total_uploaded = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
# Scrape all configured sites
|
| 150 |
+
for site_config in GOVERNMENT_SITES:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
try:
|
| 152 |
+
if site_config["extract_type"] == "myscheme":
|
| 153 |
+
results = await scrape_myscheme(page, site_config)
|
| 154 |
+
else:
|
| 155 |
+
results = await scrape_generic(page, site_config)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
+
total_uploaded += sum(1 for r in results if r)
|
| 158 |
+
except Exception as e:
|
| 159 |
+
logger.error(f"β Critical error with {site_config['name']}: {str(e)}")
|
| 160 |
+
|
| 161 |
+
# Respectful delay between requests
|
| 162 |
+
await page.wait_for_timeout(2000)
|
| 163 |
+
|
| 164 |
+
print(f"\n--- π FINAL REPORT ---")
|
| 165 |
+
print(f"β
Total items uploaded: {total_uploaded}")
|
| 166 |
+
print(f"π Sites processed: {len(GOVERNMENT_SITES)}")
|
| 167 |
print("\nShutting down bot.")
|
| 168 |
await browser.close()
|
| 169 |
|