Browse Source

Fixing a bug in the generate_coord logic

Kirk Trombley 5 năm trước cách đây
mục cha
commit
052a6b2acd
1 tập tin đã thay đổi với 7 bổ sung7 xóa
  1. 7 7
      server/lib.py

+ 7 - 7
server/lib.py

@@ -10,24 +10,24 @@ metadata_url = "https://maps.googleapis.com/maps/api/streetview/metadata"
 mapcrunch_url = "http://www.mapcrunch.com/_r/"
 
 
-def generate_coord():
+def generate_coord(max_retries=100000):
     """
     Returns (latitude, longitude) of usable coord (where google has data).
-    This function will run until a suitable coordinate is found.
+    This function will attempt at most max_retries calls to map crunch to fetch
+    candidate points, and will exit as soon as a suitable candidate is found.
 
     This function calls the streetview metadata endpoint - there is no quota consumed
     """
-    points_res = requests.get(mapcrunch_url).text
-    points_js = json.loads(points_res.strip("while(1); "))
-
-    while True:
+    for _ in range(max_retries):
+        points_res = requests.get(mapcrunch_url).text
+        points_js = json.loads(points_res.strip("while(1); "))
         for lat, lng in points_js["points"]:
             params = {
                 "key": google_api_key,
                 "location": f"{lat},{lng}",
             }
             js = requests.get(metadata_url, params=params).json()
-            if js["status"] != "ZERO_RESULTS":
+            if js["status"] == "OK":
                 return (lat, lng)