~rmcbride/+junk/gourmet

« back to all changes in this revision

Viewing changes to src/lib/importers/html_plugins/recipezaar.py

  • Committer: Rick McBride
  • Date: 2008-10-28 20:42:31 UTC
  • Revision ID: rick.mcbride@canonical.com-20081028204231-mdka2rvtac1u82he
initial push of experimental branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from gourmet import convert
 
2
import re
 
3
import os.path,sys
 
4
sys.path.append(os.path.split(os.path.split(__file__)[0])[0])
 
5
import html_importer
 
6
from html_helpers import *
 
7
 
 
8
SUPPORTED_URLS = ['www.recipezaar.com']
 
9
SUPPORTED_URLS_REGEXPS = []
 
10
 
 
11
recipezaar_group = None
 
12
 
 
13
# UNICODE was causing some nasty hanging w/ recipezaar...
 
14
NON_UNICODE_NUMBER_REGEXP = '([\d/]([\d/ ]*[\d])?)'
 
15
 
 
16
def recipezaar_ingredient_parser (text, tag):
 
17
    global recipezaar_group
 
18
    ret = {}
 
19
    if recipezaar_group: ret['inggroup']=recipezaar_group
 
20
    grouptag = tag.fetch('td',{'colspan':'3'})
 
21
    # And groups are in <h4> tags
 
22
    if grouptag: grouptag = grouptag[0].fetch('h4')
 
23
    if grouptag:
 
24
        recipezaar_group = html_importer.get_text(grouptag[0])
 
25
        return
 
26
    keytag = tag.fetch('span','food')
 
27
    if keytag:
 
28
        keytag = keytag[0]
 
29
        ret['ingkey'] = html_importer.get_text(keytag)
 
30
    amttag = tag.fetch('td','amt')
 
31
    if amttag:
 
32
        amttag = amttag[0]
 
33
        ret['amount'] = html_importer.get_text(amttag)
 
34
    ret['text'] = ret.get('amt','')+' '+html_importer.get_text(tag.fetch('td')[-1])
 
35
    ret['text'] = ret['text'].strip()
 
36
    ret['text'] = re.sub('\s+',' ',ret['text'])
 
37
    print 'ingparser',text,'->',ret
 
38
    return ret
 
39
 
 
40
def cut_title (title, tagname):
 
41
    """Cut recipe # from title
 
42
 
 
43
    Title's are often followed by the recipezaar recipe # which we
 
44
    don't really want.
 
45
    """
 
46
    i = title.find(' Recipe #')
 
47
    if i > -1:
 
48
        return title[:i]
 
49
    else:
 
50
        return title
 
51
 
 
52
RULES = [
 
53
    ['title',
 
54
     [{'tag':'h1'}],
 
55
     'text',
 
56
     cut_title
 
57
     ],
 
58
    ['source',
 
59
     [{'tag':'div',
 
60
       'attributes':{'class':'submitter'},
 
61
       },
 
62
      {'tag':'a',
 
63
       'index':0,},
 
64
      ],
 
65
     'text',
 
66
     ],
 
67
    ['image',
 
68
     [{'tag':'img',
 
69
       'attributes':{'alt':'Recipe Photo'}}],
 
70
     'src',
 
71
     ],
 
72
    ['servings',
 
73
     [{'tag':'input',
 
74
       'attributes':{'name':'scaleto'}}],
 
75
     'value'],
 
76
    ['instructions',
 
77
     [{'tag':'div',
 
78
       'attributes':{'class':'recipetext'},
 
79
       }],
 
80
     'text'],
 
81
    ['ingredient_parsed',
 
82
     [{'tag':'table',
 
83
       'attributes':{'class':'ingredients'}},
 
84
      {'tag':'tr',
 
85
       'index':[0,None]}],
 
86
     'text',
 
87
     recipezaar_ingredient_parser,
 
88
     ],
 
89
    ['servings',
 
90
     #[{'regexp':'%(num)s+\s+servings?'%{'num':convert.NUMBER_REGEXP}}],
 
91
     [{'tag':'div',
 
92
       'attributes':{'id':'servings'}},
 
93
      {'tag':'div',
 
94
       'attributes':{'class':'qty'}},
 
95
      ],
 
96
     'text',
 
97
     ('(%(num)s+).+'%{'num':'[\d/]'},True)],
 
98
    ['cooktime',
 
99
     [{'tag':'div',
 
100
       'attributes':{'id':'time'},
 
101
       'index':[0,None]},
 
102
      ],
 
103
     'text',
 
104
     ],
 
105
    ['preptime',
 
106
     [{'tag':'div',
 
107
       'attributes':{'id':'time'},
 
108
       },
 
109
      {'tag':'span',
 
110
       'attributes':{'class':'prep'},
 
111
       },
 
112
      ],
 
113
     'text',
 
114
     ('(.*)\s+prep',False),
 
115
     ],
 
116
    ['instructions',
 
117
     [{'tag':'span',
 
118
       'attributes':{'class':'recipetext'},
 
119
       'index':[0,None]
 
120
       }],
 
121
     'text',
 
122
     ],
 
123
    ['instructions',
 
124
     [{'tag':'div',
 
125
       'attributes':{'class':'steps'},
 
126
       'index':[0,None],
 
127
       },],
 
128
     'text'],
 
129
    ['category',
 
130
     [{'tag':'div',
 
131
       'attributes':{'id':'recipecats'},},
 
132
      {'tag':'li',
 
133
       'regexp':'Course',
 
134
       'moveto':'parent'
 
135
       },
 
136
      {'tag':'a',
 
137
       'index':-1}],
 
138
     'text'
 
139
     ],
 
140
    ['cuisine',
 
141
     [{'tag':'div',
 
142
       'attributes':{'id':'recipecats'},},
 
143
      {'tag':'li',
 
144
       'regexp':'Cuisine',
 
145
       'moveto':'parent'
 
146
       },
 
147
      {'tag':'a',
 
148
       'index':-1}],
 
149
     'text',
 
150
     ],
 
151
    ]
 
152
    
 
153
    
 
154