map_crunch.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import json
  2. import requests
  3. from .shared import point_has_streetview, PointSource
  4. mapcrunch_url = "http://www.mapcrunch.com/_r/"
  5. def generate_coord(max_retries=100, only_america=False):
  6. """
  7. Returns (latitude, longitude) of usable coord (where google has data).
  8. This function will attempt at most max_retries calls to map crunch to fetch
  9. candidate points, and will exit as soon as a suitable candidate is found.
  10. If no suitable candidate is found in this allotted number of retries, None is
  11. returned.
  12. This function calls the streetview metadata endpoint - there is no quota consumed.
  13. """
  14. mc_url = mapcrunch_url + ("?c=21" if only_america else "")
  15. for _ in range(max_retries):
  16. points_res = requests.get(mc_url).text
  17. points_js = json.loads(points_res.strip("while(1); "))
  18. if "c=" not in mc_url:
  19. mc_url += f"?c={points_js['country']}" # lock to the first country randomed
  20. for lat, lng in points_js["points"]:
  21. if point_has_streetview(lat, lng):
  22. return (lat, lng)
  23. class MapCrunchPointSource(PointSource):
  24. def __init__(self, stock_target=20, max_retries=100, only_america=False):
  25. super().__init__(stock_target=stock_target)
  26. self.max_retries = max_retries
  27. self.only_america = only_america
  28. def _restock_impl(self, n):
  29. points = []
  30. while len(points) < n:
  31. pt = generate_coord(
  32. max_retries=self.max_retries,
  33. only_america=self.only_america
  34. )
  35. if pt is not None:
  36. points.append(pt)
  37. return points