-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI_modul.py
115 lines (95 loc) · 5.53 KB
/
API_modul.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
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
import requests
import csv
from urllib.parse import urlparse, urlunparse
from googletrans import Translator
def translate_text(text, target_language='ru'):
translator = Translator()
translated_text = translator.translate(text, dest=target_language)
return translated_text.text
class APIModule:
def __init__(self, api_key, db_connection, db_cursor):
self.api_key = api_key
self.url = "https://food-recipes-with-images.p.rapidapi.com/"
self.headers = {
"X-RapidAPI-Key": api_key,
"X-RapidAPI-Host": "food-recipes-with-images.p.rapidapi.com"
}
self.db_connection = db_connection
self.db_cursor = db_cursor
def fetch_recipes(self, query):
querystring = {"q": query}
try:
response = requests.get(self.url, headers=self.headers, params=querystring)
response.raise_for_status() # Вызывает исключение, если ответ сервера не успешен
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error fetching recipes from API: {e}")
return None
def convert_to_csv(self, recipes_data):
try:
count_recipes_query = "SELECT COUNT(*) FROM recipes;"
self.db_cursor.execute(count_recipes_query)
current_count = self.db_cursor.fetchone()[0]
if current_count >= 100:
print(f"Limit reached. Not adding more recipes. Current count: {100}")
return
recipes = recipes_data['d']
fieldnames = ['id', 'Title', 'Ingredients', 'Instructions', 'Image', 'description']
with open('recipes.csv', 'w', newline='', encoding='utf-8') as csvfile:
csv_writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
csv_writer.writeheader()
for recipe in recipes:
# Преобразование 'Ingredients' из словаря в список строк с номерами
ingredients_list = [f"{i}. {value}" for i, (_, value) in
enumerate(recipe['Ingredients'].items(), start=1)]
ingredients_str = '\n'.join(ingredients_list)
recipe['Ingredients'] = ingredients_str
# Объединение 'Ingredients' и 'Instructions' в 'description'
recipe[
'description'] = f"Ingredients:\n{recipe['Ingredients']}\n\nInstructions: {recipe['Instructions']}"
# Приведение изображения к формату file_url
recipe['Image'] = f'https:{recipe["Image"]}'
csv_writer.writerow(recipe)
return 'recipes.csv'
except Exception as e:
print(f"Error converting to CSV: {e}")
return None
def add_recipes_to_database(self, csv_filename):
try:
fieldnames = ['id', 'Title', 'Ingredients', 'Instructions', 'Image', 'description']
with open(csv_filename, 'r', encoding='utf-8') as csvfile:
csv_reader = csv.DictReader(csvfile)
for row in csv_reader:
# Преобразование 'Ingredients' в словарь, если это строка
if isinstance(row['Ingredients'], str):
# Преобразование строки в словарь, предполагая, что элементы разделены запятыми
ingredients_dict = {str(i): value.strip() for i, value in
enumerate(row['Ingredients'].split(','), start=1)}
row['Ingredients'] = ingredients_dict
# Преобразование 'Ingredients' из словаря в список строк с номерами
ingredients_list = [f"{i}. {value}" for i, (_, value) in
enumerate(row['Ingredients'].items(), start=1)]
ingredients_str = '\n'.join(ingredients_list)
row['Ingredients'] = ingredients_str
# Объединение 'Ingredients' и 'Instructions' в 'description'
row['description'] = f"Ingredients:\n{row['Ingredients']}\n\nInstructions: {row['Instructions']}"
# Перевод 'description' на русский
translated_description = translate_text(row['description'])
row['description'] = translated_description
# Изменен запрос на добавление в базу данных
insert_recipe_query = "INSERT INTO recipes (title, description, photo_url) VALUES (%s, %s, %s);"
self.db_cursor.execute(insert_recipe_query, (
row['Title'], row['description'], row['Image']
))
self.db_connection.commit()
print("Recipes added to the database successfully.")
except Exception as e:
print(f"Error adding recipes to the database: {e}")
def fix_url_schema(url, default_scheme='https'):
parsed_url = urlparse(url)
if not parsed_url.scheme:
# Если схема отсутствует, используем default_scheme
fixed_url = urlunparse((default_scheme,) + parsed_url[1:])
else:
fixed_url = url
return fixed_url