File size: 3,424 Bytes
ea349e1
 
 
 
 
abf5754
d52ce34
abf5754
ea349e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abf5754
d52ce34
abf5754
d52ce34
abf5754
d52ce34
 
 
ea349e1
 
 
abf5754
ea349e1
 
 
d52ce34
abf5754
ea349e1
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from flask import Flask, render_template, request, jsonify
from time import sleep
from bs4 import BeautifulSoup
from requests import get
import urllib

app = Flask(__name__)

def _req(term, results, lang, start, proxies, timeout, safe, ssl_verify):
    resp = get(
        url="https://www.google.com/search",
        headers={
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.62"
        },
        params={
            "q": term,
            "num": results + 2,  # Prevents multiple requests
            "hl": lang,
            "start": start,
            "safe": safe,
        },
        proxies=proxies,
        timeout=timeout,
        verify=ssl_verify,
    )
    resp.raise_for_status()
    return resp


class SearchResult:
    def __init__(self, url, title, description):
        self.url = url
        self.title = title
        self.description = description

    def __repr__(self):
        return f"SearchResult(url={self.url}, title={self.title}, description={self.description})"

    def to_dict(self):  # Add a method to convert to a dictionary
        return {
            "url": self.url,
            "title": self.title,
            "description": self.description,
        }


def search(term, num_results=10, lang="en", proxy=None, advanced=False, sleep_interval=0, timeout=5, safe="active", ssl_verify=None):
    """Search the Google search engine"""

    escaped_term = urllib.parse.quote_plus(term)  # make 'site:xxx.xxx.xxx ' works.

    # Proxy
    proxies = None
    if proxy:
        if proxy.startswith("https"):
            proxies = {"https": proxy}
        else:
            proxies = {"http": proxy}

    # Fetch
    start = 0
    results = []
    while start < num_results:
        # Send request
        resp = _req(escaped_term, num_results - start,
                    lang, start, proxies, timeout, safe, ssl_verify)

        # Parse
        soup = BeautifulSoup(resp.text, "html.parser")
        result_block = soup.find_all("div", attrs={"class": "g"})
        if len(result_block) == 0:
            start += 1
        for result in result_block:
            # Find link, title, description
            link = result.find("a", href=True)
            title = result.find("h3")
            description_box = result.find(
                "div", {"style": "-webkit-line-clamp:2"})
            if description_box:
                description = description_box.text
                if link and title and description:
                    start += 1
                    if advanced:
                        results.append(SearchResult(link["href"], title.text, description))
                    else:
                        results.append(link["href"])
        sleep(sleep_interval)

    return results

@app.route("/", methods=["GET", "POST"])
def index():
    return render_template("index.html")

@app.route("/api/search", methods=["GET"])
def perform_search():
    search_query = request.args.get("q")
    max_results = int(request.args.get("max_results", 10))  # Default to 10 results

    search_results = search(term=search_query, num_results=max_results, advanced=True)

    # Convert search results to a list of dictionaries
    results_json = [result.to_dict() for result in search_results]
    return jsonify(results_json) 

if __name__ == "__main__":
    app.run(debug=True)