def handle_venue(self, venue): venue_id = venue.api_configuration.digital_pour_venue_id location_number = venue.api_configuration.digital_pour_location_number self.url = self.URL.format(venue_id, location_number, self.APIKEY) data = self.fetch() taps = {tap.tap_number: tap for tap in venue.taps.all()} manufacturers = {} for entry in data: if not entry['Active']: # in the cooler, not on tap continue # 1. parse the tap tap_info = self.parse_tap(entry) try: tap = taps[tap_info['tap_number']] except KeyError: tap = Tap(venue=venue, tap_number=tap_info['tap_number']) tap.time_added = tap_info['added'] tap.time_updated = tap_info['updated'] tap.estimated_percent_remaining = tap_info['percent_full'] if tap_info['gas_type'] in [i[0] for i in Tap.GAS_CHOICES]: tap.gas_type = tap_info['gas_type'] else: tap.gas_type = '' # 2. parse the manufacturer, creating if needed parsed_manufacturer = self.parse_manufacturer(entry) try: manufacturer = manufacturers[parsed_manufacturer['name']] except KeyError: defaults = {} for field in ['location', 'logo_url', 'twitter_handle']: if parsed_manufacturer[field]: defaults[field] = parsed_manufacturer[field] manufacturer = self.get_manufacturer( name=parsed_manufacturer['name'], **defaults, ) manufacturers[manufacturer.name] = manufacturer # 3. get the beer, creating if necessary parsed_beer = self.parse_beer(entry) name = parsed_beer.pop('name') color_html = parsed_beer.pop('color', '') if color_html: # convert 0xabcde into #0abcde color_html = f'#{color_html[2:]:0>6}' parsed_beer['color_html'] = color_html else: # clear the color if unknown parsed_beer['color_html'] = '' LOG.debug( 'looking up beer: name %s, mfg %s, other data %s', name, manufacturer, parsed_beer, ) beer = self.get_beer( name, manufacturer, pricing=self.parse_pricing(entry), venue=venue, **parsed_beer, ) # 4. assign the beer to the tap tap.beer = beer tap.save()
def handle_venue(self, venue): location = venue.api_configuration.taphunter_location excluded_lists = venue.api_configuration.taphunter_excluded_lists self.url = self.URL.format(location) data = self.fetch() taps = {tap.tap_number: tap for tap in venue.taps.all()} manufacturers = {} use_sequential_taps = any((tap_info["serving_info"]["tap_number"] == "" for tap_info in data["taps"])) latest_timestamp = UTC.localize(datetime.datetime(1970, 1, 1, 12)) for index, entry in enumerate(data["taps"]): # 1. parse the tap tap_info = self.parse_tap(entry) # Is it in an excluded list? if (excluded_lists and entry.get("list", {"name": -1})["name"] in excluded_lists): LOG.debug( "Skipping %s because it is in excluded list %s", entry["beer"]["beer_name"], entry["list"]["name"], ) continue if use_sequential_taps: tap_number = index + 1 else: tap_number = tap_info["tap_number"] try: tap = taps[tap_number] except KeyError: tap = Tap(venue=venue, tap_number=tap_number) tap.time_added = tap_info["added"] tap.time_updated = tap_info["updated"] parsed_time = parse(tap_info["updated"]) if parsed_time > latest_timestamp: latest_timestamp = parsed_time if "percent_full" in tap_info: tap.estimated_percent_remaining = tap_info["percent_full"] else: tap.estimated_percent_remaining = None if "gas_type" in tap_info and tap_info["gas_type"] in [ i[0] for i in Tap.GAS_CHOICES ]: tap.gas_type = tap_info["gas_type"] else: tap.gas_type = "" # 2. parse the manufacturer, creating if needed parsed_manufacturer = self.parse_manufacturer(entry) try: manufacturer = manufacturers[parsed_manufacturer["name"]] except KeyError: kwargs = { key: val for key, val in parsed_manufacturer.items() if key != "name" and val } manufacturer = self.get_manufacturer( name=parsed_manufacturer["name"], **kwargs, ) manufacturers[manufacturer.name] = manufacturer # 3. get the beer, creating if necessary parsed_beer = self.parse_beer(entry) name = parsed_beer["name"] style = parsed_beer.pop("style", {}) if style: parsed_beer["style"] = f"{style['category']} - {style['name']}" color_srm = parsed_beer.pop("srm", 0) if color_srm: parsed_beer["color_srm"] = color_srm LOG.debug( "looking up beer: name %s, mfg %s, other data %s", name, manufacturer, parsed_beer, ) beer = self.get_beer( manufacturer=manufacturer, pricing=self.parse_pricing(entry), venue=venue, **parsed_beer, ) # 4. assign the beer to the tap tap.beer = beer tap.save() return latest_timestamp
def handle_venue(self, venue): location = venue.api_configuration.taphunter_location self.url = self.URL.format(location) data = self.fetch() taps = {tap.tap_number: tap for tap in venue.taps.all()} manufacturers = {} use_sequential_taps = any((tap_info['serving_info']['tap_number'] == '' for tap_info in data['taps'])) for index, entry in enumerate(data['taps']): # 1. parse the tap tap_info = self.parse_tap(entry) if use_sequential_taps: tap_number = index + 1 else: tap_number = tap_info['tap_number'] try: tap = taps[tap_number] except KeyError: tap = Tap(venue=venue, tap_number=tap_number) tap.time_added = tap_info['added'] tap.time_updated = tap_info['updated'] if 'percent_full' in tap_info: tap.estimated_percent_remaining = tap_info['percent_full'] else: tap.estimated_percent_remaining = None if 'gas_type' in tap_info and tap_info['gas_type'] in [ i[0] for i in Tap.GAS_CHOICES ]: tap.gas_type = tap_info['gas_type'] else: tap.gas_type = '' # 2. parse the manufacturer, creating if needed parsed_manufacturer = self.parse_manufacturer(entry) try: manufacturer = manufacturers[parsed_manufacturer['name']] except KeyError: kwargs = { key: val for key, val in parsed_manufacturer.items() if key != 'name' and val } manufacturer = self.get_manufacturer( name=parsed_manufacturer['name'], **kwargs, ) manufacturers[manufacturer.name] = manufacturer # 3. get the beer, creating if necessary parsed_beer = self.parse_beer(entry) name = parsed_beer.pop('name') # TODO (#37): map styles style = parsed_beer.pop('style', {}) if style: parsed_beer['api_vendor_style'] = \ f"{style['category']} - {style['name']}" color_srm = parsed_beer.pop('srm', 0) if color_srm: parsed_beer['color_srm'] = color_srm LOG.debug( 'looking up beer: name %s, mfg %s, other data %s', name, manufacturer, parsed_beer, ) beer = self.get_beer( name, manufacturer, pricing=self.parse_pricing(entry), venue=venue, **parsed_beer, ) # 4. assign the beer to the tap tap.beer = beer tap.save()
def handle_venue(self, venue: Venue) -> datetime.datetime: venue_id = venue.api_configuration.digital_pour_venue_id location_number = venue.api_configuration.digital_pour_location_number self.url = self.URL.format(venue_id, location_number, self.APIKEY) data = self.fetch() taps = {tap.tap_number: tap for tap in venue.taps.all()} self.update_date = UTC.localize(datetime.datetime(1970, 1, 1, 0, 0, 0)) manufacturers = {} for entry in data: if not entry["Active"]: # in the cooler, not on tap continue # 1. parse the tap tap_info = self.parse_tap(entry) try: tap = taps[tap_info["tap_number"]] except KeyError: tap = Tap(venue=venue, tap_number=tap_info["tap_number"]) tap.time_added = tap_info["added"] tap.time_updated = tap_info["updated"] if tap.time_updated and tap.time_updated > self.update_date: LOG.debug("Updating venue timestamp to %s", tap.time_updated) self.update_date = tap.time_updated tap.estimated_percent_remaining = tap_info["percent_full"] if tap_info["gas_type"] in [i[0] for i in Tap.GAS_CHOICES]: tap.gas_type = tap_info["gas_type"] else: tap.gas_type = "" # 2. parse the manufacturer, creating if needed parsed_manufacturer = self.parse_manufacturer(entry) try: manufacturer = manufacturers[parsed_manufacturer["name"]] except KeyError: defaults = { field: parsed_manufacturer[field] for field in [ "location", "logo_url", "twitter_handle", "url", ] if parsed_manufacturer[field] } manufacturer = self.get_manufacturer( name=parsed_manufacturer["name"], **defaults, ) manufacturers[manufacturer.name] = manufacturer # 3. get the beer, creating if necessary parsed_beer = self.parse_beer(entry) name = parsed_beer.pop("name") color_html = parsed_beer.pop("color", "") if color_html: # convert 0xabcde into #0abcde color_html = f"#{color_html[2:]:0>6}" parsed_beer["color_html"] = color_html else: # clear the color if unknown parsed_beer["color_html"] = "" LOG.debug( "looking up beer: name %s, mfg %s, other data %s", name, manufacturer, parsed_beer, ) if name.casefold().strip() == "N/A".casefold(): if not parsed_beer.get("abv"): # it's an empty tap LOG.info("Tap %s is unused", tap.tap_number) tap.beer = None tap.save() continue beer = self.get_beer( name, manufacturer, pricing=self.parse_pricing(entry), venue=venue, **parsed_beer, ) # 4. assign the beer to the tap tap.beer = beer tap.save() return self.update_date