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

feature: geolocation utils extracted from backend-client #296

Merged
merged 17 commits into from
Nov 19, 2024

Conversation

JarbasAl
Copy link
Member

@JarbasAl JarbasAl commented Nov 18, 2024

Summary by CodeRabbit

  • New Features

    • Introduced enhanced geolocation services with functions for timezone, geolocation, reverse geolocation, and IP geolocation lookups.
    • Added IP address validation functionality.
  • Chores

    • Reinstated the langcodes package and added timezonefinder to project dependencies.
  • Bug Fixes

    • Improved error handling across geolocation functions for better user feedback.

Copy link
Contributor

coderabbitai bot commented Nov 18, 2024

Walkthrough

The changes in this pull request involve updates to the project's dependencies and the introduction of new functionality related to geolocation and IP validation. The requirements.txt file has been modified to reinstate the langcodes package and add the timezonefinder package. Additionally, the geolocation.py file has been updated with multiple functions for geolocation services, while network_utils.py has been enhanced to include a function for validating IP addresses.

Changes

File Change Summary
requirements/requirements.txt - Reinstated package: langcodes
- Added package: timezonefinder
ovos_utils/geolocation.py - Added functions: get_timezone, get_geolocation, get_reverse_geolocation, get_ip_geolocation
ovos_utils/network_utils.py - Added function: is_valid_ip for IP address validation
- Imported ipaddress module
- Marked is_connected as deprecated

Poem

In the meadow, where the bunnies play,
New tools for geolocation come our way!
With IPs validated, and timezones found,
Our hopping code leaps with joy all around! 🐇✨
Let's celebrate the changes, both big and small,
For in this code garden, there's room for us all!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Nov 18, 2024

Codecov Report

Attention: Patch coverage is 1.83486% with 107 lines in your changes missing coverage. Please review.

Project coverage is 45.72%. Comparing base (61dcb09) to head (f021340).
Report is 4 commits behind head on dev.

Files with missing lines Patch % Lines
ovos_utils/geolocation.py 0.00% 102 Missing ⚠️
ovos_utils/network_utils.py 28.57% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #296      +/-   ##
==========================================
- Coverage   47.19%   45.72%   -1.48%     
==========================================
  Files          31       32       +1     
  Lines        3244     3353     +109     
==========================================
+ Hits         1531     1533       +2     
- Misses       1713     1820     +107     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🧹 Outside diff range and nitpick comments (2)
ovos_utils/location.py (2)

70-71: Simplify timezone assignment by removing redundant checks

Since the 'timezone' key is not present in the initial 'location' dictionary, the condition if "timezone" not in location: will always be true. You can simplify the code by directly assigning the timezone.

Apply this diff to simplify the code:

-if "timezone" not in location:
-    location["timezone"] = get_timezone(lon=lon, lat=lat)
+location["timezone"] = get_timezone(lon=lon, lat=lat)

This applies to both instances in the code at lines 70-71 and 118-121.

Also applies to: 118-121


27-28: Refactor API request logic into a utility function to enhance maintainability

Both get_geolocation and get_reverse_geolocation functions perform API requests with similar error handling needs. Consider refactoring the request and response parsing into a utility function to reduce code duplication and improve maintainability.

Example:

def nominatim_api_get(url, params):
    headers = {"User-Agent": "OVOS/1.0"}
    response = requests.get(url, params=params, headers=headers)
    if response.status_code == 200:
        try:
            return response.json()
        except ValueError:
            # handle JSON decoding error
            return None
    else:
        # handle HTTP error
        return None

Then update your functions accordingly:

# In get_geolocation
results = nominatim_api_get(url, {"q": location, "format": "json", "limit": 1})
if results:
    data = results[0]
    # proceed with data
else:
    # handle error
    return None

# In get_reverse_geolocation
details = nominatim_api_get(url, {"lat": lat, "lon": lon, "format": "json"})
if details and 'address' in details:
    address = details['address']
    # proceed with address
else:
    # handle error
    return None

Also applies to: 87-88

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between eae6426 and 9ceafa2.

