-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from vxshxk/save-dish-pics
Save dish pics
- Loading branch information
Showing
9 changed files
with
103 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
from django.contrib import admin | ||
from .models import FoodImage | ||
from .models import Recipe | ||
from .models import DishImage | ||
|
||
admin.site.register(FoodImage) | ||
admin.site.register(Recipe) | ||
admin.site.register(Recipe) | ||
admin.site.register(DishImage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Generated by Django 4.2.11 on 2024-06-22 20:59 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('recipify', '0005_recipe_image'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='DishImage', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('image', models.ImageField(upload_to='images/')), | ||
('recipe', models.OneToOneField(null=True, on_delete=django.db.models.deletion.CASCADE, to='recipify.recipe')), | ||
], | ||
), | ||
] |
18 changes: 18 additions & 0 deletions
18
recipify/recipify/migrations/0007_alter_dishimage_image.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 4.2.11 on 2024-06-23 11:32 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('recipify', '0006_dishimage'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='dishimage', | ||
name='image', | ||
field=models.URLField(), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,45 @@ | ||
import requests | ||
import json | ||
from PIL import Image | ||
import base64 | ||
from io import BytesIO | ||
|
||
# def add_base64_padding(base64_string): | ||
# missing_padding = len(base64_string) % 4 | ||
# if missing_padding: | ||
# base64_string += '=' * (4 - missing_padding) | ||
# return base64_string | ||
|
||
def add_base64_padding(base64_string): | ||
missing_padding = len(base64_string) % 4 | ||
if missing_padding: | ||
base64_string += '=' * (4 - missing_padding) | ||
return base64_string | ||
|
||
def decode_base64_to_image(base64_string): | ||
base64_string = add_base64_padding(base64_string) | ||
image_data = base64.b64decode(base64_string) | ||
image = Image.open(BytesIO(image_data)) | ||
return image | ||
# def decode_base64_to_image(base64_string): | ||
# base64_string = add_base64_padding(base64_string) | ||
# image_data = base64.b64decode(base64_string) | ||
# image = Image.open(BytesIO(image_data)) | ||
# return image | ||
|
||
def getDishImage(dishname): | ||
url = "https://api.serphouse.com/serp/live?q=" + dishname + " image&engine=google_images&lang=en&device=desktop&serp_type=web&loc=Alba,Texas,United States&loc_id=1026201&verbatim=0&gfilter=0&page=1&num_result=10" | ||
client_ID = "94XalRwyaaUtAh8AuRoE_gp3QmvntuuFI_otx2Wsvso" | ||
url = "https://api.unsplash.com/search/photos/?client_id=" + client_ID + "&query=" + dishname | ||
|
||
|
||
payload = {} | ||
headers = { | ||
'Authorization': 'Bearer nddeuNFHxu2hxN0wzW8euCXbUOawG25WLnpNFdiggt8q4wPKiWTx8AS9T1Iw' | ||
} | ||
try: | ||
response = requests.request("GET", url) | ||
response.raise_for_status() # Raises an HTTPError if the HTTP request returned an unsuccessful status code | ||
ans = json.loads(response.text) | ||
image_url = ans['results'][0]['urls']['regular'] | ||
return image_url | ||
except requests.exceptions.RequestException as e: | ||
# Handle any requests exceptions (e.g., network issues, invalid responses) | ||
print(f"An error occurred with the request: {e}") | ||
except json.JSONDecodeError as e: | ||
# Handle JSON decoding errors | ||
print(f"An error occurred while decoding the JSON response: {e}") | ||
except (KeyError, IndexError) as e: | ||
# Handle errors related to accessing specific elements in the JSON response | ||
print(f"An error occurred while accessing the JSON data: {e}") | ||
except Exception as e: | ||
# Handle any other unforeseen exceptions | ||
print(f"An unexpected error occurred: {e}") | ||
|
||
response = requests.request("GET", url, headers=headers, data=payload) | ||
ans = json.loads(response.text) | ||
image_base_64 = ans['results']['results']['inline_images'][0]['image'] | ||
return image_base_64 | ||
return None | ||
|
||
# Example usage | ||
# dish_image_base64 = getDishImage("Kiwi Salsa") | ||
# print(dish_image_base64) | ||
# image = decode_base64_to_image(dish_image_base64) | ||
# image.show() | ||
# Test | ||
# img = getDishImage("Kiwi Grill ") | ||
# print(img) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.