Quant0 commited on
Commit
5e569f0
·
verified ·
1 Parent(s): c14b493

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +104 -0
utils.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ # Funciones auxiliares para limpieza, conversión y scraping
3
+
4
+ import re
5
+ import numpy as np
6
+ import pandas as pd
7
+ import requests
8
+ from bs4 import BeautifulSoup
9
+ from rapidfuzz import process
10
+ from config import SCRAPERAPI_KEY, YAHOO_FINANCE_SEARCH_URL
11
+
12
+ def limpiar_texto(texto):
13
+ """Elimina caracteres especiales y espacios extra."""
14
+ texto_limpio_parcial = re.sub(r'[^a-zA-Z0-9.,\\s]', '', texto)
15
+ texto_final = texto_limpio_parcial.strip()
16
+ return texto_final
17
+
18
+ def expand_money(val):
19
+ """Convierte strings de dinero (ej. '$1.2B') a valores flotantes."""
20
+ if pd.isna(val):
21
+ return np.nan
22
+ val = str(val).replace('$', '').replace(',', '').upper()
23
+ try:
24
+ if 'B' in val:
25
+ return float(val.replace('B', '')) * 1_000_000_000
26
+ elif 'M' in val:
27
+ return float(val.replace('M', '')) * 1_000_000
28
+ elif 'K' in val:
29
+ return float(val.replace('K', '')) * 1_000
30
+ else:
31
+ return float(val)
32
+ except:
33
+ return np.nan
34
+
35
+ def extract_file_id(url):
36
+ """Extrae el ID del archivo de una URL de Google Drive."""
37
+ match = re.search(r'/d/([a-zA-Z0-9_-]+)', url)
38
+ if match:
39
+ return match.group(1)
40
+ return None
41
+
42
+ def descargar_tabla_web(url, indice_tabla=0):
43
+ """Descarga una tabla de una URL usando ScraperAPI para evitar bloqueos."""
44
+ api_url = f"http://api.scraperapi.com?api_key={SCRAPERAPI_KEY}&url={url}"
45
+ try:
46
+ response = requests.get(api_url)
47
+ response.raise_for_status()
48
+ soup = BeautifulSoup(response.text, 'html.parser')
49
+ tables = soup.find_all('table')
50
+ if len(tables) > indice_tabla:
51
+ table = tables[indice_tabla]
52
+ else:
53
+ print(f"No se encontró la tabla con índice {indice_tabla} en la página: {url}.")
54
+ return None
55
+ headers = [th.text.strip() for th in table.find_all('th')]
56
+ data = []
57
+ for row in table.find_all('tr'):
58
+ cols = row.find_all('td')
59
+ cols = [ele.text.strip() for ele in cols]
60
+ if cols:
61
+ data.append(cols)
62
+ if headers and data and len(headers) == len(data[0]):
63
+ df = pd.DataFrame(data, columns=headers)
64
+ else:
65
+ df = pd.DataFrame(data)
66
+ return df
67
+ except requests.exceptions.RequestException as e:
68
+ print(f"Error al acceder a la URL: {e}")
69
+ return None
70
+ except Exception as e:
71
+ print(f"Ocurrió un error inesperado: {e}")
72
+ return None
73
+
74
+ def find_company_online(query, priority_tickers=None):
75
+ """Busca una empresa en Yahoo Finance y devuelve (nombre, ticker)."""
76
+ query_clean = query.strip()
77
+ url = f"{YAHOO_FINANCE_SEARCH_URL}?q={query_clean}&quotesCount=10&newsCount=0"
78
+ headers = {"User-Agent": "Mozilla/5.0"}
79
+ r = requests.get(url, headers=headers)
80
+ data = r.json()
81
+ if "quotes" not in data or not data["quotes"]:
82
+ return None, None
83
+ results = [
84
+ (item.get("shortname") or item.get("longname") or item.get("symbol"), item["symbol"])
85
+ for item in data["quotes"]
86
+ if "symbol" in item
87
+ ]
88
+ if priority_tickers:
89
+ for name, ticker in results:
90
+ if ticker in priority_tickers:
91
+ return name, ticker
92
+ tickers = [sym for _, sym in results]
93
+ best_ticker_match = process.extractOne(query_clean.upper(), tickers, score_cutoff=80)
94
+ if best_ticker_match:
95
+ ticker = best_ticker_match[0]
96
+ name = next(name for name, sym in results if sym == ticker)
97
+ return name, ticker
98
+ names = [name for name, _ in results]
99
+ best_name_match = process.extractOne(query_clean, names, score_cutoff=60)
100
+ if best_name_match:
101
+ name = best_name_match[0]
102
+ ticker = next(sym for nm, sym in results if nm == name)
103
+ return name, ticker
104
+ return results[0] if results else (None, None)