| """Menu feed example used in | |
| https://developers.google.com/maps-booking/verticals/dining/guides/tutorials/tutorial-menu-feed-protos#python. | |
| """ | |
| import json | |
| from generated import food_menu_pb2 | |
| from google.protobuf.json_format import MessageToDict | |
| # create feed | |
| feed = food_menu_pb2.FoodMenuFeed() | |
| # add a menu component to feed data | |
| menuComponent = feed.data.add() | |
| menuComponent.menu.menu_id = 'menu1' | |
| menuComponent.menu.merchant_ids.append('dining-1') | |
| menuDisplayName = menuComponent.menu.display_name.text.add() | |
| menuDisplayName.text = 'Menu' | |
| menuDisplayName.language_code = 'en-us' | |
| menuComponent.menu.language = 'en-us' | |
| menuComponent.menu.last_merchant_update_time.seconds = 1633621547 | |
| for i in ['appetizers', 'dinner']: | |
| menuComponent.menu.menu_section_ids.append(i) | |
| # add a menu section component to feed data | |
| sectionComponent = feed.data.add() | |
| sectionComponent.section.menu_section_id = 'appetizers' | |
| sectionDisplayName = sectionComponent.section.display_name.text.add() | |
| sectionDisplayName.text = 'Lunch Appetizers' | |
| sectionDisplayName.language_code = 'en-us' | |
| sectionComponent.section.menu_item_ids.append('breadsticks-sauce') | |
| # add a menu item component to feed data | |
| itemComponent = feed.data.add() | |
| itemComponent.item.menu_item_id = 'breadsticks-sauce' | |
| itemDisplayName = itemComponent.item.display_name.text.add() | |
| itemDisplayName.text = 'Breadsticks & Sauce' | |
| itemDisplayName.language_code = 'en-us' | |
| itemDescription = itemComponent.item.description.text.add() | |
| itemDescription.text = 'Breakfast basket w/ side of tomato sauce (size 6 or 12)' | |
| itemDescription.language_code = 'en-us' | |
| for i in ['breadstick-sm', 'breadstick-lg']: | |
| itemComponent.item.menu_item_option_set.menu_item_option_ids.append(i) | |
| for i in [ | |
| 'http://www.example.com/photos/breadsticks.jpg', | |
| 'http://www.example.com/photos/sauce.jpg', | |
| ]: | |
| itemImage = itemComponent.item.images.add() | |
| itemImage.uri = i | |
| # add a menu item option component to feed data | |
| optionComponent = feed.data.add() | |
| optionComponent.option.menu_item_option_id: 'breadstick-sm' | |
| optionComponent.option.value.property_type = ( | |
| food_menu_pb2.MenuItemOptionProperty.PropertyType.SIZE | |
| ) | |
| optionTextValue = optionComponent.option.value.text_val.text.add() | |
| optionTextValue.text = 'Small' | |
| optionTextValue.language_code = 'en-us' | |
| optionOffer = optionComponent.option.offer_set.offers.add() | |
| optionOffer.price.currency_code = 'USD' | |
| optionOffer.price.units = 8 | |
| optionOffer.price.nanos = 0 | |
| # Example testing for menu feed size | |
| # Protocol buffer message must be less than 2 GiB | |
| # https://protobuf.dev/programming-guides/proto-limits/ | |
| # It is recommended to not exceed 200 MB, as there is an Actions | |
| # Center limit of 200 MB per file after compression. | |
| if feed.ByteSize() > 200 * 1024 * 1024: | |
| # start new file | |
| print('starting new file...') | |
| feedJSON = json.dumps(MessageToDict(feed, preserving_proto_field_name=True)) | |
| print(feedJSON) |