Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
from flask import Flask, request, jsonify
|
| 3 |
+
from selenium import webdriver
|
| 4 |
+
from selenium.webdriver.chrome.options import Options
|
| 5 |
+
from selenium.webdriver.common.by import By
|
| 6 |
+
from selenium.webdriver.common.keys import Keys
|
| 7 |
+
from selenium.webdriver.support.ui import WebDriverWait
|
| 8 |
+
from selenium.webdriver.support import expected_conditions as EC
|
| 9 |
+
import time
|
| 10 |
+
import os
|
| 11 |
+
import logging
|
| 12 |
+
|
| 13 |
+
# Configure logging
|
| 14 |
+
logging.basicConfig(level=logging.INFO)
|
| 15 |
+
logger = logging.getLogger(__name__)
|
| 16 |
+
|
| 17 |
+
app = Flask(__name__)
|
| 18 |
+
|
| 19 |
+
# Configure Chrome options for headless browsing
|
| 20 |
+
def get_chrome_options():
|
| 21 |
+
chrome_options = Options()
|
| 22 |
+
chrome_options.add_argument("--headless")
|
| 23 |
+
chrome_options.add_argument("--no-sandbox")
|
| 24 |
+
chrome_options.add_argument("--disable-dev-shm-usage")
|
| 25 |
+
chrome_options.add_argument("--disable-gpu")
|
| 26 |
+
chrome_options.add_argument("--window-size=1280,720")
|
| 27 |
+
chrome_options.add_argument("--single-process")
|
| 28 |
+
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
|
| 29 |
+
chrome_options.add_argument("--disable-extensions")
|
| 30 |
+
chrome_options.add_argument("--log-level=3")
|
| 31 |
+
chrome_options.add_argument("--output=/dev/null")
|
| 32 |
+
return chrome_options
|
| 33 |
+
|
| 34 |
+
def perform_search(query):
|
| 35 |
+
"""Perform Google Custom Search and return results"""
|
| 36 |
+
driver = None
|
| 37 |
+
try:
|
| 38 |
+
# Set up WebDriver
|
| 39 |
+
chrome_options = get_chrome_options()
|
| 40 |
+
driver = webdriver.Chrome(options=chrome_options)
|
| 41 |
+
|
| 42 |
+
# Navigate to the search page
|
| 43 |
+
driver.get("https://cse.google.com/cse?cx=50696ceaa0f3c435c")
|
| 44 |
+
logger.info(f"Loaded search page for query: {query}")
|
| 45 |
+
|
| 46 |
+
# Wait for search input to be present
|
| 47 |
+
search_input = WebDriverWait(driver, 15).until(
|
| 48 |
+
EC.presence_of_element_located((By.ID, "gsc-i-id1"))
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Enter search query
|
| 52 |
+
search_input.send_keys(query)
|
| 53 |
+
search_input.send_keys(Keys.RETURN)
|
| 54 |
+
logger.info("Submitted search query")
|
| 55 |
+
|
| 56 |
+
# Wait for results to load
|
| 57 |
+
WebDriverWait(driver, 15).until(
|
| 58 |
+
EC.presence_of_element_located((By.CLASS_NAME, "gsc-expansionArea"))
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Additional wait time for stability
|
| 62 |
+
time.sleep(2)
|
| 63 |
+
|
| 64 |
+
# Extract search results
|
| 65 |
+
results_container = driver.find_element(By.CLASS_NAME, "gsc-expansionArea")
|
| 66 |
+
search_results = results_container.find_elements(By.CLASS_NAME, "gsc-webResult")
|
| 67 |
+
logger.info(f"Found {len(search_results)} results")
|
| 68 |
+
|
| 69 |
+
# Process results
|
| 70 |
+
results = []
|
| 71 |
+
for i, result in enumerate(search_results, 1):
|
| 72 |
+
try:
|
| 73 |
+
# Extract title and URL
|
| 74 |
+
title_element = result.find_element(By.CSS_SELECTOR, "a.gs-title")
|
| 75 |
+
title = title_element.text
|
| 76 |
+
url = title_element.get_attribute("href")
|
| 77 |
+
|
| 78 |
+
# Extract snippet
|
| 79 |
+
try:
|
| 80 |
+
snippet = result.find_element(By.CLASS_NAME, "gs-snippet").text
|
| 81 |
+
except:
|
| 82 |
+
snippet = "No snippet available"
|
| 83 |
+
|
| 84 |
+
results.append({
|
| 85 |
+
"position": i,
|
| 86 |
+
"title": title,
|
| 87 |
+
"url": url,
|
| 88 |
+
"snippet": snippet
|
| 89 |
+
})
|
| 90 |
+
|
| 91 |
+
except Exception as e:
|
| 92 |
+
logger.warning(f"Error processing result #{i}: {str(e)}")
|
| 93 |
+
continue
|
| 94 |
+
|
| 95 |
+
return results
|
| 96 |
+
|
| 97 |
+
except Exception as e:
|
| 98 |
+
logger.error(f"Search failed: {str(e)}")
|
| 99 |
+
return {"error": f"Search operation failed: {str(e)}"}
|
| 100 |
+
|
| 101 |
+
finally:
|
| 102 |
+
if driver:
|
| 103 |
+
driver.quit()
|
| 104 |
+
logger.info("WebDriver closed")
|
| 105 |
+
|
| 106 |
+
@app.route('/search', methods=['GET'])
|
| 107 |
+
def search_endpoint():
|
| 108 |
+
"""API endpoint for performing searches"""
|
| 109 |
+
query = request.args.get('q')
|
| 110 |
+
|
| 111 |
+
if not query:
|
| 112 |
+
return jsonify({"error": "Missing search query parameter 'q'"}), 400
|
| 113 |
+
|
| 114 |
+
logger.info(f"Received search request for: {query}")
|
| 115 |
+
results = perform_search(query)
|
| 116 |
+
|
| 117 |
+
if isinstance(results, dict) and "error" in results:
|
| 118 |
+
return jsonify(results), 500
|
| 119 |
+
|
| 120 |
+
return jsonify({
|
| 121 |
+
"query": query,
|
| 122 |
+
"result_count": len(results),
|
| 123 |
+
"results": results
|
| 124 |
+
})
|
| 125 |
+
|
| 126 |
+
@app.route('/')
|
| 127 |
+
def home():
|
| 128 |
+
"""Simple home page that doesn't reveal implementation details"""
|
| 129 |
+
return jsonify({
|
| 130 |
+
"service": "Search API",
|
| 131 |
+
"status": "operational",
|
| 132 |
+
"endpoints": {
|
| 133 |
+
"search": "/search?q=QUERY"
|
| 134 |
+
}
|
| 135 |
+
})
|
| 136 |
+
|
| 137 |
+
if __name__ == '__main__':
|
| 138 |
+
port = int(os.environ.get('PORT', 5000))
|
| 139 |
+
app.run(host='0.0.0.0', port=port)
|