Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
TiDurak committed May 27, 2022
1 parent 56b4680 commit d9d887b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions classes/engine.py
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 2 additions & 0 deletions classes/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class MissingApiKey(Exception):
pass
3 changes: 3 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"api_key": "INSERT API KEY HERE"
}
10 changes: 10 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit d9d887b

Please sign in to comment.