-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert ticket v3 HTML to JSON tickets #22
Open
Kuba314
wants to merge
1
commit into
Andre0512:main
Choose a base branch
from
Kuba314:ticket-v3-html
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from typing import Any | ||
import re | ||
|
||
import lxml.html as html | ||
|
||
|
||
VAT_TYPE_LINE_ENDING_PATTERN = re.compile(r" [A-Z]$") | ||
|
||
|
||
def parse_html_receipt(date: str, html_receipt: str) -> dict[str, Any]: | ||
dom = html.document_fromstring(html_receipt) | ||
|
||
receipt = { | ||
"date": date, | ||
"itemsLine": [], | ||
} | ||
for node in dom.xpath(r".//span[starts-with(@id, 'purchase_list_line_')]"): | ||
if "class" not in node.attrib: | ||
if not VAT_TYPE_LINE_ENDING_PATTERN.search(node.text): | ||
continue | ||
|
||
*name_parts, price = node.text[:-2].split() | ||
receipt["itemsLine"].append( | ||
{ | ||
"name": " ".join(name_parts), | ||
"originalAmount": price, | ||
"isWeight": True, | ||
"discounts": [], | ||
} | ||
) | ||
elif node.attrib["class"] == "currency": | ||
receipt["currency"] = {"code": node.text.strip(), "symbol": node.attrib["data-currency"]} | ||
elif node.attrib["class"] == "article": | ||
if node.text.startswith(" "): | ||
continue | ||
|
||
quantity_text = node.get("data-art-quantity") | ||
if quantity_text is None: | ||
is_weight = False | ||
quantity = 1 | ||
elif "," in quantity_text: | ||
is_weight = True | ||
quantity = quantity_text | ||
else: | ||
is_weight = False | ||
quantity = quantity_text | ||
|
||
receipt["itemsLine"].append( | ||
{ | ||
"name": node.attrib["data-art-description"], | ||
"currentUnitPrice": node.attrib["data-unit-price"], | ||
"isWeight": is_weight, | ||
"quantity": quantity, | ||
"discounts": [], | ||
} | ||
) | ||
elif node.attrib["class"] == "discount": | ||
discount = abs(parse_float(node.text.split()[-1])) | ||
receipt["itemsLine"][-1]["discounts"].append({"amount": str(discount).replace(".", ",")}) | ||
|
||
return receipt | ||
|
||
|
||
def parse_float(text: str) -> float: | ||
return float(text.replace(",", ".")) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This throws an IndexError when I'm running it because some of the span elements contain just white text so the
node.text.split()
returns an empty list.Here's the HTML on my receipt:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Man, they just can't be consistent... Thank you for providing another data point with which we can figure out all the formats they use for this! I'll try to implement the format you provided once I have time to actually do this though... You can always suggest changes and I'll be happy to use them of course.
For now I'm thinking a regex searching for something like
-\d+[\.,]\d{2}$
would be best.Btw shouldn't the code currently fail in parsing the first line's
reward
word as float instead of the whitespace-split-index-error that you're describing?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No rush to implement this. I would have done it myself but I was hesitant to suggest changes cause I'm still trying to understand what's happening. I will for sure once I get more familiar with the project. :)
I think because the class of the first line is
discount ccs_bold
instead of justdiscount
it's not parsed at all.So, does the HTML differ from country to country? Or does it depend on the coupon you use and whether it's a percentage/flat discount? That's the only receipt with a coupon I have so that's my only data point :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries :) This is not even that tied to this specific project, but more to the actual lidl API since AFAIK there's no public documentation for it and people just somehow reverse engineered it.
Right, of course, missed that.
There's definitely some difference for some reason. See diogotcorreia/lidl-to-grocy's lidl/test/receipt.html. It uses I think the same format as what I saw and implemented in this PR. It's weird that your receipt is different, but we'll probably have to implement a common parsing for all possible formats. Currently I'm blocked on #23 though so I can't verify if anything changed recently in my receipts, but in my lidl-plus android app I don't see any discounts as bold as you probably would.
We could probably do something like this to support both formats: