12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- import json
- import requests
- from .shared import point_has_streetview, PointSource
- mapcrunch_url = "http://www.mapcrunch.com/_r/"
- def generate_coord(max_retries=100, only_america=False):
- """
- Returns (latitude, longitude) of usable coord (where google has data).
- 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.
- If no suitable candidate is found in this allotted number of retries, None is
- returned.
- This function calls the streetview metadata endpoint - there is no quota consumed.
- """
- mc_url = mapcrunch_url + ("?c=21" if only_america else "")
- for _ in range(max_retries):
- points_res = requests.get(mc_url).text
- points_js = json.loads(points_res.strip("while(1); "))
- if "c=" not in mc_url:
- mc_url += f"?c={points_js['country']}" # lock to the first country randomed
- for lat, lng in points_js["points"]:
- if point_has_streetview(lat, lng):
- return (lat, lng)
- class MapCrunchPointSource(PointSource):
- def __init__(self, stock_target=20, max_retries=100, only_america=False):
- super().__init__(stock_target=stock_target)
- self.max_retries = max_retries
- self.only_america = only_america
- def _restock_impl(self, n):
- points = []
- while len(points) < n:
- pt = generate_coord(
- max_retries=self.max_retries,
- only_america=self.only_america
- )
- if pt is not None:
- points.append(pt)
- return points
|