-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Integrate GBIF API to display biodiversity data #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/initial-project-structure
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,5 +36,29 @@ def forest_area(): | |
| except requests.exceptions.RequestException as e: | ||
| return jsonify({"error": str(e)}), 500 | ||
|
|
||
| @app.route('/api/gbif_occurrences') | ||
| def gbif_occurrences(): | ||
| # GBIF API URL for occurrences in Togo | ||
| url = "https://api.gbif.org/v1/occurrence/search?country=TG&limit=5" | ||
|
|
||
| try: | ||
| response = requests.get(url) | ||
| data = response.json() | ||
|
|
||
| if data and data['results']: | ||
| # Clean and format the data | ||
| formatted_data = [] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (code-quality): Convert for loop into list comprehension ( |
||
| for entry in data['results']: | ||
| formatted_data.append({ | ||
| 'species': entry.get('scientificName', 'N/A'), | ||
| 'url': f"https://www.gbif.org/occurrence/{entry['key']}" | ||
| }) | ||
| return jsonify(formatted_data) | ||
| else: | ||
| return jsonify({"error": "No data found for the selected criteria."}), 404 | ||
|
|
||
| except requests.exceptions.RequestException as e: | ||
| return jsonify({"error": str(e)}), 500 | ||
|
Comment on lines
+42
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (python.django.security.injection.ssrf.ssrf-injection-requests): Data from request object is passed to a new server-side request. This could lead to a server-side request forgery (SSRF). To mitigate, ensure that schemes and hosts are validated against an allowlist, do not forward the response to the user, and ensure proper authentication and transport-layer security in the proxied request. See https://owasp.org/www-community/attacks/Server_Side_Request_Forgery to learn more about SSRF vulnerabilities. Source: opengrep |
||
|
|
||
| if __name__ == '__main__': | ||
| app.run(debug=True) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,12 @@ document.addEventListener('DOMContentLoaded', () => { | |
| const chatInput = document.getElementById('chat-input'); | ||
| const chatWindow = document.getElementById('chat-window'); | ||
| const worldBankDataContainer = document.getElementById('world-bank-data'); | ||
| const gbifOccurrencesContainer = document.getElementById('gbif-occurrences'); | ||
|
|
||
| // Fetch and display World Bank data on page load | ||
| fetchWorldBankData(); | ||
| // Fetch and display GBIF data on page load | ||
| fetchGbifData(); | ||
|
|
||
| sendBtn.addEventListener('click', () => { | ||
| const userInput = chatInput.value; | ||
|
|
@@ -52,4 +55,30 @@ document.addEventListener('DOMContentLoaded', () => { | |
| worldBankDataContainer.innerHTML = `<p>Error fetching data: ${error.message}</p>`; | ||
| } | ||
| } | ||
|
|
||
| async function fetchGbifData() { | ||
| try { | ||
| const response = await fetch('/api/gbif_occurrences'); | ||
| if (!response.ok) { | ||
| throw new Error(`HTTP error! status: ${response.status}`); | ||
| } | ||
| const data = await response.json(); | ||
|
|
||
| if (data.error) { | ||
| gbifOccurrencesContainer.innerHTML = `<p>Error fetching data: ${data.error}</p>`; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (javascript.browser.security.insecure-document-method): User controlled data in methods like Source: opengrep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (javascript.browser.security.insecure-innerhtml): User controlled data in a Source: opengrep |
||
| return; | ||
| } | ||
|
|
||
| let html = '<ul>'; | ||
| data.forEach(item => { | ||
| html += `<li><a href="${item.url}" target="_blank">${item.species}</a></li>`; | ||
| }); | ||
| html += '</ul>'; | ||
|
|
||
| gbifOccurrencesContainer.innerHTML = html; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (javascript.browser.security.insecure-document-method): User controlled data in methods like Source: opengrep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (javascript.browser.security.insecure-innerhtml): User controlled data in a Source: opengrep |
||
|
|
||
| } catch (error) { | ||
| gbifOccurrencesContainer.innerHTML = `<p>Error fetching data: ${error.message}</p>`; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (javascript.browser.security.insecure-document-method): User controlled data in methods like Source: opengrep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (javascript.browser.security.insecure-innerhtml): User controlled data in a Source: opengrep |
||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (code-quality): Avoid function declarations, favouring function assignment expressions, inside blocks. ( ExplanationFunction declarations may be hoisted in Javascript, but the behaviour is inconsistent between browsers. Hoisting is generally confusing and should be avoided. Rather than using function declarations inside blocks, you should use function expressions, which create functions in-scope. |
||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
security (python.requests.best-practice.use-timeout): Detected a 'requests' call without a timeout set. By default, 'requests' calls wait until the connection is closed. This means a 'requests' call without a timeout will hang the program if a response is never received. Consider setting a timeout for all 'requests'.
Source: opengrep