-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyummlyCall.rkt
More file actions
173 lines (130 loc) · 5.83 KB
/
yummlyCall.rkt
File metadata and controls
173 lines (130 loc) · 5.83 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#lang racket
(require "yummlyAPI.rkt")
(require net/url)
(require json)
(provide yummlySearch
counter
reset-yummly
add-ingredients
addToBlacklist
getRecipes
getRecipeAttribute
iterateCounter)
;; Search api: http://api.yummly.com/v1/api/recipes?_app_id=app-id&_app_key=app-key&your _search_parameters
;; Get api: http://api.yummly.com/v1/api/recipe/recipe-id?_app_id=YOUR_ID&_app_key=YOUR_APP_KEY
(define yummlyID (string-append "?_app_id=" appID "&_app_key=" appKey))
(define yummlySearch (string-append "http://api.yummly.com/v1/api/recipes" yummlyID))
(define counter 1)
(define recipeDetails "")
(define (reset-yummly)
(set! yummlySearch (string-append "http://api.yummly.com/v1/api/recipes" yummlyID)))
(define (iterateCounter)
(if (= counter 10)
(set! counter 1)
(set! counter (+ counter 1))))
;; add-ingredients will take a list of strings to parse and add to the API call url
;; ex. (add-ingredients (list "chicken" "eggs" "cheese"))
(define (add-ingredients items)
(if (null? items)
yummlySearch
(begin
(when (not (or (null? (car items)) (equal? "" (car items))))
(set! yummlySearch (string-append yummlySearch "&allowedIngredient[]=" (car items))))
(add-ingredients (cdr items)))))
;; addToBlacklist will take a list of strings to parse and add to the API call url
;; ex. (addToBlacklist (list "peanuts" "milk"))
(define (addToBlacklist items)
(if (null? items)
yummlySearch
(begin
(when (not (and (null? (car items)) (equal? "" (car items))))
(set! yummlySearch (string-append yummlySearch "&excludedIngredient[]=" (car items))))
(addToBlacklist (cdr items)))))
;; Once all information has been added from the GUI to the yummlyURL, call this procedure
;; to actually send out the API call.
(define (getRecipes)
(define in (get-pure-port (string->url yummlySearch)))
(define response-string (port->string in))
(close-input-port in)
(getAllMatches response-string))
;; Once we have a JSON response, this procedure will parse the results and give us
;; the information that we are looking for.
(define (getAllMatches response)
;; This next line should get a list of recipes, need to loop through them like nobody's biznes
(begin
(define recipes (hash-ref (string->jsexpr response) 'matches))
;;(printf (jsexpr->string (car recipes)))
;;(parseRecipes recipes 'id '())))
(getSingleRecipe recipes 1)))
;;(hash-ref (cadr recipes) 'sourceDisplayName))
(define (getSingleRecipe recipes current)
(if (= counter current)
;; Do something with the recipe (in (car recipes))
(getRecipeDetails (hash-ref (car recipes) 'id))
(if (null? (cdr recipes))
(getSingleRecipe recipes (counter))
(getSingleRecipe (cdr recipes) (+ current 1)))))
;; This will parse a list of all recipes (10).
;; - recipes: the json list of recipes recieved from getAllMatches procedure
;; - toSearch: a symbol that will parse the data that you want (ex. 'sourceDisplayName)
;; - result: the resulting list with the data from each recipe, should be given empty list
;; ex. (parseRecipes recipes 'sourceDisplayName '())
(define (parseRecipes recipes toSearch result)
(if (null? recipes)
result
(begin
(set! result (append result (list (car recipes))))
(parseRecipes (cdr recipes) toSearch result))))
;; Mostly working, gets JSON
(define (getRecipeDetails recipeID)
(let ([yummlyGet (string-append "http://api.yummly.com/v1/api/recipe/" recipeID yummlyID)])
(define in (get-pure-port (string->url yummlyGet)))
(set! recipeDetails (port->string in))
(close-input-port in)))
(define (getRecipeAttribute attribute)
(cond
[(eq? attribute 'image)
(begin
(define images (hash-ref (string->jsexpr recipeDetails) 'images))
(getImage images))]
[(eq? attribute 'nutrition)
(begin
(define nutritionalInfo (hash-ref (string->jsexpr recipeDetails) 'nutritionEstimates))
;;(printf (jsexpr->string nutritionalInfo))
(nutritionToString nutritionalInfo ""))]
[(eq? attribute 'ingredients)
(begin
(define ingredients (hash-ref (string->jsexpr recipeDetails) 'ingredientLines))
(ingredientsToString ingredients ""))]
[(eq? attribute 'url)
(hash-ref (hash-ref (string->jsexpr recipeDetails) 'source) 'sourceRecipeUrl)]
[else (hash-ref (string->jsexpr recipeDetails) attribute)]))
(define (ingredientsToString ingredientsList stringToReturn)
(if (null? ingredientsList)
stringToReturn
(if (null? (cdr ingredientsList))
(ingredientsToString
(cdr ingredientsList)
(string-append stringToReturn (car ingredientsList)))
(ingredientsToString
(cdr ingredientsList)
(string-append stringToReturn (car ingredientsList) "\n")))))
(define nutritionWeCareAbout '("ENERC_KJ" "CHOCDF" "PROCNT" "SUGAR" "NA" "ENERC_KJ"))
(define (nutritionToString infoList stringToReturn)
(if (null? infoList)
stringToReturn
(let ([nutritionName (hash-ref (car infoList) 'attribute)])
(if (careAboutThis? nutritionName nutritionWeCareAbout)
(let ([nutriDesc (hash-ref (car infoList) 'description)]
[nutriValue (hash-ref (car infoList) 'value)]
[nutriUnits (hash-ref (hash-ref (car infoList) 'unit) 'plural)])
(nutritionToString (cdr infoList) (string-append stringToReturn nutriDesc ": " (number->string nutriValue) " " nutriUnits "\n")))
(nutritionToString (cdr infoList) stringToReturn)))))
(define (careAboutThis? careItem careList)
(if (null? careList)
#f
(if (equal? careItem (car careList))
#t
(careAboutThis? careItem (cdr careList)))))
(define (getImage imageListJson)
(hash-ref (car imageListJson) 'hostedMediumUrl))