random_street_view.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import random
  2. import logging
  3. from .shared import aiohttp_client, point_has_streetview, ExhaustedSourceError
  4. RSV_URL = "https://randomstreetview.com/data"
  5. VALID_COUNTRIES = ("ad", "au", "ar", "bd", "be", "bt", "bw",
  6. "br", "bg", "kh", "ca", "cl", "hr", "co",
  7. "cz", "dk", "ae", "ee", "fi", "fr", "de",
  8. "gr", "hu", "hk", "is", "id", "ie", "it",
  9. "il", "jp", "lv", "lt", "my", "mx", "nl",
  10. "nz", "no", "pe", "pl", "pt", "ro", "ru",
  11. "sg", "sk", "si", "za", "kr", "es", "sz",
  12. "se", "ch", "tw", "th", "ua", "gb", "us")
  13. logger = logging.getLogger(__name__)
  14. async def call_random_street_view(country_lock, max_attempts=5):
  15. """
  16. Returns an array of (some number of) tuples, each being (latitude, longitude).
  17. All points will be valid streetview coordinates, in the country indicated by
  18. country_lock. If the country_lock provided is None, a valid one is chosen at
  19. random. The returned array will never be empty. If after max_attempts no points
  20. can be found (which is very rare), this raises an ExhaustedSourceError.
  21. This function calls the streetview metadata endpoint - there is no quota consumed.
  22. """
  23. if country_lock is None:
  24. country_lock = random.choice(VALID_COUNTRIES)
  25. for _ in range(max_attempts):
  26. logger.info("Attempting RSV...")
  27. try:
  28. async with aiohttp_client.post(RSV_URL, data={"country": country_lock.lower()}) as response:
  29. rsv_js = await response.json(content_type=None)
  30. logger.info(f"Got back {rsv_js.keys()}")
  31. except:
  32. logger.exception("Failed RSV")
  33. continue
  34. if not rsv_js["success"]:
  35. continue
  36. points = [
  37. (country_lock, point["lat"], point["lng"])
  38. for point in rsv_js["locations"]
  39. if await point_has_streetview(point["lat"], point["lng"])
  40. ]
  41. if len(points) > 0:
  42. return points
  43. else:
  44. raise ExhaustedSourceError()