File size: 2,974 Bytes
8c94afd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# genesis/funding.py
import os
import requests
from typing import List, Dict

# Optional: If you have Crunchbase API key
CRUNCHBASE_API_KEY = os.getenv("CRUNCHBASE_API_KEY")
CRUNCHBASE_API_URL = "https://api.crunchbase.com/api/v4/entities/organizations"

# Fallback public dataset if no API key
FALLBACK_DATA_URL = "https://raw.githubusercontent.com/owid/biotech-funding-dataset/main/funding.csv"

def search_biotech_funding(query: str, max_results: int = 5) -> List[Dict]:
    """
    Search for biotech funding data via Crunchbase API or fallback CSV.
    Returns a list of dictionaries with:
    - name
    - funding_usd
    - last_round
    - investors
    - website
    """
    if CRUNCHBASE_API_KEY:
        try:
            params = {
                "user_key": CRUNCHBASE_API_KEY,
                "query": query,
                "limit": max_results
            }
            r = requests.get(CRUNCHBASE_API_URL, params=params, timeout=15)
            r.raise_for_status()
            data = r.json()

            results = []
            for entity in data.get("entities", []):
                props = entity.get("properties", {})
                results.append({
                    "name": props.get("name", "N/A"),
                    "funding_usd": props.get("total_funding_usd", "N/A"),
                    "last_round": props.get("last_funding_type", "N/A"),
                    "investors": ", ".join(props.get("investors", [])) if props.get("investors") else "N/A",
                    "website": props.get("homepage_url", "")
                })
            return results
        except Exception as e:
            print(f"[Funding] Crunchbase API failed: {e}")

    # Fallback: Use CSV dataset
    try:
        r = requests.get(FALLBACK_DATA_URL, timeout=10)
        r.raise_for_status()
        lines = r.text.strip().split("\n")
        header = lines[0].split(",")
        results = []
        for row in lines[1:]:
            cols = row.split(",")
            if query.lower() in row.lower():
                results.append({
                    "name": cols[0],
                    "funding_usd": cols[1],
                    "last_round": cols[2],
                    "investors": cols[3],
                    "website": cols[4]
                })
            if len(results) >= max_results:
                break
        return results
    except Exception as e:
        print(f"[Funding] Fallback CSV failed: {e}")
        return []

def format_funding_markdown(companies: List[Dict]) -> str:
    """Convert funding list into markdown table."""
    if not companies:
        return "No funding data found."
    md = "| Company | Funding (USD) | Last Round | Investors |\n|---------|----------------|------------|-----------|\n"
    for c in companies:
        name_link = f"[{c['name']}]({c['website']})" if c["website"] else c["name"]
        md += f"| {name_link} | {c['funding_usd']} | {c['last_round']} | {c['investors']} |\n"
    return md