DebasishDhal99 commited on
Commit
4f15807
·
1 Parent(s): e615aaf

Add try-catch to location's coordinate finder block

Browse files

- `coords = loca.geocode(full_page)`, this was put inside a try-catch.
- cleaned up parameter names in backend apis.

Files changed (2) hide show
  1. frontend/src/components/Map.js +2 -2
  2. main.py +39 -29
frontend/src/components/Map.js CHANGED
@@ -132,10 +132,10 @@ const Map = ( { onMapClick, searchQuery, contentType, setSearchQuery, setSubmitt
132
  try{
133
  let endpoint;
134
  if (contentType === 'summary') {
135
- endpoint = `${BACKEND_URL}/wiki/${pageName}`;
136
  }
137
  else if (contentType === 'full') {
138
- endpoint = `${BACKEND_URL}/wiki/search/${pageName}`;
139
  }
140
 
141
  else {
 
132
  try{
133
  let endpoint;
134
  if (contentType === 'summary') {
135
+ endpoint = `${BACKEND_URL}/wiki/search/summary/${pageName}`;
136
  }
137
  else if (contentType === 'full') {
138
+ endpoint = `${BACKEND_URL}/wiki/search/full/${pageName}`;
139
  }
140
 
141
  else {
main.py CHANGED
@@ -33,38 +33,46 @@ full_page_cache = TTLCache(maxsize=100, ttl=300)
33
  def health_check():
34
  return {"status": "ok"}
35
 
36
- @app.get("/wiki/{page_name}")
37
- async def get_wiki_page(page_name: str, background_tasks: BackgroundTasks):
38
- if page_name in summary_cache:
39
  # print("Cache hit for summary:", page_name) #Working
40
- return JSONResponse(content=summary_cache[page_name], status_code=200)
41
- response = requests.get(f"https://en.wikipedia.org/api/rest_v1/page/summary/{page_name}", timeout=10)
 
42
 
43
- if response.status_code != 200:
44
- return JSONResponse(
45
- content={"error": "Page not found"},
46
- status_code=404
47
- )
48
-
49
- coords = loc.geocode(page_name)
50
-
51
- result = {
52
- "title": page_name,
53
- "content": f"{response.json().get('extract', 'No content available')}",
54
- "latitude": coords.latitude if coords else None,
55
- "longitude": coords.longitude if coords else None
56
- }
57
-
58
- background_tasks.add_task(lambda: summary_cache.__setitem__(page_name, result))
 
 
59
 
60
 
61
- return JSONResponse(
62
- content= result,
63
- status_code=200
64
- )
 
 
 
 
 
65
 
66
- @app.get("/wiki/search/{full_page}")
67
- def search_wiki(full_page: str, background_tasks: BackgroundTasks):
68
  if full_page in full_page_cache:
69
  # print("Cache hit for full_page:", full_page) #Working
70
  return JSONResponse(content=full_page_cache[full_page], status_code=200)
@@ -76,8 +84,10 @@ def search_wiki(full_page: str, background_tasks: BackgroundTasks):
76
  content={"error": "Page not found"},
77
  status_code=404
78
  )
79
-
80
- coords = loc.geocode(full_page)
 
 
81
 
82
  result = {
83
  "title": full_page,
 
33
  def health_check():
34
  return {"status": "ok"}
35
 
36
+ @app.get("/wiki/search/summary/{summary_page_name}")
37
+ async def get_wiki_summary(summary_page_name: str, background_tasks: BackgroundTasks):
38
+ if summary_page_name in summary_cache:
39
  # print("Cache hit for summary:", page_name) #Working
40
+ return JSONResponse(content=summary_cache[summary_page_name], status_code=200)
41
+ try:
42
+ response = requests.get(f"https://en.wikipedia.org/api/rest_v1/page/summary/{summary_page_name}", timeout=10)
43
 
44
+ if response.status_code != 200:
45
+ return JSONResponse(
46
+ content={"error": "Page not found"},
47
+ status_code=404
48
+ )
49
+ try:
50
+ coords = loc.geocode(summary_page_name, timeout=5)
51
+ except Exception as e:
52
+ coords = None
53
+
54
+ result = {
55
+ "title": summary_page_name,
56
+ "content": f"{response.json().get('extract', 'No content available')}",
57
+ "latitude": coords.latitude if coords else None,
58
+ "longitude": coords.longitude if coords else None
59
+ }
60
+
61
+ background_tasks.add_task(lambda: summary_cache.__setitem__(summary_page_name, result))
62
 
63
 
64
+ return JSONResponse(
65
+ content= result,
66
+ status_code=200
67
+ )
68
+ except Exception as e:
69
+ return JSONResponse(
70
+ content={"error": str(e), 'response': str(response)},
71
+ status_code=500
72
+ )
73
 
74
+ @app.get("/wiki/search/full/{full_page}")
75
+ def search_wiki_full_page(full_page: str, background_tasks: BackgroundTasks):
76
  if full_page in full_page_cache:
77
  # print("Cache hit for full_page:", full_page) #Working
78
  return JSONResponse(content=full_page_cache[full_page], status_code=200)
 
84
  content={"error": "Page not found"},
85
  status_code=404
86
  )
87
+ try:
88
+ coords = loc.geocode(full_page, timeout=5)
89
+ except Exception as e:
90
+ coords = None
91
 
92
  result = {
93
  "title": full_page,