From 56a9b3300cbbd4c5ba839b29299f910931fbdfd9 Mon Sep 17 00:00:00 2001
From: Brendan <94894839+abrendan@users.noreply.github.com>
Date: Mon, 2 Dec 2024 14:12:42 +0000
Subject: [PATCH] Made the languageSelect flexible and added countrySelect
---
index.html | 9 ++++----
script.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 71 insertions(+), 6 deletions(-)
diff --git a/index.html b/index.html
index ffeadb4..2b4fdfa 100644
--- a/index.html
+++ b/index.html
@@ -38,10 +38,11 @@
+
+
+
diff --git a/script.js b/script.js
index a9bcf82..05fd962 100644
--- a/script.js
+++ b/script.js
@@ -16,31 +16,92 @@ function configureDropdowns() {
const apiSelection = document.getElementById('apiSelect').value;
const sourceSelectContainer = document.getElementById('sourceSelectContainer');
const languageSelectContainer = document.getElementById('languageSelect').parentElement;
+ const countrySelectContainer = document.getElementById('countrySelect').parentElement;
if (apiSelection === 'currents') {
sourceSelectContainer.style.display = 'block';
languageSelectContainer.style.display = 'block';
+ countrySelectContainer.style.display = 'block';
+
+ fetchLanguages(); // Fetch and populate languages
+ fetchCountries(); // Fetch and populate countries
} else if (apiSelection === 'nyt') {
sourceSelectContainer.style.display = 'none';
languageSelectContainer.style.display = 'none';
+ countrySelectContainer.style.display = 'none';
}
}
+// Fetch available languages from the Currents API
+function fetchLanguages() {
+ const apiKey = 'b2uZWPY42BaUWN4Luaj_fbjJR6y7idTudew9UcpSbzr2D2VO';
+ const url = `https://api.currentsapi.services/v1/available/languages?apiKey=${apiKey}`;
+
+ fetch(url)
+ .then(response => {
+ if (!response.ok) {
+ throw new Error(`Network response was not ok: ${response.statusText} (${response.status})`);
+ }
+ return response.json();
+ })
+ .then(data => {
+ const languageSelect = document.getElementById('languageSelect');
+ languageSelect.innerHTML = ''; // Reset languages dropdown
+
+ // Loop through the language object
+ for (const [language, code] of Object.entries(data.languages)) {
+ const option = document.createElement('option');
+ option.value = code; // Set the value to the language code
+ option.textContent = language; // Display the full language name
+ languageSelect.appendChild(option);
+ }
+ })
+ .catch(error => console.error('Error fetching languages:', error));
+}
+
+// Fetch available countries from the Currents API
+function fetchCountries() {
+ const apiKey = 'b2uZWPY42BaUWN4Luaj_fbjJR6y7idTudew9UcpSbzr2D2VO';
+ const url = `https://api.currentsapi.services/v1/available/regions?apiKey=${apiKey}`; // New API link for regions
+
+ fetch(url)
+ .then(response => {
+ if (!response.ok) {
+ throw new Error(`Network response was not ok: ${response.statusText} (${response.status})`);
+ }
+ return response.json();
+ })
+ .then(data => {
+ const countrySelect = document.getElementById('countrySelect');
+ countrySelect.innerHTML = ''; // Reset countries dropdown
+
+ // Loop through the regions object
+ for (const [country, code] of Object.entries(data.regions)) {
+ const option = document.createElement('option');
+ option.value = code; // Set the value to the country code
+ option.textContent = country; // Display the full country name
+ countrySelect.appendChild(option);
+ }
+ })
+ .catch(error => console.error('Error fetching countries:', error));
+}
+
function fetchNewsArticles() {
const query = document.getElementById('searchInput').value;
const topic = document.getElementById('topicSelect').value;
const apiSelection = document.getElementById('apiSelect').value;
const language = document.getElementById('languageSelect').value;
+ const region = document.getElementById('countrySelect').value; // Get selected region
if (apiSelection === 'currents') {
const source = document.getElementById('sourceSelect').value;
- fetchFromCurrentsAPI(query, source, topic, language);
+ fetchFromCurrentsAPI(query, source, topic, language, region); // Pass region filter
} else if (apiSelection === 'nyt') {
fetchFromNYTAPI(query, topic);
}
}
-function fetchFromCurrentsAPI(query, source, topic, language) {
+function fetchFromCurrentsAPI(query, source, topic, language, region) {
const apiKey = 'b2uZWPY42BaUWN4Luaj_fbjJR6y7idTudew9UcpSbzr2D2VO';
let url = `https://api.currentsapi.services/v1/search?apiKey=${apiKey}`;
@@ -56,6 +117,9 @@ function fetchFromCurrentsAPI(query, source, topic, language) {
if (language) {
url += `&language=${language}`;
}
+ if (region) {
+ url += `&country=${region}`; // Add region to the query if selected
+ }
fetch(url)
.then(response => {