Fix online search sometimes blocking local search

This commit is contained in:
augustin64 2023-10-18 14:45:40 +02:00
parent 1d02292c61
commit 971d54af41

View File

@ -5,12 +5,15 @@ Module implémentant la recherche de partitions par mots-clés
from uuid import uuid4
import urllib.request
import threading
import socket
import os
import googlesearch
from .db import get_db
socket.setdefaulttimeout(5) # Maximum time before we give up on downloading a file (dead url)
def local_search(query, partitions):
"""
@ -60,6 +63,8 @@ def online_search(query, num_queries):
db = get_db()
query = f"partition filetype:pdf {query}"
partitions = []
try:
results = googlesearch.search(
query,
num=num_queries,
@ -90,6 +95,9 @@ def online_search(query, num_queries):
except db.IntegrityError:
pass
except urllib.error.URLError: # Unable to access network
return []
threads = [threading.Thread(target=download_search_result, args=(elem,)) for elem in partitions]
for thread in threads: