-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpersistent_helpers.py
More file actions
executable file
·123 lines (103 loc) · 3.41 KB
/
Copy pathpersistent_helpers.py
File metadata and controls
executable file
·123 lines (103 loc) · 3.41 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
#!flask/bin/python
import json
import os
import urllib2
import httplib2 as http
import requests
from creds import bigOvenAPIkey
try:
from urlparse import urlparse
except ImportError:
from urllib.parse import urlparse
from flask import Flask, jsonify
recipe_blob_dct = {
1 : {
'id': 1,
'title': 'Chicken Alfredo',
'description': 'Chicken & Pasta & Cream.',
'img': 'https://c1.staticflickr.com/3/2504/3874012191_48ec021023.jpg'
},
2 : {
'id': 2,
'title': 'Lasagna',
'description': 'Garfield\'s Favorite.',
'img': 'https://upload.wikimedia.org/wikipedia/commons/6/6b/Lasagna_(1).jpg'
},
3 : {
'id': 3,
'title': 'Pizza',
'description': 'Best served cold, just like revenge.',
'img': 'https://upload.wikimedia.org/wikipedia/commons/9/95/Pizza_with_various_toppings.jpg'
}
}
def get_recipe_ids():
"""Returns a list of valid recipe IDS.
@return: a JSON object - list of recipe IDs, where recipe ID is a number
"""
"""
1. Get listing of files in the directory
2. Make array of integers with the names of the files
"""
dirListing = os.listdir("recipeJSONs/")
lst = []
tempName = []
for item in dirListing:
if ".json" in item:
tempName = os.path.splitext(item)[0]
lst.append(int(tempName))
response = json.dumps({'response' : lst})
return response
def get_recipe_short_descriptions():
"""Returns a list of valid recipe IDS with small descriptions.
@return: a JSON object - list of recipe IDs, where recipe ID is a number
"""
"""
1. Get listing of files in the directory
2. Make array of integers with the names of the files
"""
dirListing = os.listdir("recipeJSONs/")
lst = []
tempName = []
for item in dirListing:
if ".json" in item:
tempName = os.path.splitext(item)[0]
with open(os.path.join("recipeJSONs", item)) as f:
recipeInfo = json.load(f)
lst.append({
"RecipeID": recipeInfo["RecipeID"],
"Title": recipeInfo["Title"],
"Description": recipeInfo["Description"],
"ImageURL": recipeInfo["ImageURL"]
})
response = json.dumps({'response' : lst})
return response
#return jsonify({'list': recipe_blob_dct.keys()})
def get_recipe_info(recipe_id):
"""Information about the recipe
@type recipe_id: number
@return: a JSON object
"""
"""
1. Get list of files
2. If recipe_id present in list, get the contents of the files
3. Else, make API call to get the JSON data from bigOven
4. jsonify the data and return
"""
dct = json.loads(get_recipe_ids())
lst = dct['response']
if recipe_id not in lst:
url_path = "http://api.bigoven.com/recipe/" + str(recipe_id) + "?api_key=" + bigOvenAPIkey
headers = {'content-type': 'application/json'}
req = requests.get(url_path, headers=headers)
content = req.content
recipe_info = json.loads(content)
else:
json_fname = "%d.json" % recipe_id
json_path = os.path.join("recipeJSONs", json_fname)
recipe_info_str = open(json_path).read()
recipe_info = json.loads(recipe_info_str)
return json.dumps({'response' : recipe_info})
def main():
print get_recipe_info(158905)
if __name__ == '__main__':
main()