2020-02-21 12:05:41 +01:00
|
|
|
import json
|
2021-03-10 01:04:12 +01:00
|
|
|
import random
|
|
|
|
import time
|
2020-02-21 12:05:41 +01:00
|
|
|
try:
|
2021-03-09 23:46:11 +01:00
|
|
|
from googleapi import google
|
2020-02-21 12:05:41 +01:00
|
|
|
except ImportError:
|
2020-02-26 11:59:14 +01:00
|
|
|
print("GoogleAPI not installed. Command : pip install git+https://github.com/abenassi/Google-Search-API")
|
2020-02-21 12:05:41 +01:00
|
|
|
|
|
|
|
misperrors = {'error': 'Error'}
|
|
|
|
mispattributes = {'input': ['url'], 'output': ['text']}
|
2024-08-12 11:23:10 +02:00
|
|
|
moduleinfo = {
|
|
|
|
'author': 'Oun & Gindt',
|
|
|
|
'module-type': ['hover'],
|
|
|
|
'name': 'Google Search',
|
|
|
|
'description': 'An expansion hover module to expand google search information about an URL',
|
|
|
|
'version': '1.0',
|
|
|
|
'logo': 'google.png',
|
|
|
|
'requirements': ['The python Google Search API library'],
|
|
|
|
'features': 'The module takes an url as input to query the Google search API. The result of the query is then return as raw text.',
|
|
|
|
'references': ['https://github.com/abenassi/Google-Search-API'],
|
|
|
|
'input': 'An url attribute.',
|
|
|
|
'output': 'Text containing the result of a Google search on the input url.',
|
|
|
|
}
|
2020-02-21 12:05:41 +01:00
|
|
|
|
2021-03-18 19:22:26 +01:00
|
|
|
|
2021-03-10 01:04:12 +01:00
|
|
|
def sleep(retry):
|
|
|
|
time.sleep(random.uniform(0, min(40, 0.01 * 2 ** retry)))
|
2020-02-21 12:05:41 +01:00
|
|
|
|
2021-03-18 19:22:26 +01:00
|
|
|
|
2020-02-21 12:05:41 +01:00
|
|
|
def handler(q=False):
|
2020-02-26 11:59:14 +01:00
|
|
|
if q is False:
|
|
|
|
return False
|
|
|
|
request = json.loads(q)
|
|
|
|
if not request.get('url'):
|
|
|
|
return {'error': "Unsupported attributes type"}
|
|
|
|
num_page = 1
|
|
|
|
res = ""
|
2021-03-10 01:04:12 +01:00
|
|
|
# The googleapi module sets a random useragent. The output depends on the useragent.
|
|
|
|
# It's better to retry 3 times.
|
|
|
|
for retry in range(3):
|
|
|
|
search_results = google.search(request['url'], num_page)
|
|
|
|
if len(search_results) > 0:
|
|
|
|
break
|
|
|
|
sleep(retry)
|
|
|
|
for i, search_result in enumerate(search_results):
|
2020-02-26 11:59:14 +01:00
|
|
|
res += "("+str(i+1)+")" + '\t'
|
2021-03-10 01:04:12 +01:00
|
|
|
res += json.dumps(search_result.description, ensure_ascii=False)
|
2020-02-26 11:59:14 +01:00
|
|
|
res += '\n\n'
|
|
|
|
return {'results': [{'types': mispattributes['output'], 'values':res}]}
|
2020-02-21 12:05:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def introspection():
|
2020-02-26 11:59:14 +01:00
|
|
|
return mispattributes
|
2020-02-21 12:05:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def version():
|
2020-02-26 11:59:14 +01:00
|
|
|
return moduleinfo
|