Perl Paste by Apple Receipt Parser
Description: Parses Apple's iOS and Mac App Store purchase receipts
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #!/usr/bin/perl -w use warnings; use strict; use Data::Dumper; my $catalogFn = "receiptCatalog.txt"; my $firstCatalogFlag = 0; if (! -e $catalogFn) { print STDERR "Making $catalogFn...\n"; $firstCatalogFlag = 1; } else { print STDERR "Appending to existing catalog file...\n"; } open (CATALOG, ">> $catalogFn") or die "ERROR: Could not open handle to catalog...\n"; if ($firstCatalogFlag) { print CATALOG "itemName\tartist\tunitPrice\ttype\n"; } my $infoFlag = 0; my $itemFlag = 0; my $artistFlag = 0; my $typeFlag = 0; my $unitPriceFlag = 0; my $elementRef; my $itemName; while (<>) { chomp $_; if ($_ eq "Unit Price") { print STDERR "Found unit price label...\n"; $infoFlag = 1; $itemFlag = 1; next; } if ($infoFlag) { if ($_ eq "Subtotal:") { print Dumper $elementRef; foreach my $itemNameKey (keys %{$elementRef}) { my $artist = $elementRef->{$itemNameKey}->{artist}; my $unitPrice = $elementRef->{$itemNameKey}->{unitPrice}; my $type = $elementRef->{$itemNameKey}->{type}; print CATALOG "$itemNameKey\t$artist\t$unitPrice\t$type\n"; } print STDERR "Ceasing with file scraping...\n"; last; } elsif (length $_ > 1) { if ($itemFlag) { $itemName = $_; $itemFlag = 0; $artistFlag = 1; $_ = <>; # this skips over the "Report a Problem" line next; } elsif ($artistFlag) { $elementRef->{$itemName}->{artist} = $_; $artistFlag = 0; $typeFlag = 1; next; } elsif ($typeFlag) { $elementRef->{$itemName}->{type} = $_; $typeFlag = 0; $unitPriceFlag = 1; next; } elsif ($unitPriceFlag) { $elementRef->{$itemName}->{unitPrice} = $_; $unitPriceFlag = 0; next; } } } } close CATALOG; |