mgbam commited on
Commit
e35b779
·
verified ·
1 Parent(s): 013e688

Update genesis/funding.py

Browse files
Files changed (1) hide show
  1. genesis/funding.py +95 -6
genesis/funding.py CHANGED
@@ -5,39 +5,57 @@ Fetches worldwide biotech & synthetic biology grant opportunities.
5
  """
6
 
7
  import requests
 
 
 
8
 
9
  NIH_FUNDING_API = "https://api.reporter.nih.gov/v2/projects/search"
10
  EU_CORDIS_API = "https://cordis.europa.eu/api/project" # Horizon Europe
11
  WELLCOME_TRUST_API = "https://api.wellcome.org/grants" # Example placeholder
12
 
13
- def get_nih_funding(query="synthetic biology"):
 
 
 
14
  """Fetch NIH funding opportunities."""
15
- payload = {"criteria": {"textCriteria": {"searchText": query}}, "offset": 0, "limit": 10}
16
  try:
17
- r = requests.post(NIH_FUNDING_API, json=payload)
18
  r.raise_for_status()
19
  return r.json()
20
  except Exception as e:
 
21
  return {"error": str(e)}
22
 
23
- def get_eu_funding(query="synthetic biology"):
 
 
 
24
  """Fetch Horizon Europe / EU CORDIS funding calls."""
25
  try:
26
- r = requests.get(f"{EU_CORDIS_API}?q={query}&num=10")
27
  r.raise_for_status()
28
  return r.json()
29
  except Exception as e:
 
30
  return {"error": str(e)}
31
 
 
 
 
32
  def get_wellcome_funding(query="synthetic biology"):
33
  """Fetch Wellcome Trust grants."""
34
  try:
35
- r = requests.get(f"{WELLCOME_TRUST_API}?query={query}")
36
  r.raise_for_status()
37
  return r.json()
38
  except Exception as e:
 
39
  return {"error": str(e)}
40
 
 
 
 
41
  def get_global_funding(query="synthetic biology"):
42
  """Aggregate funding from all major sources."""
43
  return {
@@ -45,3 +63,74 @@ def get_global_funding(query="synthetic biology"):
45
  "EU_CORDIS": get_eu_funding(query),
46
  "Wellcome_Trust": get_wellcome_funding(query)
47
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  """
6
 
7
  import requests
8
+ import logging
9
+
10
+ logging.basicConfig(level=logging.INFO)
11
 
12
  NIH_FUNDING_API = "https://api.reporter.nih.gov/v2/projects/search"
13
  EU_CORDIS_API = "https://cordis.europa.eu/api/project" # Horizon Europe
14
  WELLCOME_TRUST_API = "https://api.wellcome.org/grants" # Example placeholder
15
 
16
+ # -----------------------
17
+ # NIH Funding
18
+ # -----------------------
19
+ def get_nih_funding(query="synthetic biology", limit=10):
20
  """Fetch NIH funding opportunities."""
21
+ payload = {"criteria": {"textCriteria": {"searchText": query}}, "offset": 0, "limit": limit}
22
  try:
23
+ r = requests.post(NIH_FUNDING_API, json=payload, timeout=30)
24
  r.raise_for_status()
25
  return r.json()
26
  except Exception as e:
27
+ logging.error(f"[Funding] NIH API error: {e}")
28
  return {"error": str(e)}
29
 
30
+ # -----------------------
31
+ # EU Horizon Europe / CORDIS Funding
32
+ # -----------------------
33
+ def get_eu_funding(query="synthetic biology", limit=10):
34
  """Fetch Horizon Europe / EU CORDIS funding calls."""
35
  try:
36
+ r = requests.get(f"{EU_CORDIS_API}?q={query}&num={limit}", timeout=30)
37
  r.raise_for_status()
38
  return r.json()
39
  except Exception as e:
40
+ logging.error(f"[Funding] EU CORDIS API error: {e}")
41
  return {"error": str(e)}
42
 
43
+ # -----------------------
44
+ # Wellcome Trust Grants
45
+ # -----------------------
46
  def get_wellcome_funding(query="synthetic biology"):
47
  """Fetch Wellcome Trust grants."""
48
  try:
49
+ r = requests.get(f"{WELLCOME_TRUST_API}?query={query}", timeout=30)
50
  r.raise_for_status()
51
  return r.json()
52
  except Exception as e:
53
+ logging.error(f"[Funding] Wellcome Trust API error: {e}")
54
  return {"error": str(e)}
55
 
56
+ # -----------------------
57
+ # Aggregator
58
+ # -----------------------
59
  def get_global_funding(query="synthetic biology"):
60
  """Aggregate funding from all major sources."""
61
  return {
 
63
  "EU_CORDIS": get_eu_funding(query),
64
  "Wellcome_Trust": get_wellcome_funding(query)
65
  }
66
+
67
+ # -----------------------
68
+ # Pipeline-Compatible Function
69
+ # -----------------------
70
+ def fetch_funding_data(query, max_results=10):
71
+ """
72
+ Pipeline-friendly function that returns a list of funding entries.
73
+ Falls back to mock data if APIs fail.
74
+ """
75
+ logging.info(f"[Funding] Fetching global funding data for: {query}")
76
+ try:
77
+ results = []
78
+
79
+ nih_data = get_nih_funding(query, limit=max_results)
80
+ if isinstance(nih_data, dict) and "projects" in nih_data:
81
+ for proj in nih_data.get("projects", []):
82
+ results.append({
83
+ "source": "NIH",
84
+ "title": proj.get("projectTitle", "No title"),
85
+ "amount": proj.get("awardAmount", "Unknown"),
86
+ "date": proj.get("projectStartDate", ""),
87
+ "link": f"https://reporter.nih.gov/project-details/{proj.get('projectNumber', '')}"
88
+ })
89
+
90
+ eu_data = get_eu_funding(query, limit=max_results)
91
+ if isinstance(eu_data, dict) and "projects" in eu_data:
92
+ for proj in eu_data.get("projects", []):
93
+ results.append({
94
+ "source": "EU CORDIS",
95
+ "title": proj.get("title", "No title"),
96
+ "amount": proj.get("totalCost", "Unknown"),
97
+ "date": proj.get("startDate", ""),
98
+ "link": proj.get("rcn", "")
99
+ })
100
+
101
+ wellcome_data = get_wellcome_funding(query)
102
+ if isinstance(wellcome_data, dict) and "grants" in wellcome_data:
103
+ for grant in wellcome_data.get("grants", []):
104
+ results.append({
105
+ "source": "Wellcome Trust",
106
+ "title": grant.get("title", "No title"),
107
+ "amount": grant.get("amountAwarded", "Unknown"),
108
+ "date": grant.get("awardDate", ""),
109
+ "link": grant.get("url", "")
110
+ })
111
+
112
+ # If no real data found, return mock data
113
+ if not results:
114
+ logging.warning("[Funding] No live API results — returning mock data.")
115
+ results = [
116
+ {
117
+ "company": "Example Biotech Inc.",
118
+ "amount": "$5M",
119
+ "date": "2024-05-10",
120
+ "source": "Mock Funding API",
121
+ "link": "https://example.com/funding/123"
122
+ },
123
+ {
124
+ "company": "Synthetic Bio Solutions",
125
+ "amount": "$2.5M",
126
+ "date": "2023-12-20",
127
+ "source": "Mock Funding API",
128
+ "link": "https://example.com/funding/456"
129
+ }
130
+ ]
131
+
132
+ return results
133
+
134
+ except Exception as e:
135
+ logging.error(f"[Funding] fetch_funding_data failed: {e}")
136
+ return []