This repository has been archived by the owner on Apr 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
google.py
115 lines (93 loc) · 2.72 KB
/
google.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
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
108
109
110
111
112
113
114
115
"""
Get suggestions from Google
"""
from albertv0 import *
from os import path
import requests
import lxml.html
import json
__iid__ = 'PythonInterface/v0.1'
__prettyname__ = 'Google Suggestions'
__version__ = '1.0'
__trigger__ = 'gg '
__author__ = 'Angelo Gazzola'
__dependencies__ = ['lxml', 'cssselect']
__icon__ = path.dirname(__file__) + '/icons/Google.png'
MAX_LINE_LENGTH = 75
REQUEST_HEADERS = {
'User-Agent': (
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)'
' Chrome/62.0.3202.62 Safari/537.36'
)
}
session = requests.Session()
session.trust_env = False
class SuggestionItem(Item):
def __init__(self, suggestion):
super().__init__(
id=str(hash(suggestion)),
icon=__icon__,
text=suggestion,
completion=__trigger__ + suggestion + '_',
actions=[
UrlAction(
'Search on Google',
'https://google.com/search?q={}'.format(suggestion)
)
]
)
class ResultItem(Item):
def __init__(self, result, query):
super().__init__(
id=result[1],
icon=__icon__,
text=result[0],
subtext=result[1] + '\n' + result[2],
completion=__trigger__ + query + '_',
actions=[UrlAction('Search on Google', result[1])]
)
def search(query):
response = session.get('https://google.com/search',
headers=REQUEST_HEADERS,
params={
'sourceid': 'chrome',
'hl': 'en',
'q': query,
}
)
html = lxml.html.fromstring(response.text)
results = html.cssselect('.rc')
items = []
for result in results:
title = result.cssselect('h3 > a')[0]
url = title.get('href')
description = result.cssselect('.s .st')
formatted_description = []
if len(description) > 0:
description = description[0].text_content()
for i in range(MAX_LINE_LENGTH, len(description), MAX_LINE_LENGTH):
formatted_description.append(description[i-MAX_LINE_LENGTH:i])
formatted_description.append(description[i:-1])
items.append((title.text, url, '\n'.join(formatted_description)))
return [ResultItem(r, query) for r in items]
def suggest(query):
response = session.get('https://clients1.google.com/complete/search',
headers=REQUEST_HEADERS,
params={
'client': 'firefox',
'output': 'toolbar',
'hl': 'en',
'q': query,
}
)
suggestions = json.loads(response.text)[1]
return [SuggestionItem(suggestion) for suggestion in suggestions]
def handleQuery(query):
if query.isTriggered and len(query.string) > 0:
if query.string[-1] == '_':
items = search(query.string[:-1])
else:
items = suggest(query.string)
items.insert(0, SuggestionItem(query.string))
return items
return [Item(icon=__icon__, text='Google')]