Skip to content

Instantly share code, notes, and snippets.

@ChaiBapchya
Last active March 17, 2023 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChaiBapchya/e2d65552e25acc1d32347e9874941ae2 to your computer and use it in GitHub Desktop.
Save ChaiBapchya/e2d65552e25acc1d32347e9874941ae2 to your computer and use it in GitHub Desktop.
Find restaurants near the given partial post code just-eat API
import requests
MIN_AVERAGE_RATING = 4.5
MIN_RATING_COUNT = 100
MAX_DELIVERY_ETA_MINS_UPPER = 45
TARGET_CUISINE = 'pizza'
class InvalidPostcodeException(Exception):
pass
class NoRestaurantFoundException(Exception):
pass
var = input("Please enter partial postcode of office location: ")
print("you entered", var)
# need to pass headers to prevent 403 forbidden request error
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
r = requests.get("https://uk.api.just-eat.io/restaurants/bypostcode/"+str(var), headers=headers)
print(r.status_code)
if r.status_code!=200:
# coud try-catch if need be
raise InvalidPostcodeException()
restaurants = r.json()["Restaurants"]
if not len(restaurants):
raise NoRestaurantFoundException()
results = []
for restaurant in restaurants:
restaurant_rating = restaurant["Rating"]
filter1 = restaurant_rating["Average"]>MIN_AVERAGE_RATING and restaurant_rating["Count"]>=MIN_RATING_COUNT
filter2 = restaurant["DeliveryEtaMinutes"]["RangeUpper"]<=MAX_DELIVERY_ETA_MINS_UPPER
cuisines = restaurant["Cuisines"]
filter3 = False
for cuisine in cuisines:
if cuisine["SeoName"] == TARGET_CUISINE:
filter3 = True
break
if filter1 and filter2 and filter3:
special_address = restaurant["Address"]["FirstLine"]+", "+restaurant["Address"]["Postcode"].split(' ')[0]
results.append((restaurant["Name"],restaurant["Url"],special_address))
for result in results:
print(result[0]+' | '+result[1]+' | '+result[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment