random_street_view.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import requests
  2. from .shared import point_has_streetview, PointSource
  3. rsv_url = "https://randomstreetview.com/data"
  4. def call_random_street_view(only_america=False):
  5. """
  6. Returns an array of (some number of) tuples, each being (latitude, longitude).
  7. All points will be valid streetview coordinates. There is no guarantee as to the
  8. length of this array (it may be empty), but it will never be None.
  9. This function calls the streetview metadata endpoint - there is no quota consumed.
  10. """
  11. rsv_js = requests.post(rsv_url, data={"country": "us" if only_america else "all"}).json()
  12. if not rsv_js["success"]:
  13. return []
  14. return [
  15. (point["lat"], point["lng"])
  16. for point in rsv_js["locations"]
  17. if point_has_streetview(point["lat"], point["lng"])
  18. ]
  19. class RSVPointSource(PointSource):
  20. def __init__(self, stock_target=20, only_america=False):
  21. super().__init__(stock_target=stock_target)
  22. self.only_america = only_america
  23. def _restock_impl(self, n):
  24. points = []
  25. while len(points) < n:
  26. points.extend(call_random_street_view(only_america=self.only_america))
  27. return points