Commit
·
71de332
1
Parent(s):
bcf12b6
fix Adaptação Chatbot - Sicoob UI
Browse files- drive_search.py +77 -0
- requirements.txt +1 -2
drive_search.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from googleapiclient.discovery import build
|
2 |
+
from google.oauth2.service_account import Credentials
|
3 |
+
from fuzzywuzzy import process # Importando a biblioteca fuzzywuzzy
|
4 |
+
import os
|
5 |
+
|
6 |
+
SCOPES = ["https://www.googleapis.com/auth/drive.readonly"]
|
7 |
+
FOLDER_ID = "1hqfPQnsVL2Ld8hu0GRIqcuOp-eDz-CAX" # ID da pasta que você quer buscar
|
8 |
+
|
9 |
+
SERVICE_ACCOUNT_FILE = os.path.join(os.getcwd(), "./files/credenciais.json")
|
10 |
+
def authenticate_drive():
|
11 |
+
"""Autentica no Google Drive usando uma conta de serviço."""
|
12 |
+
credentials = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
|
13 |
+
service = build("drive", "v3", credentials=credentials)
|
14 |
+
return service
|
15 |
+
|
16 |
+
def list_files_recursive(service, folder_id, path=""):
|
17 |
+
"""Lista todos os arquivos e subpastas no Google Drive de forma recursiva."""
|
18 |
+
query = f"'{folder_id}' in parents and trashed = false"
|
19 |
+
response = service.files().list(
|
20 |
+
q=query,
|
21 |
+
spaces="drive",
|
22 |
+
fields="files(id, name, mimeType, parents)",
|
23 |
+
).execute()
|
24 |
+
|
25 |
+
files = response.get("files", [])
|
26 |
+
all_files = []
|
27 |
+
|
28 |
+
for file in files:
|
29 |
+
# caminho completo
|
30 |
+
current_path = f"{path}/{file['name']}"
|
31 |
+
all_files.append({"id": file["id"], "name": file["name"], "path": current_path})
|
32 |
+
|
33 |
+
# buscar recursivamente
|
34 |
+
if file["mimeType"] == "application/vnd.google-apps.folder":
|
35 |
+
all_files.extend(list_files_recursive(service, file["id"], current_path))
|
36 |
+
|
37 |
+
return all_files
|
38 |
+
|
39 |
+
def find_file_by_name(files, search_name):
|
40 |
+
"""Encontra um arquivo com nome aproximado utilizando fuzzy matching."""
|
41 |
+
# Usando fuzzywuzzy para encontrar o arquivo mais próximo
|
42 |
+
file_names = [file["name"] for file in files]
|
43 |
+
best_match, score = process.extractOne(search_name, file_names)
|
44 |
+
|
45 |
+
if score >= 80: # Ajuste a pontuação mínima de correspondência (por exemplo, 80%)
|
46 |
+
matching_files = [file for file in files if file["name"] == best_match]
|
47 |
+
return matching_files
|
48 |
+
else:
|
49 |
+
return []
|
50 |
+
|
51 |
+
def search_file_in_drive(search_name):
|
52 |
+
"""Procura o arquivo mais relevante no Google Drive e retorna o link."""
|
53 |
+
service = authenticate_drive()
|
54 |
+
print("Autenticado com sucesso no Google Drive!")
|
55 |
+
|
56 |
+
# Listar arquivos na pasta e subpastas
|
57 |
+
files = list_files_recursive(service, FOLDER_ID)
|
58 |
+
|
59 |
+
if not files:
|
60 |
+
print("Nenhum arquivo encontrado na pasta!")
|
61 |
+
return None # Retorna None se nenhum arquivo for encontrado
|
62 |
+
|
63 |
+
print(f"Total de arquivos encontrados: {len(files)}")
|
64 |
+
|
65 |
+
# Encontrar o arquivo com nome aproximado
|
66 |
+
matching_files = find_file_by_name(files, search_name)
|
67 |
+
|
68 |
+
if matching_files:
|
69 |
+
best_file = matching_files[0] # Pega o arquivo mais relevante (primeiro da lista)
|
70 |
+
link = f"https://drive.google.com/file/d/{best_file['id']}/view"
|
71 |
+
print(f"Arquivo encontrado: {best_file['name']}")
|
72 |
+
print(f"Link: {link}")
|
73 |
+
return link # Retorna o link do arquivo encontrado
|
74 |
+
else:
|
75 |
+
print(f"Nenhum arquivo encontrado com nome aproximado '{search_name}'.")
|
76 |
+
return None # Retorna None se nenhum arquivo for encontrado
|
77 |
+
|
requirements.txt
CHANGED
@@ -7,5 +7,4 @@ flask==3.1.0
|
|
7 |
streamlit==1.41.1
|
8 |
streamlit-authenticator==0.4.1
|
9 |
python-levenshtein==0.26.1
|
10 |
-
streamlit_feedback
|
11 |
-
drive_search
|
|
|
7 |
streamlit==1.41.1
|
8 |
streamlit-authenticator==0.4.1
|
9 |
python-levenshtein==0.26.1
|
10 |
+
streamlit_feedback
|
|