class Dish(object): def __init__(self,caller,locu_object ='',searchTime='',**attrs): self.venue_client = VenueApiClient(KEY) self.locu_object = locu_object self.parse_locu(self.locu_object) if caller == 'search': self.load_rating_from_id() elif usage == "menu": self.load_rating_from_venue() def get_attr(self,attr): self.attrs.get(attr,"Data Not Available") def parse_locu(self,locu_object): """ Takes Locu Object and creates attributes dictonary. args: locu_object: a json style dict with dish information """ self.name = self.locu_object.get("name") self.description = self.locu_object.get("description") self.price = self.locu_object.get("price") self.venue = self.locu_object["venue"].get("name") self.venue_id = self.locu_object["venue"].get("id") self.id = self.locu_object.get("id") details = self.venue_client.get_details(self.locu_object["venue"]["id"]) # if self.searchDay: # hours_today = details["objects"][0]["open_hours"][self.searchDay] # if hours_today: # self.attrs["available"] = LocuHelper.isOpen(hours_today,self.searchTime) def load_rating_from_id(self): """ load the rating and number of ratings from database. """ pass def load_rating_from_venue(self): pass
global KEY KEY = '2d36afa81b05f641ec3382d9992b8cec3d64a4e4' s_type = sys.argv[1].strip() if s_type =="v" or s_type == 'm': print "in if" client = VenueApiClient(KEY) elif s_type =="d": client = MenuItemApiClient(KEY) raw_in= sys.argv[2].strip() search_terms = raw_in.split(',') resp = client.search(name =search_terms[0],locality=search_terms[1],region=search_terms[2]) if s_type == "v" or s_type =="d": print json.dumps(resp,indent=4, sort_keys=True) else: dets = client.get_details(resp["objects"][0]["id"]) print json.dumps(dets,indent=4, sort_keys=True)
def load_rating_from_id(self): """ load the rating and number of ratings from database. """ pass def load_rating_from_venue(self): pass if __name__ == '__main__': # dish = Dish() venue_client = VenueApiClient(KEY) menu_item_client = MenuItemApiClient(KEY) menu_items = menu_item_client.search(locality = 'San Francisco', name = 'espresso', price__gte = 6) item = menu_items['objects'][0] venueID = item["venue"]["id"] details = venue_client.get_details('dc241e328c5cc445aea5') # hours = details["objects"][0]["open_hours"]["Monday"] # dish.parse_open_hours(hours)
class Venue(object): def __init__(self,locu_info,caller,searchTime=None): """ If caller is a search locu_info will just be a json dict if it's a venue it'll be a venue id this is to prevent making a ton of venue detail calls """ self.venue_client = VenueApiClient(KEY) self.caller = caller self.menu = [] ## Filled by set_menu() if caller == 'search': self.locu_object = locu_info elif caller == "venue": self.locu_object = self.venue_client.get_details(locu_info)["objects"][0] self.set_attrs(caller) if searchTime: self.searchTime = time.strftime( "%H:%M:%S",searchTime) self.searchDay = time.strftime("%A",searchTime) def get_attr(self,attr): instance_variables = self.__dict__ return instance_variables.get(attr) def set_attrs(self,caller): """ Sets the Venue attrs. It will only set the menu if the caller is a venue page. This is for two reasons: a) to avoid extraneous venue detail calls b) the locu_object will be a json style dict that won't have a menu entry """ dna = "Data Not Available" self.name = self.locu_object.get("name",dna) self.address = self.locu_object.get("street_address",dna) self.postal_code = self.locu_object.get("postal_code",dna) self.locality = self.locu_object.get("locality",dna) self.region = self.locu_object.get("region",dna) self.phone = self.locu_object.get("phone",dna) self.venue_id = self.locu_object.get("id",dna) self.lat = self.locu_object.get("lat",dna) self.long = self.locu_object.get("long",dna) self.has_menu = self.locu_object.get("has_menu",dna) self.categories = self.locu_object.get("categories") if self.has_menu and caller=='venue': self.set_menu() # if self.searchDay: # hours_today = details["objects"][0]["open_hours"][self.searchDay] # if hours_today: # self.attrs["available"] = LocuHelper.isOpen(hours_today,self.searchTime) def set_menu(self): """ Parse the locu json and creates a list of Dish objects Must be called when venue page is loaded. We don't do it in the init so we don't have to parse a menu for all the venues in a query. """ venue_query = DishEntry.objects.filter(venue_id=self.venue_id) for menu in self.locu_object["menus"]: for section in menu["sections"]: for sub_section in section["subsections"]: for item in sub_section["contents"]: item["venue"] = self.locu_object if item["type"] == "ITEM": dish = Dish(item,queryset=venue_query) self.menu.append(dish)