01/05/22 - 11h30

master
ChérifBALDE 2022-05-01 11:40:37 +02:00 committed by cherif
parent ae76aa23a6
commit 9ecac8909f
3 changed files with 127 additions and 5 deletions

View File

@ -1055,7 +1055,6 @@ def get_class_global_search(search_string):
coll_name = dbname['myclass']
val = re.compile(r".*"+search_string+".*")
my_regex = "/.*"+search_string+".*/"
insertObject = []

View File

@ -16,7 +16,8 @@ from datetime import datetime
from pymongo import ReturnDocument
from unidecode import unidecode
import GlobalVariable as MYSY_GV
from serpapi import GoogleSearch
import prj_common as mycommon
def myprint(message = ""):
@ -460,3 +461,93 @@ def Parse_Clean_Search_Text(sentence=None):
myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
return False, ""
'''
Cette fonction va aller cherche la recherche edendue en
utilisant l'api de "serpapi"
'''
def Get_Extended_Result(sentence=None):
try:
list_extended = []
external_code_prefixe = str(datetime.now().timestamp()).replace(".", "")
print("external_code_prefixe = " + str(external_code_prefixe))
status = RunSearchAPI(sentence, external_code_prefixe)
if( status is False):
return True, list_extended
return True, external_code_prefixe
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
return False, ""
'''
Cette fonction lance l'API et enregistre le resultat en base
'''
def RunSearchAPI(search_text=None, external_code_prefixe=None):
try:
if( len(str(search_text).strip()) < 0 ):
return True
params = {
"q": str(search_text),
"hl": "fr",
"gl": "fr",
"num":"20",
"safe": "active",
"google_domain": "google.com",
"api_key": "596cb9a468f8292fcefa6f297444db9c12478685d8734b52efdf8aa53c54fd55"
}
mycommon.myprint("######## PARAM RunSearchAPI = "+str(params))
search = GoogleSearch(params)
results = search.get_dict()
organic_results = results['organic_results']
my_collection = MYSY_GV.YTUBES_dbname['mysyserpapi']
mycommon.myprint("resutlat 1 organic_results ")
cmpt = 0
for val in organic_results:
cmpt = cmpt +1
mydata = {}
print("External_code = " + str(val['title']))
print("Title = "+str(val['title']))
print("url = " + str(val['link']))
print("description = " + str(val['snippet']))
print("mot_cles = " + str(val['snippet_highlighted_words']))
mydata['external_code'] = external_code_prefixe+"_"+str(cmpt)
mydata['title'] = str(val['title'])
mydata['url'] = str(val['link'])
mydata['description'] = str(val['snippet'])
mydata['tags'] = str(val['snippet_highlighted_words'])
mydata['update_daye'] = str(datetime.now())
mydata['rang'] = str(val['position'])
mydata['orign_search_text'] = str(search_text)
mydata['valide'] = "1"
mydata['treated'] = "0"
ret_val = my_collection.find_one_and_update({'url': str(mydata['url'])},
{"$set": mydata},
upsert=True,
return_document=ReturnDocument.AFTER
)
if ret_val and ret_val['_id']:
mycommon.myprint(" Le document de la recherche étendu a bien été ajouté = " + str(ret_val['_id']))
else:
mycommon.myprint(" WARNING : Impossible d'ajouter le document de la recherche étentue " + str(mydata['url']))
return True
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
mycommon.myprint(str(e) + " - Line : " + str(exc_tb.tb_lineno))
return False

View File

@ -517,9 +517,8 @@ def recherche_text_simple(diction):
mycommon.myprint(" pour phrase : #" + search_text + "#, voici la liste des formations")
mycommon.myprint(tab_training)
coll_name = MYSY_GV.dbname['myclass']
'''
la valeur nb_result permettra de savoir si la requete a donnée un resultat.
si ce n'est pas le cas, il faudra à la fin enregistrer la requete avec un result a vide'''
@ -527,7 +526,6 @@ def recherche_text_simple(diction):
insertObject = []
for val in tab_training:
for x in coll_name.find(({"$and": [ {"external_code": str(val) },
certif_crit,lang_crit, type_crit,price_crit,
support_crit, duration_crit ]}
@ -557,8 +555,42 @@ def recherche_text_simple(diction):
x['description'] = val[:MYSY_GV.MAX_CARACT] + " ..."
else:
x['description'] = val[:MYSY_GV.MAX_CARACT]
insertObject.append(JSONEncoder().encode(user))
'''
/!\ Important : Recuperation des elements de la recherche etendue
c'est a dire l'utilisation d'API externe
'''
# aller chercher la recherche etendue et la rajouter ici.
ext_status, external_code_prefixe = mycommon.Get_Extended_Result(search_text)
if(ext_status is True):
exten_coll = MYSY_GV.YTUBES_dbname['mysyserpapi']
for x in exten_coll.find({'external_code': {'$regex': re.compile(r".*" + str(external_code_prefixe) + ".*")}},
{"_id": 0, "valide": 0, }):
nb_result = nb_result + 1
my_recid = {}
my_recid['user_rec_id'] = str(user_recid)
user = x
val = x['description']
if (len(x['description']) > MYSY_GV.MAX_CARACT):
x['description'] = val[:MYSY_GV.MAX_CARACT] + " ..."
else:
x['description'] = val[:MYSY_GV.MAX_CARACT]
x['extented_search'] = "1"
if str(x['url']) not in str(insertObject):
insertObject.append(JSONEncoder().encode(user))
else:
print(str(x['url'])+" existe deja, pas d'ajout à faire ")
#print(insertObject)
''' en cas de resultat vide, enregsitrement de la requete de recherche avec les filtres associé'''