📒 Files selected for processing (2)
  • ovos_utils/location.py (1 hunks)
  • requirements/requirements.txt (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • requirements/requirements.txt

ovos_utils/location.py Outdated Show resolved Hide resolved
ovos_utils/location.py Outdated Show resolved Hide resolved
ovos_utils/location.py Outdated Show resolved Hide resolved
ovos_utils/location.py Outdated Show resolved Hide resolved
ovos_utils/location.py Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🧹 Outside diff range and nitpick comments (1)
ovos_utils/location.py (1)

1-155: Consider architectural improvements for reliability

The module heavily depends on external APIs without fallback mechanisms.

Consider these improvements:

  1. Add configuration management for API endpoints and timeouts:
NOMINATIM_BASE_URL = "https://nominatim.openstreetmap.org"
IP_API_BASE_URL = "https://ip-api.com"
REQUEST_TIMEOUT = 10  # seconds
  1. Implement fallback mechanisms:
  • Alternative geolocation providers
  • Local timezone database fallback
  • Caching failed requests to prevent repeated failures
  1. Add retry mechanism for transient failures:
from tenacity import retry, stop_after_attempt, wait_exponential

@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
def _make_api_request(url, params, headers):
    response = requests.get(url, params=params, headers=headers, timeout=REQUEST_TIMEOUT)
    response.raise_for_status()
    return response.json()
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 9ceafa2 and ad1b550.

📒 Files selected for processing (1)
  • ovos_utils/location.py (1 hunks)

ovos_utils/location.py Outdated Show resolved Hide resolved
ovos_utils/location.py Outdated Show resolved Hide resolved
ovos_utils/location.py Outdated Show resolved Hide resolved
ovos_utils/location.py Outdated Show resolved Hide resolved
ovos_utils/location.py Outdated Show resolved Hide resolved
JarbasAl and others added 2 commits November 19, 2024 09:32
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (2)
ovos_utils/location.py (2)

19-20: Avoid exposing raw error messages

The error message from the original exception is directly exposed in the raised ValueError. This could potentially expose implementation details.

Consider sanitizing the error message:

-    raise ValueError(f"Invalid coordinates: {str(e)}")
+    raise ValueError("Invalid coordinates: Values must be valid numbers within range")
🧰 Tools
🪛 Ruff

20-20: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


25-32: Update docstring return type

The docstring incorrectly states that the return type is 'str', but the function returns a dictionary or None.

Update the return type in the docstring:

     Returns:
-        str: JSON structure with lookup results
+        dict or None: Dictionary containing location details if found, None otherwise
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between ad1b550 and 09ee11a.

📒 Files selected for processing (1)
  • ovos_utils/location.py (1 hunks)
🧰 Additional context used
🪛 Ruff
ovos_utils/location.py

2-2: requests.exceptions.RequestException imported but unused

Remove unused import

(F401)


2-2: requests.exceptions.Timeout imported but unused

Remove unused import

(F401)


20-20: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


167-167: Redefinition of unused get_ip_geolocation from line 148

(F811)

🔇 Additional comments (2)
ovos_utils/location.py (2)

109-111:


112-139:

ovos_utils/location.py Outdated Show resolved Hide resolved
ovos_utils/location.py Outdated Show resolved Hide resolved
ovos_utils/location.py Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (1)
ovos_utils/location.py (1)

121-121: Remove redundant address lookup

The address is already retrieved and checked at line 114:

-    address = details.get("address")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 09ee11a and 429205a.

📒 Files selected for processing (1)
  • ovos_utils/location.py (1 hunks)
🧰 Additional context used
🪛 Ruff
ovos_utils/location.py

22-22: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

🔇 Additional comments (4)
ovos_utils/location.py (4)

157-202: LGTM! Well-implemented IP geolocation functionality

The implementation includes:

  • Proper IP validation
  • Good error handling
  • Comprehensive response processing

1-8: 🛠️ Refactor suggestion

Import request exceptions for better error handling

Add exception imports from requests for better error handling of network-related issues:

 import ipaddress
 
 import requests
+from requests.exceptions import RequestException, Timeout
 from timezonefinder import TimezoneFinder

Likely invalid or redundant comment.


78-80: ⚠️ Potential issue

Remove incorrect usage of calculated_postcode as state code

The state code is incorrectly falling back to calculated_postcode:

-                "code": tags.get("state_code") or
-                        details["calculated_postcode"] or "",
+                "code": tags.get("state_code") or "",

Likely invalid or redundant comment.


56-58: ⚠️ Potential issue

Add error handling for OSM ID lookup

The code assumes osm_id and osm_type exist in the response. Add proper error handling:

-    details = requests.get(url, params={"osmid": data['osm_id'], "osmtype": data['osm_type'][0].upper(),
-                                        "format": "json"},
-                           headers={"User-Agent": "OVOS/1.0"}).json()
+    if 'osm_id' not in data or 'osm_type' not in data:
+        raise ValueError("Missing OSM ID or type in response")
+    response = _make_nominatim_request(url,
+                                     params={"osmid": data['osm_id'],
+                                            "osmtype": data['osm_type'][0].upper(),
+                                            "format": "json"},
+                                     headers={"User-Agent": "OVOS/1.0"})
+    if response.status_code != 200:
+        raise ValueError(f"OSM details lookup failed: status code {response.status_code}")
+    details = response.json()

Likely invalid or redundant comment.

ovos_utils/location.py Outdated Show resolved Hide resolved
ovos_utils/location.py Outdated Show resolved Hide resolved
ovos_utils/location.py Outdated Show resolved Hide resolved
JarbasAl and others added 3 commits November 19, 2024 09:49
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (3)
ovos_utils/location.py (3)

12-41: Consider caching timezone lookups

The function performs well but could benefit from caching since timezone data is static for given coordinates. Consider adding the @timed_lru_cache decorator as used in other functions.

+@timed_lru_cache(seconds=3600)  # Cache for 1 hour since timezone data is static
 def get_timezone(lat: float, lon: float) -> Dict[str, str]:

105-125: Consider extracting location object construction to a helper function

The nested dictionary construction is complex and could benefit from being extracted into a helper function for better maintainability and reusability.

Consider creating a helper function like:

def _build_location_dict(address: str, city: Dict, coordinates: Dict) -> Dict[str, Any]:
    return {
        "address": address,
        "city": city,
        "coordinate": coordinates
    }

251-265: Consider extracting data structure construction

Similar to earlier suggestions, the nested dictionary construction could benefit from helper functions.

Consider creating helper functions:

def _build_region_dict(code: str, name: str, country: Dict) -> Dict[str, Any]:
    return {"code": code, "name": name, "country": country}

def _build_city_dict(code: str, name: str, state: Dict) -> Dict[str, Any]:
    return {"code": code, "name": name, "state": state}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 429205a and 2763066.

📒 Files selected for processing (1)
  • ovos_utils/location.py (1 hunks)
🧰 Additional context used
🪛 Ruff
ovos_utils/location.py

64-64: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


87-87: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


154-154: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


241-241: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

🔇 Additional comments (3)
ovos_utils/location.py (3)

201-216: LGTM! Well-implemented helper function

The function is focused, well-documented, and handles errors appropriately.


238-240: Ensure HTTPS usage for IP API calls

While the code uses HTTPS, it's worth noting that the free tier of ip-api.com only supports HTTP. Consider either:

  1. Documenting this limitation
  2. Using the pro version for HTTPS support
  3. Switching to a different service that supports HTTPS in the free tier

112-114: ⚠️ Potential issue

Remove incorrect state code fallback to postcode

The state code should not fall back to calculated_postcode as these are different types of data.

-                "code": tags.get("state_code") or
-                        details["calculated_postcode"] or "",
+                "code": tags.get("state_code") or "",

Likely invalid or redundant comment.

ovos_utils/location.py Outdated Show resolved Hide resolved
ovos_utils/location.py Outdated Show resolved Hide resolved
JarbasAl and others added 4 commits November 19, 2024 10:41
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (2)
ovos_utils/network_utils.py (1)

20-34: Consider enhancing the IP validation documentation and handling

The implementation is clean and correct, using the standard library's ipaddress module. Consider these enhancements:

 def is_valid_ip(ip: str) -> bool:
     """
     Validate an IP address.
+
+    Validates both IPv4 and IPv6 addresses using the ipaddress module.
 
     Args:
-        ip (str): The IP address to validate.
+        ip (str): The IP address to validate (IPv4 or IPv6).
+
+    Examples:
+        >>> is_valid_ip("192.168.1.1")
+        True
+        >>> is_valid_ip("2001:db8::1")
+        True
+        >>> is_valid_ip("256.256.256.256")
+        False
 
     Returns:
         bool: True if the IP is valid, False otherwise.
     """
+    if ip is None:
+        return False
     try:
         ipaddress.ip_address(ip)
         return True
     except ValueError:
         return False
ovos_utils/geolocation.py (1)

222-223: Consider using HTTPS for API requests to enhance security

The request to the IP geolocation service is made over HTTP, which may expose sensitive data over the network. If the API supports HTTPS, consider switching to HTTPS to improve security.

Verify if the API supports HTTPS and update the URL accordingly:

-    response = requests.get(f"http://ip-api.com/json/{ip}",
+    response = requests.get(f"https://ip-api.com/json/{ip}",
                             params={"fields": fields}, timeout=timeout)

If HTTPS is not available for the free API, consider using an alternative service that supports secure connections to protect user data.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 2763066 and 08ac7ff.

📒 Files selected for processing (2)
  • ovos_utils/geolocation.py (1 hunks)
  • ovos_utils/network_utils.py (2 hunks)
🧰 Additional context used
🪛 Ruff
ovos_utils/geolocation.py

63-63: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


86-86: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


153-153: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


225-225: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

🔇 Additional comments (2)
ovos_utils/network_utils.py (2)

1-1: LGTM: Clean import addition

The ipaddress standard library import is appropriately placed and properly grouped with other standard library imports.


20-34: Verify integration with geolocation functionality

Let's verify that this function's interface aligns with its usage in the geolocation module.

✅ Verification successful

Based on the results, I can now generate the final response since I have sufficient information about the integration between is_valid_ip and the geolocation functionality.

Integration with geolocation functionality is properly aligned

The verification shows clean integration:

  • Function signature matches usage: is_valid_ip(ip: str) -> bool aligns with its usage in geolocation.py
  • Error handling is consistent: geolocation.py properly checks the return value and raises a ValueError with a descriptive message
  • Import is correctly defined: from ovos_utils.network_utils import get_external_ip, is_valid_ip
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check how is_valid_ip is used in geolocation.py and verify alignment

# Find the geolocation file and check usage
echo "Checking for geolocation.py file..."
fd -t f "geolocation.py$"

# Check how is_valid_ip is used
echo "Checking is_valid_ip usage..."
rg -A 5 "is_valid_ip" 

# Look for any error handling patterns around IP validation
echo "Checking error handling patterns..."
ast-grep --pattern 'try {
  $$$
  is_valid_ip($$$)
  $$$
} catch'

Length of output: 1460

ovos_utils/geolocation.py Outdated Show resolved Hide resolved
ovos_utils/geolocation.py Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (1)
ovos_utils/geolocation.py (1)

105-105: Offer assistance for implementing language support

There's a TODO comment indicating that language support is needed for handling official names reported in various languages. Implementing this feature will enhance the localization capabilities of the geolocation service.

Would you like assistance in implementing multilingual support for official names? I can help develop a solution or open a GitHub issue to track this task.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 08ac7ff and 8101b4a.

📒 Files selected for processing (1)
  • ovos_utils/geolocation.py (1 hunks)
🧰 Additional context used
🪛 Ruff
ovos_utils/geolocation.py

65-65: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


88-88: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


155-155: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


244-244: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

ovos_utils/geolocation.py Show resolved Hide resolved
ovos_utils/geolocation.py Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (4)
ovos_utils/geolocation.py (4)

106-106: Address TODO comment for language support

The TODO comment indicates missing language support for official names. Would you like me to help implement multi-language support for official names?


99-105: Simplify nested dictionary access

Consider using a helper function to handle the nested dictionary access with fallbacks. This would make the code more readable and maintainable.

+ def get_nested_value(data: dict, *keys: str, default: str = "") -> str:
+     """Helper function to safely get nested dictionary values with multiple fallbacks."""
+     for key_list in keys:
+         result = data
+         for k in key_list.split('.'):
+             result = result.get(k, {}) if isinstance(result, dict) else {}
+         if result and not isinstance(result, dict):
+             return result
+     return default

- place_type = details.get("extratags", {}).get("linked_place") or details.get("category") or data.get(
-     "type") or data.get("class")
+ place_type = get_nested_value(details, 
+     "extratags.linked_place", 
+     "category",
+     "type",
+     "class")

171-176: Simplify city name resolution logic

Consider extracting the city name resolution logic into a helper function for better maintainability:

+ def resolve_city_name(address: dict) -> str:
+     """Helper function to resolve city name from address fields."""
+     city_fields = ['city', 'village', 'town', 'hamlet', 'county']
+     return next((address.get(field) for field in city_fields if address.get(field)), "")

- "name": address.get("city") or
-         address.get("village") or
-         address.get("town") or
-         address.get("hamlet") or
-         address.get("county") or "",
+ "name": resolve_city_name(address),

230-238: Simplify language normalization logic

Consider extracting the language normalization logic into a helper function:

+ def normalize_ip_api_language(lang: str) -> str:
+     """Normalize language code for ip-api.com."""
+     supported_langs = {"en", "de", "es", "fr", "ja", "ru"}
+     special_cases = {"pt": "pt-BR", "zh": "zh-CN"}
+     
+     lang = standardize_lang_tag(lang).split("-")[0]
+     if lang in special_cases:
+         return special_cases[lang]
+     return lang if lang in supported_langs else "en"

- lang = standardize_lang_tag(lang).split("-")[0]
- if lang not in ["en", "de", "es", "pt", "fr", "ja", "zh", "ru"]:
-     LOG.warning(f"Language unsupported by ip-api.com ({lang}), defaulting to english")
-     lang = "en"
- elif lang == "pt":
-     lang = "pt-BR"
- elif lang == "zh":
-     lang = "zh-CN"
+ normalized_lang = normalize_ip_api_language(lang)
+ if normalized_lang != lang:
+     LOG.warning(f"Language unsupported by ip-api.com ({lang}), using {normalized_lang}")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8101b4a and 0c8f1ac.

📒 Files selected for processing (1)
  • ovos_utils/geolocation.py (1 hunks)
🧰 Additional context used
🪛 Ruff
ovos_utils/geolocation.py

66-66: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


89-89: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


157-157: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


246-246: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

🔇 Additional comments (3)
ovos_utils/geolocation.py (3)

28-37: Reuse TimezoneFinder instance for better performance

The previous review comment about reusing the TimezoneFinder instance is still valid. Creating a new instance on every function call is resource-intensive.


63-64: Update User-Agent headers for Nominatim compliance

The previous review comment about updating User-Agent headers to comply with Nominatim's usage policy is still valid.


242-245: Security Concern: Use HTTPS for IP geolocation

The previous review comment about using a geolocation service that supports HTTPS is still valid.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (4)
ovos_utils/geolocation.py (4)

69-95: Consider using a session for multiple requests

The function makes two separate requests to Nominatim. Using a session would improve performance and connection reuse.

+    session = requests.Session()
+    session.headers.update({"User-Agent": "OVOS/1.0", "Accept-Language": lang})
+
     try:
-        response = requests.get(url, params={"q": location, "format": "json", "limit": 1},
-                                headers={"User-Agent": "OVOS/1.0", "Accept-Language": lang}, timeout=timeout)
+        response = session.get(url, params={"q": location, "format": "json", "limit": 1},
+                             timeout=timeout)

204-206: Simplify coordinate handling and move type conversion earlier

The type conversion and coordinate fallback logic could be simplified and validated earlier.

+    lat = float(details.get("lat") or lat)
+    lon = float(details.get("lon") or lon)
     location["timezone"] = get_timezone(
-        lat=float(details.get("lat") or lat),
-        lon=float(details.get("lon") or lon))
+        lat=lat, lon=lon)

