Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Place:
- def __init__(self, place):
- self.id = place["_id"]
- self.photos = place["result"].get("photos", [])
- self.photos = self.make_photos()
- def make_photos(self):
- photo_objects = []
- photos = self.photos
- if not photos:
- return []
- for photo in photos:
- photo = Photo(photo)
- photo.place_id_db = self.id
- photo_objects.append(photo)
- return photo_objects
- class Photo:
- def __init__(self, photo):
- self.photo = photo
- self.photo_reference = photo["photo_reference"]
- self.max_width = photo["width"]
- self.max_height = photo["height"]
- self.url_exists_in_db = False
- self.url = self.photo.get("url")
- self.place_id_db = None # set by Place
- def get_url(self):
- if self.url:
- self.url_exists_in_db = True
- p("Url is already retrieved")
- return self.url
- p("Retrieving url from API")
- base = "https://maps.googleapis.com/maps/api/place/photo"
- params = {
- "photoreference": self.photo_reference,
- "maxheight": self.max_height,
- "maxwidth": self.max_width,
- "key": API_key
- }
- response = requests.get(base, params=params)
- self.url = response.url
- return self.url
- def download(self):
- url = self.get_url()
- photo = requests.get(url)
- photo_content = photo.content
- with open(f"photos/{self.photo_reference}.jpg", "wb") as photo:
- photo.write(photo_content)
- def save_url_to_db(self):
- if self.url_exists_in_db:
- p("Url exists in db")
- return
- p("Saving url to db")
- detailed_google_places_afula_db.update_one(
- {"_id": self.place_id_db},
- {
- "$set": {
- "result.photos.$[p].url": self.url
- }
- },
- array_filters=[ {"p.photo_reference": self.photo_reference} ],
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement