-
Notifications
You must be signed in to change notification settings - Fork 1
/
zomato.py
44 lines (35 loc) · 1.34 KB
/
zomato.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
from bs4 import BeautifulSoup
import requests
import socket
import re
def fetch(url, config):
menu = ""
names = []
prices = []
try:
url = re.sub(r"\?.*/?(daily-menu|denní-menu)?$", "/denní-menu", url)
result = requests.get(url, timeout=config["HTTP_TIMEOUT"], headers={
"User-Agent": config["ZOMATO_UA"],
"Accept-Language": config["ZOMATO_LANG"],
"Referer": url
})
html = BeautifulSoup(result.content, "html5lib")
day = html.findAll("div", {"class": "tmi-group"})[0]
restaurant = html.select("h1.res-name a")[0].text.strip()
for j in day.findAll("div", {"class": "tmi-daily"}):
names.append(j.findAll("div", {"class": "tmi-name"})[0].text.strip())
prices.append(j.findAll("div", {"class": "tmi-price"})[0].text.strip())
for i in range(len(names)):
line = "- " + names[i] + " " + prices[i]
menu += line + "\n"
if len(menu) > 0:
return "\n#### " + restaurant + ":\n\n" + menu
else:
return ""
except socket.timeout:
return "" + url + ": timeout :angry:\n"
except IndexError:
# return empty string when there is no menu for today
return ""
except Exception:
return "" + url + ": something went wrong :dizzy_face:\n"