Skip to content

Commit

Permalink
Merge pull request #97 from hcp-uw/date-format
Browse files Browse the repository at this point in the history
Added date reformatting
  • Loading branch information
sdpandit authored Sep 24, 2024
2 parents d7c1c21 + 2a03879 commit d2ec4b4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 31 deletions.
58 changes: 29 additions & 29 deletions source/backend/api_parsing_examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -14,7 +14,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand All @@ -25,12 +25,12 @@
"{\n",
" \"message\": \"Togekiss logged in successfully.\"\n",
"}\n",
"<RequestsCookieJar[<Cookie session=.eJwlzjEOwjAMQNG7ZGaIHdeOew52lMQ2VAxILZ0QdycS89OX_ifdYvfjkdb3fvol3TZLayLskCkyj8agS1ciZ5JaW3SRilBcaywcylhY1B0CbSh3YpxdVSM07FlYDEaZ2rgheORCZarlHCWYu4K1jgKOjiS2aCvNRpoj5-H7_-b6uvtzO470_QGoMzGk.ZtJHig.lcF5gX8kv7VCH9pKAdFuLB_gSIU for receiptplus.pythonanywhere.com/>]>\n"
"<RequestsCookieJar[<Cookie session=.eJwlzjsOwjAMANC7ZGaIE3_inoO9ShwbKgaklk6Iu4PEO8F7pzV2P-5pee2nX9K6zbSk6Vq6ozGGWBOqNYLYmTtWb6J1RHfrw40VfFIW02mTug6LHEXZHKiKYi44EEitZVBtJCrAozEy1QaTG2jh8Nxr7jAwZ0JRTb_Iefj-31yfN39sx5E-X0bRMZg.ZvIKkg.aAXznj52OCD3YdsVflQ5sjdGhiA for 127.0.0.1/>]>\n"
]
}
],
"source": [
"url = 'https://receiptplus.pythonanywhere.com/api/login'\n",
"url = 'http://127.0.0.1:5000/api/login'\n",
"returning_user = {'user_id':'Togekiss',\n",
" 'password':'Togekiss'}\n",
"response = requests.post(url, json=returning_user)\n",
Expand All @@ -42,7 +42,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 12,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -194,16 +194,16 @@
" \"quantity\": 1.28\n",
" }\n",
" ],\n",
" \"receipt_date\": \"10/21/23\",\n",
" \"receipt_date\": \"2023-10-21\",\n",
" \"store\": \"jobs.qfc.com\",\n",
" \"total\": 119.31\n",
"}\n"
]
}
],
"source": [
"# url = 'http://127.0.0.1:5000/api/receipts_parsing'\n",
"url = 'https://receiptplus.pythonanywhere.com/api/receipts_parsing'\n",
"url = 'http://127.0.0.1:5000/api/receipts_parsing'\n",
"# url = 'https://receiptplus.pythonanywhere.com/api/receipts_parsing'\n",
"image_path = os.path.join(os.getcwd(), \"sample_receipts/QFC_1.jpg\")\n",
"\n",
"# Open the image file in binary mode\n",
Expand All @@ -223,7 +223,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 13,
"metadata": {},
"outputs": [
{
Expand All @@ -240,16 +240,16 @@
" \"quantity\": 3.01\n",
" }\n",
" ],\n",
" \"receipt_date\": \"02/24/24\",\n",
" \"receipt_date\": \"2024-02-24\",\n",
" \"store\": \"QFC\",\n",
" \"total\": 2.08\n",
"}\n"
]
}
],
"source": [
"# url = 'http://127.0.0.1:5000/api/receipts_parsing'\n",
"url = 'https://receiptplus.pythonanywhere.com/api/receipts_parsing'\n",
"url = 'http://127.0.0.1:5000/api/receipts_parsing'\n",
"# url = 'https://receiptplus.pythonanywhere.com/api/receipts_parsing'\n",
"image_path = os.path.join(os.getcwd(), \"sample_receipts/QFC_2.jpg\")\n",
"\n",
"# Open the image file in binary mode\n",
Expand All @@ -269,7 +269,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 14,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -326,16 +326,16 @@
" \"quantity\": 2.0\n",
" }\n",
" ],\n",
" \"receipt_date\": \"04-30-24\",\n",
" \"receipt_date\": \"2024-04-30\",\n",
" \"store\": \"TRADER JOE'S\",\n",
" \"total\": 32.3\n",
"}\n"
]
}
],
"source": [
"# url = 'http://127.0.0.1:5000/api/receipts_parsing'\n",
"url = 'https://receiptplus.pythonanywhere.com/api/receipts_parsing'\n",
"url = 'http://127.0.0.1:5000/api/receipts_parsing'\n",
"# url = 'https://receiptplus.pythonanywhere.com/api/receipts_parsing'\n",
"image_path = os.path.join(os.getcwd(), \"sample_receipts/TJ_1.jpg\")\n",
"\n",
"# Open the image file in binary mode\n",
Expand All @@ -355,7 +355,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 15,
"metadata": {},
"outputs": [
{
Expand All @@ -377,16 +377,16 @@
" \"quantity\": 4.0\n",
" }\n",
" ],\n",
" \"receipt_date\": \"03-29-24\",\n",
" \"receipt_date\": \"2024-03-29\",\n",
" \"store\": \"TRADER JOE'S\",\n",
" \"total\": 15.13\n",
"}\n"
]
}
],
"source": [
"# url = 'http://127.0.0.1:5000/api/receipts_parsing'\n",
"url = 'https://receiptplus.pythonanywhere.com/api/receipts_parsing'\n",
"url = 'http://127.0.0.1:5000/api/receipts_parsing'\n",
"# url = 'https://receiptplus.pythonanywhere.com/api/receipts_parsing'\n",
"image_path = os.path.join(os.getcwd(), \"sample_receipts/TJ_2.jpg\")\n",
"\n",
"# Open the image file in binary mode\n",
Expand All @@ -406,7 +406,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 16,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -443,16 +443,16 @@
" \"quantity\": 1.0\n",
" }\n",
" ],\n",
" \"receipt_date\": \"03-29-24\",\n",
" \"receipt_date\": \"2024-03-29\",\n",
" \"store\": \"TRADER JOE'S\",\n",
" \"total\": 17.04\n",
"}\n"
]
}
],
"source": [
"# url = 'http://127.0.0.1:5000/api/receipts_parsing'\n",
"url = 'https://receiptplus.pythonanywhere.com/api/receipts_parsing'\n",
"url = 'http://127.0.0.1:5000/api/receipts_parsing'\n",
"# url = 'https://receiptplus.pythonanywhere.com/api/receipts_parsing'\n",
"image_path = os.path.join(os.getcwd(), \"sample_receipts/TJ_3.jpg\")\n",
"\n",
"# Open the image file in binary mode\n",
Expand Down Expand Up @@ -620,7 +620,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 17,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -672,16 +672,16 @@
" \"quantity\": 1.0\n",
" }\n",
" ],\n",
" \"receipt_date\": \"Feb 12, 2024\",\n",
" \"receipt_date\": \"2024-02-12\",\n",
" \"store\": \"HMART\",\n",
" \"total\": 31.73\n",
"}\n"
]
}
],
"source": [
"# url = 'http://127.0.0.1:5000/api/receipts_parsing'\n",
"url = 'https://receiptplus.pythonanywhere.com/api/receipts_parsing'\n",
"url = 'http://127.0.0.1:5000/api/receipts_parsing'\n",
"# url = 'https://receiptplus.pythonanywhere.com/api/receipts_parsing'\n",
"image_path = os.path.join(os.getcwd(), \"sample_receipts/HMART_3.jpg\")\n",
"\n",
"# Open the image file in binary mode\n",
Expand Down Expand Up @@ -723,7 +723,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
"version": "3.12.1"
}
},
"nbformat": 4,
Expand Down
46 changes: 44 additions & 2 deletions source/backend/resource/resource_receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,51 @@ def receipts_parsing():
finally:
os.remove(file_path)
result = response.json()


