Shivam4096 commited on
Commit
17e1628
·
verified ·
1 Parent(s): 325cb74

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. pipeline/peer_selector.py +9 -5
  2. pipeline/resolver.py +31 -4
pipeline/peer_selector.py CHANGED
@@ -104,13 +104,17 @@ def _hf_intelligent_peers(meta: CompanyMeta, log_fn=None) -> List[str]:
104
 
105
  # Extract JSON array using regex
106
  import re, json
107
- match = re.search(r'\[(.*?)\]', text, re.DOTALL)
 
108
  if match:
109
  arr_str = match.group(0)
110
- tickers = json.loads(arr_str)
111
- if isinstance(tickers, list) and all(isinstance(t, str) for t in tickers):
112
- log(f" [OK] HF AI successfully identified highly specific peers: {tickers}")
113
- return tickers
 
 
 
114
  log(" ⚠ HF AI returned invalid format, falling back...")
115
  else:
116
  log(f" ⚠ HF API Error. Falling back...")
 
104
 
105
  # Extract JSON array using regex
106
  import re, json
107
+ # Match anything between [ and ] inclusive, across multiple lines
108
+ match = re.search(r'\[\s*(.*?)\s*\]', text, re.DOTALL)
109
  if match:
110
  arr_str = match.group(0)
111
+ try:
112
+ tickers = json.loads(arr_str)
113
+ if isinstance(tickers, list) and all(isinstance(t, str) for t in tickers):
114
+ log(f" [OK] HF AI successfully identified highly specific peers: {tickers}")
115
+ return tickers
116
+ except json.JSONDecodeError as e:
117
+ log(f" ⚠ HF AI JSON parse error: {e}")
118
  log(" ⚠ HF AI returned invalid format, falling back...")
119
  else:
120
  log(f" ⚠ HF API Error. Falling back...")
pipeline/resolver.py CHANGED
@@ -179,12 +179,11 @@ def _yq_resolve(ticker: str, log_fn=None) -> Optional[CompanyMeta]:
179
 
180
  if isinstance(price_data, str) or isinstance(profile_data, str):
181
  # Yahooquery sometimes returns strings like "No fundamentals data found"
182
- log(f" [ERR] yahooquery returned no data for {ticker}")
183
- return None
184
 
185
  name = price_data.get("longName") or price_data.get("shortName", "")
186
  if not name:
187
- return None
188
 
189
  exchange_code = price_data.get("exchange", "")
190
  currency = price_data.get("currency", "INR")
@@ -217,7 +216,35 @@ def _yq_resolve(ticker: str, log_fn=None) -> Optional[CompanyMeta]:
217
  )
218
  except Exception as e:
219
  log(f" [ERR] yahooquery failed for {ticker}: {e}")
220
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
 
222
 
223
  # -- FMP resolution ------------------------------------------------------------
 
179
 
180
  if isinstance(price_data, str) or isinstance(profile_data, str):
181
  # Yahooquery sometimes returns strings like "No fundamentals data found"
182
+ raise ValueError(f"yahooquery returned string instead of dict: {price_data}")
 
183
 
184
  name = price_data.get("longName") or price_data.get("shortName", "")
185
  if not name:
186
+ raise ValueError(f"yahooquery returned empty name for {ticker}")
187
 
188
  exchange_code = price_data.get("exchange", "")
189
  currency = price_data.get("currency", "INR")
 
216
  )
217
  except Exception as e:
218
  log(f" [ERR] yahooquery failed for {ticker}: {e}")
219
+
220
+ # FALLBACK: If yahooquery fails, try screener.in for Indian tickers
221
+ if ticker.endswith(".NS") or ticker.endswith(".BO"):
222
+ log(f" -> Trying screener.in fallback for {ticker}...")
223
+ try:
224
+ base = ticker.split(".")[0].upper()
225
+ import requests
226
+ from bs4 import BeautifulSoup
227
+ r = requests.get(f"https://www.screener.in/company/{base}/", headers={"User-Agent": "Mozilla/5.0"}, timeout=10)
228
+ if r.ok:
229
+ soup = BeautifulSoup(r.text, "lxml")
230
+ h1 = soup.find("h1")
231
+ if h1:
232
+ name = h1.get_text(strip=True)
233
+ desc_div = soup.find("div", class_="company-profile")
234
+ desc = desc_div.get_text(strip=True) if desc_div else ""
235
+ log(f" [OK] screener.in resolved: {name}")
236
+ return CompanyMeta(
237
+ name=name, ticker=ticker, exchange="NSE/BSE (India)",
238
+ currency="INR", sector="Unknown", industry="Unknown",
239
+ country="India", description=desc[:500], website="",
240
+ mkt_cap=None, share_price=None, is_listed=True,
241
+ source_note="Data sourced from screener.in (yahooquery fallback).",
242
+ fiscal_year_end="March"
243
+ )
244
+ except Exception as e2:
245
+ log(f" [ERR] screener.in fallback failed: {e2}")
246
+
247
+ return None
248
 
249
 
250
  # -- FMP resolution ------------------------------------------------------------