diff --git a/README.md b/README.md index b03334c..f9ee926 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,8 @@ # Google-Search-Parser Simple google search parser, based on https://rapidapi.com/apigeek/api/google-search3 api + +# Setup +- Sign up on https://rapidapi.com/ website +- Subscribe to https://rapidapi.com/apigeek/api/google-search3/ api (Don't worry, it's free) +- Insert api key from https://rapidapi.com/ to `config.json` +- Run `main.py` file \ No newline at end of file diff --git a/classes/engine.py b/classes/engine.py new file mode 100644 index 0000000..68a2046 --- /dev/null +++ b/classes/engine.py @@ -0,0 +1,29 @@ +import json +import requests +from rich import print + +from classes import exceptions + +class SearchEngine(): + def __init__(self, user_request, API_KEY): + self.__user_request = user_request + self.__user_request = self.__user_request.replace(" ", "+") + self.__url = f"https://google-search3.p.rapidapi.com/api/v1/search/q={self.__user_request}" + self.__headers = { + "X-User-Agent": "desktop", + "X-Proxy-Location": "EU", + "X-RapidAPI-Host": "google-search3.p.rapidapi.com", + "X-RapidAPI-Key": API_KEY + } + self.__response = requests.request("GET", self.__url, headers=self.__headers) + self.__json_data = json.loads(self.__response.text).get("results") + + + def parse(self): + if self.__json_data is None: + raise(exceptions.MissingApiKey("\n\nYou are don't have API_KEY! Please, get it in https://rapidapi.com/, and subscribe to https://rapidapi.com/apigeek/api/google-search3/ api!")) + else: + for result in self.__json_data: + print(f"[b blue]{result.get('title')}\n" + f"[purple]{result.get('link')}\n" + f"[green]{result.get('description')}\n\n") \ No newline at end of file diff --git a/classes/exceptions.py b/classes/exceptions.py new file mode 100644 index 0000000..cd70c4e --- /dev/null +++ b/classes/exceptions.py @@ -0,0 +1,2 @@ +class MissingApiKey(Exception): + pass \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..4739068 --- /dev/null +++ b/config.json @@ -0,0 +1,3 @@ +{ + "api_key": "INSERT API KEY HERE" +} \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..3e82675 --- /dev/null +++ b/main.py @@ -0,0 +1,10 @@ +from classes import engine +import json + +if __name__ == '__main__': + with open('config.json') as f: + data = json.load(f) + api_key = data.get('api_key') + + search = engine.SearchEngine(input("Search request: "), api_key) + search.parse() \ No newline at end of file