Source code for api

# -*- coding: utf-8 -*-

from google_streetview import helpers
from os import path, makedirs
from pprint import pprint
try:
  from urllib.parse import urlencode
except ImportError:
  from urllib import urlencode

import json
import requests

[docs]class results: """Google Street View Image API results. Uses the `Google Street View Image API <https://developers.google.com/maps/documentation/streetview/>`_ to search for street level imagery. Args: params (listof dict): List of dict containing batch `street view URL parameters <https://developers.google.com/maps/documentation/streetview/intro>`_. site_api(str): The site for the URL request (example: https://maps.googleapis.com/maps/api/streetview). site_metadata(str): The site for the URL `metadata <https://developers.google.com/maps/documentation/streetview/metadata>`_ request (example: https://maps.googleapis.com/maps/api/streetview/metadata). Attributes: params (listof dict): Same as argument ``params`` for reference of inputs. links (listof str): List of str containing street view URL requests. metadata (listof dict): Objects returned from `street view metadata request <https://developers.google.com/maps/documentation/streetview/metadata>`_. metadata_links (listof str): List of str containing street view URL metadata requests. Examples: :: # Import google_streetview for the api module import google_streetview.api # Define parameters for street view api params = [{ 'size': '600x300', # max 640x640 pixels 'location': '46.414382,10.013988', 'heading': '151.78', 'pitch': '-0.76', 'key': 'your_dev_key' }] # Create a results object results = google_streetview.api.results(params) # Preview results results.preview() # Download images to directory 'downloads' results.download_links('downloads') # Save links results.save_links('links.txt') # Save metadata results.save_metadata('metadata.json') """ def __init__( self, params, site_api='https://maps.googleapis.com/maps/api/streetview', site_metadata='https://maps.googleapis.com/maps/api/streetview/metadata'): # (params) Set default params defaults = { 'size': '640x640' } for i in range(len(params)): for k in defaults: if k not in params[i]: params[i][k] = defaults[k] self.params = params # (image) Create image api links from parameters self.links = [site_api + '?' + urlencode(p) for p in params] # (metadata) Create metadata api links and data from parameters self.metadata_links = [site_metadata + '?' + urlencode(p) for p in params] self.metadata = [requests.get(url, stream=True).json() for url in self.metadata_links]
[docs] def preview(self, n=10, k=['date', 'location', 'pano_id', 'status'], kheader='pano_id'): """Print a preview of the request results. Args: n (int): Maximum number of requests to preview k (str): Keys in :class:`api.results`.metadata to preview kheader (str): Key in :class:`api.results`.metadata[``k``] to use as the header """ items = self.metadata # (cse_print) Print results for i, kv in enumerate(items[:n]): # (print_header) Print result header header = '\n[' + str(i) + '] ' + kv[kheader] print(header) print('=' * len(header)) # (print_metadata) Print result metadata for ki in k: if ki in kv: if ki == 'location': print(ki + ': \n lat: ' + str(kv[ki]['lat']) + '\n lng: ' + str(kv[ki]['lng'])) else: print(ki + ': ' + str(kv[ki]))
[docs] def save_metadata(self, file_path): """Save Google Street View metadata from parameter queries. Args: file_path (str): Path of the file with extension to save the :class:`api.results`.metadata """ with open(file_path, 'w+') as out_file: json.dump(self.metadata, out_file)