Skip to content
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

Added condition before fetching IP from ipinfo.io to avoid the rate-limiting issue #91

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,19 @@ window.addEventListener('DOMContentLoaded', async () => {
// Use the user's API key if available, otherwise use the default API key
const apiKey = userApiKey || defaultApiKey;
proxyurl = userproxyurl || defaultProxyURL;
// Getting current user location
const geoLocation = 'https://ipinfo.io/json/';
const locationData = await fetch(geoLocation);
const parsedLocation = await locationData.json();
const currentUserLocation = parsedLocation.ip;
const locationQuery = savedLocation || currentUserLocation;
var currentLanguage = getLanguageStatus('selectedLanguage') || 'en';

const weatherApi = `https://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${locationQuery}&aqi=no&lang=${currentLanguage}`;
var currentUserLocation = savedLocation; // Get saved location
if (!currentUserLocation) {
// Only fetch if there is no saved location
const geoLocation = 'https://ipinfo.io/json/';
const locationData = await fetch(geoLocation);
const parsedLocation = await locationData.json();
currentUserLocation = parsedLocation.ip; // Update to user's IP-based location
}
var currentLanguage = getLanguageStatus('selectedLanguage') || 'en';

// Weather API call using currentUserLocation, which is either user input or IP address
const weatherApi = `https://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${currentUserLocation}&aqi=no&lang=${currentLanguage}`;

const data = await fetch(weatherApi);
const parsedData = await data.json();
Expand Down