()
| 19651 | |
| 19652 | @app.route('/api/web_dork', methods=['POST']) |
| 19653 | def api_web_dork(): |
| 19654 | from bs4 import BeautifulSoup |
| 19655 | from playwright.sync_api import sync_playwright |
| 19656 | import urllib.parse |
| 19657 | |
| 19658 | query = request.json.get('query', '').strip() |
| 19659 | if not query: |
| 19660 | return jsonify({"results": []}) |
| 19661 | |
| 19662 | results = [] |
| 19663 | |
| 19664 | try: |
| 19665 | with sync_playwright() as p: |
| 19666 | browser = p.chromium.launch(headless=True, args=["--disable-blink-features=AutomationControlled"]) |
| 19667 | context = browser.new_context( |
| 19668 | user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", |
| 19669 | viewport={"width": 1280, "height": 720} |
| 19670 | ) |
| 19671 | |
| 19672 | # Iniezione cookie per bypassare il pop-up di consenso europeo di Google |
| 19673 | context.add_cookies([{"name": "CONSENT", "value": "YES+cb.20230501-14-p0.it+FX+874", "domain": ".google.com", "path": "/"}]) |
| 19674 | page = context.new_page() |
| 19675 | |
| 19676 | # --- GOOGLE CSE --- |
| 19677 | try: |
| 19678 | page.goto(f"https://cse.google.com/cse?cx=d28c23ec014bd4cca&q={urllib.parse.quote(query)}", wait_until="domcontentloaded", timeout=15000) |
| 19679 | |
| 19680 | for _ in range(3): |
| 19681 | try: page.wait_for_selector('.gsc-webResult', timeout=8000) |
| 19682 | except: pass |
| 19683 | |
| 19684 | soup = BeautifulSoup(page.content(), 'html.parser') |
| 19685 | for res in soup.find_all('div', class_='gsc-webResult'): |
| 19686 | a = res.find('a', class_='gs-title') |
| 19687 | if a and a.get('href'): |
| 19688 | title = a.text.strip() |
| 19689 | url_node = a.get('data-ctorig') or a.get('href') |
| 19690 | if url_node and url_node.startswith('http'): |
| 19691 | snippet_div = res.find('div', class_='gs-snippet') |
| 19692 | snippet = snippet_div.text.strip() if snippet_div else "" |
| 19693 | if title: |
| 19694 | results.append({ |
| 19695 | "engine": "Google", |
| 19696 | "title": title, |
| 19697 | "snippet": snippet, |
| 19698 | "url": url_node |
| 19699 | }) |
| 19700 | try: |
| 19701 | has_next = page.evaluate('''() => { |
| 19702 | let curr = document.querySelector('.gsc-cursor-current-page'); |
| 19703 | if(curr && curr.nextSibling) { |
| 19704 | curr.nextSibling.click(); |
| 19705 | return true; |
| 19706 | } |
| 19707 | return false; |
| 19708 | }''') |
| 19709 | if has_next: |
| 19710 | page.wait_for_timeout(2000) |
nothing calls this directly
no test coverage detected