|
32 | 32 | # These replace the legacy module dependencies |
33 | 33 |
|
34 | 34 | # URL constants for fund searches |
35 | | -fund_series_search_url = "https://www.sec.gov/cgi-bin/series?company=" |
36 | 35 | fund_class_or_series_search_url = "https://www.sec.gov/cgi-bin/browse-edgar?CIK={}" |
37 | 36 | fund_series_direct_url = "https://www.sec.gov/cgi-bin/browse-edgar?CIK={}&scd=series" |
38 | 37 |
|
@@ -400,75 +399,183 @@ def direct_get_fund_with_filings(contract_or_series_id: str): |
400 | 399 | log.warning("Error retrieving fund information for %s: %s", contract_or_series_id, e) |
401 | 400 | return None |
402 | 401 |
|
| 402 | +def _resolve_company_cik(identifier: str) -> Optional[tuple]: |
| 403 | + """ |
| 404 | + Resolve a fund identifier (ticker, series ID, or class ID) to a company CIK and name |
| 405 | + using the SEC's browse-edgar endpoint. |
| 406 | +
|
| 407 | + Returns: |
| 408 | + Tuple of (cik, company_name) or None if not found |
| 409 | + """ |
| 410 | + resolve_url = ( |
| 411 | + f"https://www.sec.gov/cgi-bin/browse-edgar?" |
| 412 | + f"company=&CIK={identifier}&type=&dateb=&owner=include&count=1" |
| 413 | + f"&search_text=&action=getcompany" |
| 414 | + ) |
| 415 | + try: |
| 416 | + html = download_text(resolve_url) |
| 417 | + soup = BeautifulSoup(html, "html.parser") |
| 418 | + tag = soup.find('span', class_='companyName') |
| 419 | + if not tag: |
| 420 | + return None |
| 421 | + company_name = tag.text.split('CIK')[0].strip() |
| 422 | + cik_link = tag.find('a') |
| 423 | + if not cik_link: |
| 424 | + return None |
| 425 | + cik = cik_link.text.split(' ')[0].strip() |
| 426 | + return cik, company_name |
| 427 | + except Exception as e: |
| 428 | + log.warning("Error resolving fund identifier %s: %s", identifier, e) |
| 429 | + return None |
| 430 | + |
| 431 | + |
| 432 | +def _parse_series_table(html: str) -> tuple: |
| 433 | + """ |
| 434 | + Parse the browse-edgar series listing page (scd=series) into structured data. |
| 435 | +
|
| 436 | + Returns: |
| 437 | + Tuple of (company_cik, company_name, series_list) where series_list is a list of dicts: |
| 438 | + [{'series_id': str, 'series_name': str, 'classes': [{'class_id': str, 'class_name': str, 'ticker': str}]}] |
| 439 | + """ |
| 440 | + soup = BeautifulSoup(html, "html.parser") |
| 441 | + tables = soup.find_all("table") |
| 442 | + if not tables: |
| 443 | + return None, None, [] |
| 444 | + |
| 445 | + table = tables[0] |
| 446 | + rows = table.find_all('tr') |
| 447 | + |
| 448 | + company_cik = None |
| 449 | + company_name = None |
| 450 | + series_list = [] |
| 451 | + current_series = None |
| 452 | + |
| 453 | + for row in rows: |
| 454 | + cells = row.find_all('td') |
| 455 | + num_cells = len(cells) |
| 456 | + |
| 457 | + # Skip header rows (0-2) and the large summary row (484+ cells) |
| 458 | + if num_cells < 2 or num_cells > 10: |
| 459 | + continue |
| 460 | + |
| 461 | + # Company row: 2 cells with CIK link + company name link |
| 462 | + if num_cells == 2: |
| 463 | + links = [a.text.strip() for a in cells[0].find_all('a')] |
| 464 | + if links and re.match(r'^0\d{9}$', links[0]): |
| 465 | + company_cik = links[0] |
| 466 | + name_links = [a.text.strip() for a in cells[1].find_all('a')] |
| 467 | + company_name = name_links[0] if name_links else cells[1].get_text(strip=True) |
| 468 | + |
| 469 | + # Series row: 3 cells — cell[1] has series ID, cell[2] has series name |
| 470 | + elif num_cells == 3: |
| 471 | + links_1 = [a.text.strip() for a in cells[1].find_all('a')] |
| 472 | + if links_1 and re.match(r'^S\d+$', links_1[0]): |
| 473 | + series_id = links_1[0] |
| 474 | + name_links = [a.text.strip() for a in cells[2].find_all('a')] |
| 475 | + series_name = name_links[0] if name_links else cells[2].get_text(strip=True) |
| 476 | + current_series = { |
| 477 | + 'series_id': series_id, |
| 478 | + 'series_name': series_name, |
| 479 | + 'classes': [] |
| 480 | + } |
| 481 | + series_list.append(current_series) |
| 482 | + |
| 483 | + # Class row: 4-5 cells — cell[2] has class ID, cell[3] has name, cell[4] has ticker |
| 484 | + elif num_cells in (4, 5) and current_series is not None: |
| 485 | + links_2 = [a.text.strip() for a in cells[2].find_all('a')] |
| 486 | + if links_2 and re.match(r'^C\d+$', links_2[0]): |
| 487 | + class_id = links_2[0] |
| 488 | + class_name = cells[3].get_text(strip=True) |
| 489 | + ticker = cells[4].get_text(strip=True) if num_cells == 5 else "" |
| 490 | + current_series['classes'].append({ |
| 491 | + 'class_id': class_id, |
| 492 | + 'class_name': class_name, |
| 493 | + 'ticker': ticker |
| 494 | + }) |
| 495 | + |
| 496 | + return company_cik, company_name, series_list |
| 497 | + |
| 498 | + |
403 | 499 | @lru_cache(maxsize=16) |
404 | 500 | def get_fund_object(identifier: str) -> Optional[Union[FundCompany, FundSeries, FundClass]]: |
405 | 501 | """ |
406 | | - Get a Fund related object by it's identifier. |
| 502 | + Get a Fund related object by its identifier. |
407 | 503 |
|
408 | 504 | Args: |
409 | | - identifier: A CIK, a series id (e.g. 'S000001234') or class id or Fund ticker (e.g. 'VFINX') |
| 505 | + identifier: A CIK, a series id (e.g. 'S000001234') or class id or Fund ticker (e.g. 'VFINX') |
410 | 506 |
|
411 | 507 | Returns: |
412 | 508 | A FundCompany or FundSeries or FundClass |
413 | 509 | """ |
414 | 510 |
|
415 | 511 | if re.match(r'^[CS]\d+$', identifier): |
416 | 512 | identifier_type = 'Series' if identifier.startswith('S') else 'Class' |
417 | | - fund_search_url = fund_series_search_url + f"&CIK={identifier}" |
418 | 513 | elif re.match(r"^[A-Z]{4}X$", identifier): |
419 | 514 | identifier_type = 'Class' |
420 | | - fund_search_url = fund_series_search_url + f"&ticker={identifier}" |
421 | 515 | elif re.match(r"^0\d{9}$", identifier): |
422 | 516 | identifier_type = 'FundCompany' |
423 | | - fund_search_url = fund_series_search_url + f"&CIK={identifier}" |
424 | 517 | else: |
425 | 518 | log.warning("Invalid fund identifier %s", identifier) |
426 | 519 | return None |
427 | 520 |
|
428 | | - # Download the fund page |
429 | | - fund_text = download_text(fund_search_url) |
430 | | - |
431 | | - soup = BeautifulSoup(fund_text, "html.parser") |
432 | | - if 'To retrieve filings, click on the CIK' not in soup.text: |
433 | | - return None |
434 | | - |
435 | | - tables = soup.find_all("table") |
| 521 | + # Step 1: Resolve identifier to company CIK |
| 522 | + if identifier_type == 'FundCompany': |
| 523 | + company_cik = identifier |
| 524 | + company_name = None |
| 525 | + else: |
| 526 | + result = _resolve_company_cik(identifier) |
| 527 | + if not result: |
| 528 | + log.warning("Could not resolve fund identifier %s", identifier) |
| 529 | + return None |
| 530 | + company_cik, company_name = result |
436 | 531 |
|
437 | | - # The fund table is the 6th table on the page |
438 | | - if len(tables) < 6: |
439 | | - log.warning("Expected fund table not found for %s", identifier) |
| 532 | + # Step 2: Fetch and parse the series listing page |
| 533 | + series_url = fund_series_direct_url.format(company_cik) |
| 534 | + try: |
| 535 | + series_html = download_text(series_url) |
| 536 | + except Exception as e: |
| 537 | + log.warning("Error fetching series listing for %s: %s", company_cik, e) |
440 | 538 | return None |
441 | 539 |
|
442 | | - fund_table = tables[5] |
| 540 | + parsed_cik, parsed_name, series_data = _parse_series_table(series_html) |
| 541 | + if company_name is None: |
| 542 | + company_name = parsed_name |
443 | 543 |
|
| 544 | + # Step 3: Build the object hierarchy |
444 | 545 | all_series = [] |
445 | | - fund_company:Optional[FundCompany] = None |
| 546 | + fund_company = FundCompany(cik_or_identifier=company_cik, fund_name=company_name, all_series=all_series) |
446 | 547 |
|
447 | | - current_series:Optional[FundSeries] = None |
448 | | - current_class:Optional[FundClass] = None |
449 | | - for tr in fund_table.find_all('tr')[4:]: # Skip the first 4 rows as they contain headers |
450 | | - row_data = [td.get_text().strip() for td in tr.find_all('td') if td.get_text().strip()] |
| 548 | + target_series = None |
| 549 | + target_class = None |
451 | 550 |
|
452 | | - if not row_data: |
453 | | - continue |
454 | | - if re.match(r'^0\d{9}$', row_data[0]): |
455 | | - fund_company = FundCompany(cik_or_identifier=row_data[0], fund_name=row_data[1], all_series=all_series) |
456 | | - elif re.match(r'^S\d+$', row_data[0]): |
457 | | - current_series = FundSeries(series_id=row_data[0], name=row_data[1], fund_company=fund_company) |
458 | | - fund_company.all_series.append(current_series) |
459 | | - elif re.match(r'^C\d+$', row_data[0]): |
460 | | - class_id, class_name = row_data[0], row_data[1] |
461 | | - ticker = row_data[2] if len(row_data) > 2 else None |
462 | | - current_class = FundClass(class_id=class_id, name=class_name, ticker=ticker) |
| 551 | + for s in series_data: |
| 552 | + current_series = FundSeries(series_id=s['series_id'], name=s['series_name'], fund_company=fund_company) |
| 553 | + fund_company.all_series.append(current_series) |
| 554 | + |
| 555 | + for c in s.get('classes', []): |
| 556 | + ticker = c.get('ticker') or None |
| 557 | + current_class = FundClass(class_id=c['class_id'], name=c['class_name'], ticker=ticker) |
463 | 558 | current_class.series = current_series |
464 | 559 | current_series.fund_classes.append(current_class) |
465 | 560 |
|
| 561 | + # Track the target class for class/ticker lookups |
| 562 | + if identifier_type == 'Class': |
| 563 | + if identifier.startswith('C') and current_class.class_id == identifier: |
| 564 | + target_class = current_class |
| 565 | + elif not identifier.startswith('C') and current_class.ticker == identifier: |
| 566 | + target_class = current_class |
| 567 | + |
| 568 | + # Track the target series |
| 569 | + if identifier_type == 'Series' and current_series.series_id == identifier: |
| 570 | + target_series = current_series |
| 571 | + |
| 572 | + # Step 4: Return the appropriate object |
466 | 573 | if identifier_type == "FundCompany": |
467 | 574 | return fund_company |
468 | 575 | elif identifier_type == "Series": |
469 | | - return current_series |
| 576 | + return target_series |
470 | 577 | elif identifier_type == "Class": |
471 | | - return current_class |
| 578 | + return target_class |
472 | 579 |
|
473 | 580 |
|
474 | 581 | def is_fund_ticker(identifier: str) -> bool: |
|
0 commit comments