237-244: Simplify language normalization logic

The language normalization could be simplified using a mapping dictionary.

+    LANG_MAPPING = {
+        "pt": "pt-BR",
+        "zh": "zh-CN"
+    }
     lang = standardize_lang_tag(lang).split("-")[0]
     if lang not in ["en", "de", "es", "pt", "fr", "ja", "zh", "ru"]:
         LOG.warning(f"Language unsupported by ip-api.com ({lang}), defaulting to english")
         lang = "en"
-    elif lang == "pt":
-        lang = "pt-BR"
-    elif lang == "zh":
-        lang = "zh-CN"
+    else:
+        lang = LANG_MAPPING.get(lang, lang)

263-277: Make response structure consistent with other geolocation functions

The response structure differs slightly from other geolocation functions (missing 'address' field).

     return {"city": city_data,
             "coordinate": coordinate_data,
-            "timezone": timezone_data}
+            "timezone": timezone_data,
+            "address": f"{data['city']}, {data['regionName']}, {data['country']}"}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 0c8f1ac and f021340.

📒 Files selected for processing (1)
  • ovos_utils/geolocation.py (1 hunks)
🔇 Additional comments (1)
ovos_utils/geolocation.py (1)

16-49: LGTM! Well-structured timezone lookup implementation.

The implementation is robust with proper coordinate validation, efficient resource handling through lazy loading, and comprehensive error handling.

ovos_utils/geolocation.py Show resolved Hide resolved
@JarbasAl JarbasAl merged commit 77bc115 into dev Nov 19, 2024
8 of 9 checks passed
@JarbasAl JarbasAl deleted the feat/geolocaiton branch November 19, 2024 11:53
@coderabbitai coderabbitai bot mentioned this pull request Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant