-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include also support for K-ruoka (#5)
- Loading branch information
Showing
5 changed files
with
127 additions
and
5 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,9 +1,20 @@ | ||
import 'read_html_and_save_as_csv.dart'; | ||
import 'read_receipt_and_save_as_csv.dart'; | ||
import 'shop_selector_helper.dart'; | ||
|
||
void main(List<String> arguments) { | ||
readReceiptAndSaveAsCSV('assets/files/_cashReceipt.txt'); | ||
readHtmlAndSaveAsCSV('assets/files/_orderedProducts.html'); | ||
const shopSelector = | ||
ShopSelector.kRuoka; // TODO: make this a command line argument | ||
|
||
if (shopSelector == ShopSelector.sKaupat) { | ||
readReceiptAndSaveAsCSV('assets/files/_cashReceipt.txt'); | ||
readHtmlAndSaveAsCSV( | ||
'assets/files/_orderedProducts_S-kaupat.html', shopSelector); | ||
} else { | ||
// K-ruoka | ||
readHtmlAndSaveAsCSV( | ||
'assets/files/_orderedProducts_K-ruoka.html', shopSelector); | ||
} | ||
|
||
print('Done!'); | ||
} |
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,96 @@ | ||
import 'dart:io'; | ||
import 'package:html/parser.dart'; | ||
|
||
import 'ean_product.dart'; | ||
|
||
/// Loads the HTML file from assets and parses it. Then, it returns a list of EANProduct objects. | ||
Future<List<EANProduct>> loadHtmlFromAssets(String filePath) async { | ||
List<EANProduct> eanProducts = []; | ||
|
||
var file = File(filePath); | ||
var html = await file.readAsString(); | ||
var document = parse(html); | ||
|
||
var substitutedProducts = | ||
document.getElementsByClassName('old-order-substituted-products-list')[0]; | ||
|
||
for (var product in substitutedProducts.children) { | ||
var productInfo = product.children[1].children[0]; | ||
|
||
var productEan = productInfo.id.replaceAll('department-product-item-', ''); | ||
|
||
var productName = productInfo | ||
.children[0].children[2].children[0].children[0].text | ||
.trim() | ||
.replaceAll('\n', '') | ||
.replaceAll(RegExp(r'\s{40,50}'), ' '); | ||
|
||
var productQuantity = productInfo.children[0].children[3].children[0].text | ||
.replaceAll('kpl', '') | ||
.replaceAll('kg', ''); | ||
|
||
var priceElement = | ||
productInfo.children[0].children[3].children[1].children[0]; | ||
|
||
var finalPrice = StringBuffer(); | ||
|
||
for (var i = 0; i < priceElement.children.length; i++) { | ||
if (i != 3) { | ||
finalPrice.write(priceElement.children[i].text); | ||
} | ||
} | ||
var productPrice = finalPrice.toString(); | ||
|
||
eanProducts.add(EANProduct( | ||
ean: productEan, | ||
name: productName, | ||
quantity: int.parse(productQuantity), | ||
price: productPrice)); | ||
} | ||
|
||
var pickedProducts = | ||
document.getElementsByClassName('old-order-departments')[0]; | ||
|
||
for (var department in pickedProducts.children) { | ||
var itemListing = department.children[1]; | ||
|
||
for (var productRow in itemListing.children) { | ||
var productItem = productRow.children[0]; | ||
|
||
var productEan = | ||
productItem.id.replaceAll('department-product-item-', ''); | ||
|
||
var productName = productItem | ||
.children[0].children[1].children[0].children[0].text | ||
.trim() | ||
.replaceAll('\n', '') | ||
.replaceAll(RegExp(r'\s{40,50}'), ' '); | ||
|
||
var productQuantity = productItem.children[0].children[2].children[0].text | ||
.replaceAll('kpl', '') | ||
.replaceAll('kg', '') | ||
.replaceAll(',', '.'); | ||
|
||
var priceElement = | ||
productItem.children[0].children[2].children[1].children[0]; | ||
|
||
var finalPrice = StringBuffer(); | ||
|
||
for (var i = 0; i < priceElement.children.length; i++) { | ||
if (i != 3) { | ||
finalPrice.write(priceElement.children[i].text); | ||
} | ||
} | ||
var productPrice = finalPrice.toString(); | ||
|
||
eanProducts.add(EANProduct( | ||
ean: productEan, | ||
name: productName, | ||
quantity: double.parse(productQuantity).round(), | ||
price: productPrice)); | ||
} | ||
} | ||
|
||
// TODO: Add an own section for home delivery price | ||
return eanProducts; | ||
} |
File renamed without changes.
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,8 +1,18 @@ | ||
import 'load_html.dart'; | ||
import 'ean_product.dart'; | ||
import 'load_html_s_kaupat.dart' as s_kaupat; | ||
import 'load_html_k_ruoka.dart' as k_ruoka; | ||
import 'save_eps_as_csv.dart'; | ||
import 'shop_selector_helper.dart'; | ||
|
||
void readHtmlAndSaveAsCSV(String filePath) async { | ||
var awaitedEANProductList = await loadHtmlFromAssets(filePath); | ||
void readHtmlAndSaveAsCSV(String filePath, ShopSelector shopSelector) async { | ||
List<EANProduct> awaitedEANProductList = []; | ||
|
||
if (shopSelector == ShopSelector.sKaupat) { | ||
awaitedEANProductList = await s_kaupat.loadHtmlFromAssets(filePath); | ||
} else { | ||
// K-ruoka | ||
awaitedEANProductList = await k_ruoka.loadHtmlFromAssets(filePath); | ||
} | ||
|
||
eanProductListToCSV(awaitedEANProductList); | ||
} |
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,5 @@ | ||
/// Shop selector (sKaupat, kRuoka) | ||
enum ShopSelector { | ||
sKaupat, | ||
kRuoka, | ||
} |