Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
aisbf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexlab
aisbf
Commits
ad27385e
Commit
ad27385e
authored
Apr 24, 2026
by
Stefy Lanza (nextime / spora )
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add IP geolocation service with caching
parent
8a71da25
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
0 deletions
+43
-0
geolocation.py
geolocation.py
+43
-0
No files found.
geolocation.py
0 → 100644
View file @
ad27385e
import
asyncio
import
httpx
from
typing
import
Dict
,
Optional
# Global cache for IP -> country mappings
_ip_country_cache
:
Dict
[
str
,
str
]
=
{}
async
def
get_ip_country
(
ip
:
str
)
->
Optional
[
str
]:
"""Get country code for IP address using ipapi.co, with caching.
Returns country code (e.g., 'IL') or None if failed.
"""
if
ip
in
_ip_country_cache
:
return
_ip_country_cache
[
ip
]
try
:
async
with
httpx
.
AsyncClient
(
timeout
=
5.0
)
as
client
:
response
=
await
client
.
get
(
f
"http://ipapi.co/{ip}/country/"
)
if
response
.
status_code
==
200
:
country
=
response
.
text
.
strip
()
.
upper
()
_ip_country_cache
[
ip
]
=
country
return
country
except
Exception
:
pass
# Cache failure to avoid repeated calls
_ip_country_cache
[
ip
]
=
None
return
None
def
is_ip_israeli
(
ip
:
str
)
->
bool
:
"""Check if IP is from Israel."""
# Run async function in sync context if needed
try
:
loop
=
asyncio
.
get_event_loop
()
if
loop
.
is_running
():
# If loop is running, we can't use run_until_complete
# For middleware, we'll need to handle this differently
return
False
# Temporary fallback
else
:
country
=
loop
.
run_until_complete
(
get_ip_country
(
ip
))
return
country
==
'IL'
except
Exception
:
return
False
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment