-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_scraper.py
More file actions
70 lines (49 loc) · 1.75 KB
/
api_scraper.py
File metadata and controls
70 lines (49 loc) · 1.75 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
from app.models import User, RecipeCategory, Recipe
from app import app, db
from dotenv import load_dotenv
import requests
load_dotenv()
def convert_api_obj(obj):
ingredients = []
for i in range(1, 21):
if obj[f'strIngredient{i}'] != '':
ingredients.append(
f"{obj[f'strMeasure{i}']} {obj[f'strIngredient{i}']}")
ingredients = ' | '.join(ingredients)
category = RecipeCategory.query.filter(
RecipeCategory.category == obj['strCategory']).first()
category_id = 10
if category != None:
category_id = category.id
recipeData = {
'title': obj['strMeal'],
'ingredients': ingredients,
'instructions': obj['strInstructions'],
'image_src': obj['strMealThumb'],
'category_id': category_id,
'from_url': obj['strYoutube'],
'user_id': 2
}
print(recipeData)
recipe = Recipe(**recipeData)
db.session.add(recipe)
alphabet = 'abcdefghjklmnopqrstuvwxyz'
# with app.app_context():
# for letter in alphabet:
# r = requests.get(
# 'https://www.themealdb.com/api/json/v2/9973533/search.php', params={'f': letter})
# data = r.json().get('meals')
# if data != None:
# for meal in data:
# convert_api_obj(meal)
# db.session.commit()
# for letter in alphabet:
url = "https://bigovenvolodimir-kudriachenkov1.p.rapidapi.com/getRandomRecipe"
payload = ""
headers = {
'x-rapidapi-host': "BigOvenvolodimir-kudriachenkoV1.p.rapidapi.com",
'x-rapidapi-key': "5cbad61472mshf96e6552c76437bp102f16jsnd63329b999b1",
'content-type': "application/x-www-form-urlencoded"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)