-
Notifications
You must be signed in to change notification settings - Fork 1
/
Metaphor.py
38 lines (36 loc) · 1.57 KB
/
Metaphor.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
# Loading the necessary packages
from metaphor_python import Metaphor
from bs4 import BeautifulSoup
from datetime import date,timedelta
class MetaphorSearch:
def __init__(self,api_key):
self.metaphor = Metaphor(api_key)
#print('API Connection successful!')
def search(self, search_value,keyword = False):
if keyword == False:
number_results = 8
search_response = self.metaphor.search(
search_value,
num_results=number_results,
start_crawl_date = (date.today()).strftime("%Y-%m-%d"),
use_autoprompt=True)
#print(len(search_response.results))
if len(search_response.results) == 0:
search_response = self.metaphor.search(
search_value,
num_results=number_results,
start_published_date = (date.today()-timedelta(days=7)).strftime("%Y-%m-%d"),
use_autoprompt=True, )
else:
number_results = 8
print('Now Keyword=True')
search_response = self.metaphor.search(
search_value,
num_results=number_results,
start_crawl_date = (date.today()).strftime("%Y-%m-%d"),
type = 'keyword',)
contents_response = search_response.get_contents()
documents = [ BeautifulSoup(content.extract.replace('<|endoftext|>',''), 'html.parser').get_text() for content in contents_response.contents]
#print(len(documents))
return ' '.join(documents)
#return documents