clone3 commited on
Commit
5ba3fb4
·
verified ·
1 Parent(s): aa2eef6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -24
app.py CHANGED
@@ -4,6 +4,11 @@ import geoip2.database
4
  import os
5
  from pathlib import Path
6
 
 
 
 
 
 
7
  app = FastAPI()
8
 
9
  # Paths to your downloaded databases (adjust if needed)
@@ -17,16 +22,16 @@ asn_reader = geoip2.database.Reader(str(ASN_DB_PATH))
17
 
18
  @app.get("/track")
19
  async def track_ip(request: Request):
20
- # Get client IP (handle proxies)
21
  client_ip = request.headers.get("X-Forwarded-For")
22
  if client_ip:
23
  client_ip = client_ip.split(",")[0].strip()
24
  else:
25
  client_ip = request.client.host
26
 
27
- # Browser / Device info
28
  ua_string = request.headers.get("User-Agent", "Unknown")
29
  ua = parse(ua_string)
 
30
  browser_info = {
31
  "browser": ua.browser.family,
32
  "version": ua.browser.version_string,
@@ -37,47 +42,47 @@ async def track_ip(request: Request):
37
  "is_bot": ua.is_bot
38
  }
39
 
40
- # Geolocation lookup (offline!)
41
- geo_data = {"error": "IP not found"}
42
  asn_data = {}
43
 
44
  try:
45
- city_response = city_reader.city(client_ip)
46
  geo_data = {
47
- "country": city_response.country.name,
48
- "country_code": city_response.country.iso_code,
49
- "city": city_response.city.name,
50
- "region": city_response.subdivisions.most_specific.name,
51
- "region_code": city_response.subdivisions.most_specific.iso_code,
52
- "postal": city_response.postal.code,
53
  "location": {
54
- "latitude": city_response.location.latitude,
55
- "longitude": city_response.location.longitude,
56
- "accuracy_radius": city_response.location.accuracy_radius,
57
- "timezone": city_response.location.time_zone
58
  }
59
  }
60
- except geoip2.errors.AddressNotFoundError:
61
- pass
62
- except Exception as e:
63
- geo_data = {"error": str(e)}
64
 
65
  try:
66
- asn_response = asn_reader.asn(client_ip)
67
  asn_data = {
68
- "asn": asn_response.autonomous_system_number,
69
- "org": asn_response.autonomous_system_organization
70
  }
71
  except:
72
- pass
73
 
74
- return {
75
  "ip": client_ip,
76
  "browser": browser_info,
77
  "geolocation": geo_data,
78
  "asn": asn_data
79
  }
80
 
 
 
 
 
 
 
 
 
81
  # Optional: close readers on shutdown (good practice)
82
  @app.on_event("shutdown")
83
  def close_readers():
 
4
  import os
5
  from pathlib import Path
6
 
7
+ GOOGLE_SHEET_WEBHOOK = os.getenv("GOOGLE_SHEET_WEBHOOK")
8
+
9
+ if not GOOGLE_SHEET_WEBHOOK:
10
+ raise RuntimeError("GOOGLE_SHEET_WEBHOOK environment variable not set")
11
+
12
  app = FastAPI()
13
 
14
  # Paths to your downloaded databases (adjust if needed)
 
22
 
23
  @app.get("/track")
24
  async def track_ip(request: Request):
25
+
26
  client_ip = request.headers.get("X-Forwarded-For")
27
  if client_ip:
28
  client_ip = client_ip.split(",")[0].strip()
29
  else:
30
  client_ip = request.client.host
31
 
 
32
  ua_string = request.headers.get("User-Agent", "Unknown")
33
  ua = parse(ua_string)
34
+
35
  browser_info = {
36
  "browser": ua.browser.family,
37
  "version": ua.browser.version_string,
 
42
  "is_bot": ua.is_bot
43
  }
44
 
45
+ geo_data = {}
 
46
  asn_data = {}
47
 
48
  try:
49
+ city = city_reader.city(client_ip)
50
  geo_data = {
51
+ "country": city.country.name,
52
+ "city": city.city.name,
53
+ "region": city.subdivisions.most_specific.name,
 
 
 
54
  "location": {
55
+ "latitude": city.location.latitude,
56
+ "longitude": city.location.longitude
 
 
57
  }
58
  }
59
+ except:
60
+ geo_data = {}
 
 
61
 
62
  try:
63
+ asn = asn_reader.asn(client_ip)
64
  asn_data = {
65
+ "asn": asn.autonomous_system_number,
66
+ "org": asn.autonomous_system_organization
67
  }
68
  except:
69
+ asn_data = {}
70
 
71
+ payload = {
72
  "ip": client_ip,
73
  "browser": browser_info,
74
  "geolocation": geo_data,
75
  "asn": asn_data
76
  }
77
 
78
+ async with httpx.AsyncClient(timeout=5) as client:
79
+ try:
80
+ await client.post(GOOGLE_SHEET_WEBHOOK, json=payload)
81
+ except Exception as e:
82
+ print("Sheet error:", e)
83
+
84
+ return payload
85
+
86
  # Optional: close readers on shutdown (good practice)
87
  @app.on_event("shutdown")
88
  def close_readers():