-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chrome extension and new APIs (#383)
* Initial parse-recipe-html tests * Function to parse html as a file * working version * Added QR code * scan page for test * Added qrcode generation and scanning * Fixed warnings * Fixed qrcode size and removed duplicate code * Fixed tests * UI adjustments to the chrome extnsion * Attempt to fix flaky test
- Loading branch information
1 parent
e9ab49c
commit 1c8817f
Showing
26 changed files
with
6,540 additions
and
372 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
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import logging | ||
import json | ||
|
||
import azure.functions as func | ||
|
||
from uuid import uuid4 | ||
from time import perf_counter | ||
|
||
from recipe_scrapers import scrape_html, AbstractScraper | ||
|
||
from .util import get_recipe_from_scraper | ||
|
||
bp = func.Blueprint() | ||
|
||
@bp.route(route="parse-recipe-html", methods=["POST"]) | ||
def parse_recipe_html(req: func.HttpRequest) -> func.HttpResponse: | ||
start = perf_counter() | ||
correlation_id = uuid4() | ||
|
||
url: str = req.form.get("url") | ||
html: str = req.form.get("html") | ||
scraper: AbstractScraper | ||
download_image: bool = req.form.get("downloadImage") or False | ||
try: | ||
logging.info(f"processing parse request id {correlation_id} for url: {html}") | ||
for file in req.files.values(): | ||
scraper = scrape_html(file.stream.read(), url, wild_mode=True) | ||
|
||
result = get_recipe_from_scraper(scraper, download_image) | ||
|
||
return func.HttpResponse(json.dumps(result), status_code=200, mimetype="application/json") | ||
except Exception as e: | ||
logging.error(f"Failed to process parse request id {correlation_id}. Error: {e}") | ||
|
||
return func.HttpResponse("Could not find a recipe in the web page", status_code=400) | ||
finally: | ||
end = perf_counter() | ||
logging.info(f"Finished processing parse request id {correlation_id}. Time taken: {end - start:0.4f}s") |
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 |
---|---|---|
|
@@ -11,4 +11,5 @@ pillow-avif-plugin~=1.4 | |
pytest~=8.3 | ||
azure-cosmos~=4.9 | ||
jsonschema~=4.23 | ||
lxml==5.1.0 | ||
lxml==5.1.0 | ||
qrcode==8.0 |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,79 @@ | ||
chrome.runtime.onMessage.addListener( | ||
function (request, sender, sendResponse) { | ||
if (request.contentScriptQuery == "parseRecipe") { | ||
parseRecipe(request.url, request.downloadImage, request.body, sendResponse); | ||
|
||
return true; // Will respond asynchronously. | ||
} | ||
}); | ||
|
||
function parseRecipe(url, downloadImage, body, sendResponse) { | ||
const postFile = new Blob([body], { | ||
type: 'application/x-object' | ||
}); | ||
|
||
const data = new FormData(); | ||
data.append('file', postFile); | ||
data.append('url', url); | ||
data.append('downloadImage', downloadImage); | ||
|
||
let options = { | ||
method: "POST", | ||
body: data | ||
}; | ||
|
||
fetch(`https://sharpcooking.lpains.net/api/parse-recipe-html`, options) | ||
.then(response => { | ||
if (response.ok) { | ||
return response.json(); | ||
} else { | ||
throw new Error(`Something went wrong: ${response.status}`); | ||
} | ||
}) | ||
.then(data => { | ||
shareParsedRecipe(data, sendResponse); | ||
}) | ||
.catch(error => { | ||
chrome.runtime.sendMessage( | ||
{ contentScriptQuery: "parseRecipeResult", error: error }, | ||
); | ||
console.log(error); | ||
}); | ||
} | ||
|
||
function shareParsedRecipe(parseResult, sendResponse) { | ||
const body = { | ||
title: parseResult.title, | ||
ingredients: parseResult.ingredients.map(item => item.raw), | ||
steps: parseResult.steps.map(item => item.raw), | ||
notes: "", | ||
source: parseResult.host ?? "imported from browser", | ||
media: [] | ||
}; | ||
let options = { | ||
method: "POST", | ||
headers: { 'content-type': 'application/json' }, | ||
body: JSON.stringify(body) | ||
}; | ||
|
||
fetch(`https://delightful-flower-0c3edd710-383.centralus.2.azurestaticapps.net/api/share-recipe`, options) | ||
.then(response => { | ||
if (response.ok) { | ||
return response.json(); | ||
} else { | ||
throw new Error(`Something went wrong: ${response.status}`); | ||
} | ||
}) | ||
.then(data => { | ||
sendResponse(data.id) | ||
chrome.runtime.sendMessage( | ||
{ contentScriptQuery: "parseRecipeResult", code: data.id }, | ||
); | ||
}) | ||
.catch(error => { | ||
chrome.runtime.sendMessage( | ||
{ contentScriptQuery: "parseRecipeResult", error: error }, | ||
); | ||
console.log(error); | ||
}); | ||
} |
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,3 @@ | ||
chrome.runtime.sendMessage( | ||
{ contentScriptQuery: "parseRecipe", url: document.URL, downloadImage: false, body: document.documentElement.outerHTML }, | ||
response => console.log(response)); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,25 @@ | ||
{ | ||
"name": "Sharp Cooking Recipe Importer", | ||
"version": "1.0.0", | ||
"description": "Import recipes from the webpage.", | ||
"manifest_version": 3, | ||
"author": "lpains", | ||
"permissions": [ | ||
"activeTab", | ||
"scripting" | ||
], | ||
"action": { | ||
"default_popup": "popup.html" | ||
}, | ||
"background": { | ||
"service_worker": "background.js" | ||
}, | ||
"host_permissions": [ | ||
"https://sharpcooking.lpains.net/api/*", | ||
"https://delightful-flower-0c3edd710-383.centralus.2.azurestaticapps.net/api/*" | ||
], | ||
"icons": { | ||
"48": "images/icon-48.png", | ||
"128": "images/icon-128.png" | ||
} | ||
} |
Oops, something went wrong.