-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathepicurious.py
67 lines (50 loc) · 2.1 KB
/
epicurious.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
#!/usr/bin/env python
"""
epicurious.py
This module inherits from RecipeParser, and provides an implementation
for parsing recipes from the epicurious.com site.
"""
import re
from parser import RecipeParser
class Epicurious(RecipeParser):
# define some patterns to match/filter
badTag = re.compile('Bon App\u00e9tit', re.I)
def getTitle(self):
"""The title format is:
<title>[Recipe] recipe | Epicurious.com</title>
we want just 'Recipe'
"""
return u' '.join(self.tree.xpath('//title')[0].text.split('|')[0].strip().split()[:-1])
def getImage(self):
"""The image format is:
<meta property="og:image" content="IMG_URL">
we want just 'IMG_URL'
"""
return self.tree.xpath('//meta[@property="og:image"]')[0].get('content')
def getIngredients(self):
"""Return a list or a map of the recipe ingredients"""
data = []
for node in self.tree.xpath('//li[@itemprop="ingredients"]'):
data.append(''.join(node.xpath('descendant-or-self::text()')).strip())
return data
def getDirections(self):
"""Return a list or a map of the preparation instructions"""
data = []
for node in self.tree.xpath('//li[@class="preparation-step"]'):
data.append(''.join(node.xpath('descendant-or-self::text()')).strip())
return data
def getTags(self):
"""Return a list of tags for this recipe"""
data = []
for node in self.tree.xpath('//*[@itemprop="recipeCategory"]'):
data.append(u''.join(node.xpath('descendant-or-self::text()')).strip())
return list(filter(lambda x: self.badTag.search(x) is None, data))
def getOtherRecipeLinks(self):
"""Return a list of other recipes found in the page"""
data = []
for link in self.tree.xpath('//div[contains(@class,"recipes")]/ul[contains(@class,"content")]/*/a'):
if 'href' in link.keys():
l = 'http://www.epicurious.com' + link.get('href')
if l not in data:
data.append(l)
return data