artwork = Artwork(record) if artwork.precise_address: continue response = None if artwork.lat and artwork.long: response = requests.get(GEOCODE_URL + '&latlng=' + requests.utils.quote(artwork.lat + ',' + artwork.long)) elif artwork.address: response = requests.get(GEOCODE_URL + '&address=' + requests.utils.quote(artwork.address)) if response: response = json.loads(response.text) if response['status'] == 'OK': result = response['results'][0] print(result) artwork.precise_address = result['formatted_address'] artwork.precise_lat = result['geometry']['location']['lat'] artwork.precise_long = result['geometry']['location']['lng'] artwork.place_id = result['place_id'] records[i] = artwork.get_fields() with open('artwork.json', 'w') as outfile: json.dump(records, outfile, indent=4)
for image_url in image_urls: suffix = '' if image == 0 else '-alt' suffix += '' if image <= 1 else str(image) _, ext = path.splitext(image_url) ext = ext.lower() ext = '.jpg' if ext == '.jpeg' else ext filename = artwork.id + suffix + ext if path.exists(filename): print(f'{filename} already exists') else: print(f'downloading {image_url} to {filename}') try: urlretrieve(image_url, path.join(image_dir, filename)) except HTTPError as e: print(e.msg) continue artwork.remote_images.append(image_url) artwork.add_image(filename) image += 1 artworks.append(artwork) data.append(artwork.get_fields()) with open('artwork.json', 'w') as outfile: json.dump(data, outfile, indent=4)