/
app.py
57 lines (48 loc) · 1.79 KB
/
app.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
from flask import Flask, jsonify, request
from recipe_scrapers import scrape_me
import logging
app = Flask(__name__)
# Variable "application" is picked up by uWSGI server
application = app
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0')
@app.get("/api/v1/scrape-recipe")
def import_recipe():
scrape_url = request.args.get("url")
try:
scraper = scrape_me(scrape_url)
except Exception as e:
logging.warn("Error while scraping " + scrape_url)
logging.info("Trying with wild mode")
try:
scraper = scrape_me(scrape_url, wild_mode=True)
except Exception as e:
logging.error("Error retrying with wild mode")
return "", 501
response = {
"language": getInfo(scraper, "language"),
"title": getInfo(scraper, "title"),
"category": getInfo(scraper, "category"),
"author": getInfo(scraper, "author"),
"total_time": getInfo(scraper, "total_time"),
"cook_time": getInfo(scraper, "cook_time"),
"prep_time": getInfo(scraper, "prep_time"),
"yields": getInfo(scraper, "yields"),
"imgage": getInfo(scraper, "image"),
"ingredients": getInfo(scraper, "ingredients"),
"nutrients": getInfo(scraper, "nutrients"),
"instructions": getInfo(scraper, "instructions"),
"ratings": getInfo(scraper, "ratings"),
"cuisine": getInfo(scraper, "cuisine"),
"host": getInfo(scraper, "host"),
"links": getInfo(scraper, "links"),
}
return jsonify(response)
def getInfo(scraper, attribute):
try:
method = getattr(scraper, attribute, None)
return method()
except Exception as e:
logging.warn(
"Error getting info from scraper for attribute " + attribute)
return ""