예제 #1
0
 def get_stores(self, brand_id=None):
     """
     Get json stores from pisspricer api
     :param brand_id: Optional brand id filter
     :return: List of json objects
     """
     params = {}
     if brand_id is not None:
         params = {"brandId": brand_id}
     res = req.get(self.api.url + "/stores", headers=self.api.headers, params=params)
     return res.json()
예제 #2
0
    def get_regions(self):
        """
        Get a list of regions from pisspricer api
        :return: List of region dict objects from pisspricer api
        """
        regions_res = req.get(self.api.url + "/regions",
                              headers=self.api.headers)
        if regions_res.status != 200:
            raise custom_exceptions.AiohttpException(regions_res, "get_regions", "pisspricer")
        regions = regions_res.json()

        return regions
예제 #3
0
 def get_locations():
     """
     Get a json list of liquorland locations
     :return: List of liquorland store locations
     """
     task = "get_locations"
     url = "https://www.liquorland.co.nz/themes/liquorland/scripts/StoreFinder/branches.json?v6"
     res = req.get(url)
     if res.status != 200:
         raise LiquorlandException(res, task)
     locations = res.json()
     new_locations = []
     for location in locations:
         if location["BranchType"] is not None:
             new_locations.append(location)
     return new_locations
예제 #4
0
    def upload_new_stores(self, locations, brand_id, printer=None):
        """
        Posts stores to pisspricer api that are new (based on internal id).
        Gets data from google maps api if location data is incomplete.
        Uploads region if it doesn't already exist.
        :param printer: (print_function, total, title) for printing
        :param brand_id: Brand id of store locations
        :param locations: List of dict objects
            {
                name: "required|string",
                url: "required|string",
                region: "required|string",
                region_lat: "numeric",
                region_lng: "numeric",
                address: "required|string",
                postcode: "numeric",
                lattitude: "numeric",
                longitude: "numeric"
                internalId: "string"
            }
        :return: None
        """
        # Get current locations
        cur_locations_res = req.get(self.api.url + "/stores",
                                    headers=self.api.headers,
                                    params={"brandId": brand_id})
        if cur_locations_res.status != 200:
            raise custom_exceptions.AiohttpException(cur_locations_res, "get stores", "pisspricer")
        cur_locations = cur_locations_res.json()

        # Create a set of internal ids
        cur_locs_set = set()
        for loc in cur_locations:
            cur_locs_set.add(loc["internalId"])

        # Get a list of regions
        regions = self.get_regions()

        # Print first iteration
        if printer is not None:
            print_func, total, task = printer
            print_func(0, total, task)

        # Create a list of new stores
        new_locations = []
        for i, loc in enumerate(locations):
            try:
                # Check if the store is new
                if loc["internalId"] not in cur_locs_set:

                    # Get location if data not supplied
                    region = loc["region"]
                    lat = loc["lattitude"]
                    lng = loc["longitude"]
                    postcode = loc["postcode"]
                    address = loc["address"]
                    location_list = [region, lat, lng, postcode, address]
                    if any(item is None for item in location_list):
                        lat, lng, address, postcode, region = tools.geocode_address(f"{loc['name']}, {address}")

                    # Create new location dict
                    new_loc = {
                        "name": loc["name"],
                        "url": loc["url"],
                        "brandId": brand_id,
                        "regionId": self._get_region_id(regions, region, lat=loc["region_lat"], lng=loc["region_lng"]),
                        "lattitude": lat,
                        "longitude": lng,
                        "postcode": postcode,
                        "address": address,
                        "internalId": loc["internalId"]
                    }

                    # Add new store to task list
                    new_locations.append([self.api.url + "/stores", new_loc])

            except custom_exceptions.GoogleApiException as err:
                tools.log_error(err)
            except custom_exceptions.AiohttpException as err:
                tools.log_error(err)
            finally:
                if printer is not None:
                    print_func(i+1, total, task)

        # Post all stores
        # TODO Change post function
        iteration = [0]
        print_func, _, task = printer
        kwargs = {"headers": self.api.headers,
                  "printer": (print_func, len(new_locations), task),
                  "iteration": iteration}
        responses = asyncio.run(req.create_async_tasks(new_locations, kwargs, self._async_post_json))
        for res in responses:
            if res.status != 201:
                tools.log_error(custom_exceptions.AiohttpException(res, "post stores", "pisspricer"))