inputDate = result['amazon']['extracted_data'][0]["financial_document_information"]['invoice_date']
outputDate = ""

months = {"jan": "01", "feb": "02", "mar": "03", "apr": "04",
"may": "05", "jun": "06", "jul": "07", "aug": "08",
"sep": "09", "oct": "10", "nov": "11", "dec": "12"}
splitList = []

# 09/12/2024
if "/" in inputDate:
splitList = inputDate.split("/")

# 09-12-2024
elif "-" in inputDate:
splitList = inputDate.split("-")

# Sep 12, 2024
elif inputDate[0:3].lower() in months:
splitList = inputDate.split(" ")

try:
month = months[splitList[0][0:3].lower()]
except:
month = splitList[0]
day = splitList[1]
year = splitList[2]

if len(year) == 2:
year = "20" + year
if len(month) == 1:
month = "0" + month
if "," in day:
day = day[:-1]
if len(day) == 1:
day = "0" + day

assert int(month) >= 1 and int(month) <= 12
assert int(day) >= 1 and int(day) <= 31
assert int(year) >= 2000 and int(year) < 2100

outputDate = year + "-" + month + "-" + day

output = {
'receipt_date': result['amazon']['extracted_data'][0]["financial_document_information"]['invoice_date'],
'receipt_date': outputDate,
'total': result['amazon']['extracted_data'][0]['payment_information']['amount_due'],
'store': result['amazon']['extracted_data'][0]['merchant_information']['name'],
'location': result['amazon']['extracted_data'][0]['merchant_information']['address'],
Expand Down

0 comments on commit d2ec4b4

Please sign in to comment.