-
Notifications
You must be signed in to change notification settings - Fork 0
/
restaurants.py
132 lines (103 loc) · 3.6 KB
/
restaurants.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
#
# Many thanks to Pavel Grochal (https://github.com/Darkless012) who provided the parsers
# for Zomato.cz and Menicka.cz !!!
#
import urllib
import os
import json
from bs4 import BeautifulSoup
import datetime
def zomato(url):
menu = []
try:
zomato_apikey = ENV['ZOMATO_APIKEY']
except:
# use export ZOMATO_APIKEY=<zomato_api_key> before you start this application locally
zomato_apikey = os.environ.get('ZOMATO_APIKEY')
HEADERS = {
'user_key': zomato_apikey,
'Accept': 'application/json',
'User-Agent': 'Mozilla/5.0'
}
zomato_request = urllib.request.Request(url, headers=HEADERS)
zomato_response = urllib.request.urlopen(zomato_request)
zomato_data = json.load(zomato_response)
zomato_response.close()
try:
dishes = zomato_data.get('daily_menus', [{}])[0].get('daily_menu', {}).get('dishes', [{}])
for x in dishes:
dish = x.get('dish', {})
menu.append([dish.get('name'), dish.get('price')])
except:
pass
return menu
def menicka(url):
menu = []
menicka_request = urllib.request.urlopen(url)
menicka_response = menicka_request.read()
menicka_request.close()
soup = BeautifulSoup(menicka_response, "lxml")
# try:
menicka = soup.select('.menicka')
try:
if len(menicka) >= 1:
first_menu = menicka[0]
today_menu = first_menu.find_all("li")
completion = []
for item in today_menu:
food = item.find('div', {'class': 'polozka'})
if food is not None:
completion.append([food.get_text()])
price = item.find('div', {'class': 'cena'})
if price is not None:
completion[-1].append(price.get_text())
for item in completion:
if len(item) == 0:
continue
if len(item) == 1:
menu.append([item[0], ""])
if len(item) >= 2:
menu.append([item[0], item[1]])
except:
pass
return menu
def bernard():
menu = []
bernard_file = urllib.request.urlopen("https://www.bernardpub.cz/pub/andel")
bernard_html = bernard_file.read()
bernard_file.close()
body = BeautifulSoup(bernard_html, "lxml")
datetab = body.find(text=datetime.datetime.today().__format__('%-d. %-m.'))
if datetab:
menu_id = datetab.parent.parent['data-tab-target']
else:
menu_id = body.find(attrs={"class": "day-selection-tab"})['id']
for food in body.find(id=menu_id).find_all("div", {"class": "single-food"}):
name = food.strong.contents[0]
price = food.find_all("span", {"class": "food-price"})[0].contents[0]
menu.append([name, price])
return menu
def run(filename):
with open('pages/'+filename) as f:
restaurants = json.load(f)
for restaurant in restaurants:
if restaurant['type'] == 'bernard':
try:
restaurant['menu'] = bernard()
except:
pass
elif restaurant['type'] == 'zomato':
try:
restaurant['menu'] = zomato(
"https://developers.zomato.com/api/v2.1/dailymenu?res_id=" + restaurant['zomatoId'])
except:
pass
elif restaurant['type'] == 'menicka':
try:
restaurant['menu'] = menicka(restaurant['menickaLink'])
except:
pass
elif restaurant['type'] == 'link':
pass
return restaurants