-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (49 loc) · 1.47 KB
/
main.py
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
from serpapi import GoogleSearch
import os
import json
import scratchattach as scratch3
from dotenv import load_dotenv
load_dotenv()
scratch_token = os.getenv('SCRATCH_TOKEN')
scratch_user = os.getenv('SCRATCH_USERNAME')
scratch_project = os.getenv('SCRATCH_PROJECT')
api_key = os.getenv('API_KEY')
session = scratch3.Session(scratch_token, username=scratch_user)
conn = session.connect_cloud(scratch_project)
client = scratch3.CloudRequests(conn)
@client.request
def ping():
return "pong"
@client.request
def searches():
search = GoogleSearch({"api_key": api_key})
account = search.get_account()
return account["plan_searches_left"]
@client.event
def on_ready():
print("Request handler is running")
@client.request
def search(question, username):
search = GoogleSearch({"api_key": api_key})
account = search.get_account()
if (account["plan_searches_left"] == 0):
return "('Max Searches Reached!')"
params = {
"q": question,
"hl": "en",
"gl": "us",
"google_domain": "google.com",
"api_key": api_key
}
print(username + " searched " + question)
print()
search = GoogleSearch(params)
results = search.get_dict()
organic_results = results.get('organic_results', [])
results_list = []
for result in organic_results[:10]:
title = result.get('title', '')
link = result.get('link', '')
results_list.append((title, link))
return results_list
client.run()