Skip to content

Commit

Permalink
Include also support for K-ruoka (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
areee authored Dec 4, 2021
1 parent 6ca5dbb commit 4a74cf8
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 5 deletions.
15 changes: 13 additions & 2 deletions bin/dart_kassakuitti_cli.dart
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!');
}
96 changes: 96 additions & 0 deletions bin/load_html_k_ruoka.dart
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.
16 changes: 13 additions & 3 deletions bin/read_html_and_save_as_csv.dart
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);
}
5 changes: 5 additions & 0 deletions bin/shop_selector_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// Shop selector (sKaupat, kRuoka)
enum ShopSelector {
sKaupat,
kRuoka,
}

0 comments on commit 4a74cf8

Please sign in to comment.