|
| 1 | +#!/usr/bin/perl |
| 2 | +# |
| 3 | +# nokia-suite-convert.pl - convert SMS export from CSV exported |
| 4 | +# by Nokia Suite into CSV that can be parsed by csv-convert.py |
| 5 | +# |
| 6 | +# Copyright (c) 2023 - 2024 by Matus UHLAR <uhlar@fantomas.sk> |
| 7 | +# |
| 8 | +# This program is free software: you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU General Public License as published by |
| 10 | +# the Free Software Foundation, either version 3 of the License, or |
| 11 | +# (at your option) any later version. |
| 12 | +# |
| 13 | +# This program is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License |
| 19 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + |
| 21 | +use strict; |
| 22 | +use Text::CSV; |
| 23 | +use Time::Local qw( timelocal_posix ); |
| 24 | +use POSIX::strptime; |
| 25 | +use bignum; |
| 26 | +use open qw( :std :encoding(UTF-8) ); |
| 27 | + |
| 28 | +my ($fnin, $fhin, $fnout,$fhout); |
| 29 | +if ($fnin=shift @ARGV) { |
| 30 | + open $fhin, "<", $fnin or die "$fnin $!"; |
| 31 | +} else { |
| 32 | + $fhin = *STDIN; |
| 33 | +} |
| 34 | +if ($fnout=shift @ARGV) { |
| 35 | + open $fhout, ">", $fnout or |
| 36 | + die "$fnout $!"; |
| 37 | +} else { |
| 38 | + $fhout = *STDOUT; |
| 39 | +} |
| 40 | + |
| 41 | +my $csvin = Text::CSV->new ({ binary => 1, auto_diag => 1 }); |
| 42 | +#my $csvout = Text::CSV->new ({ binary => 1, quote_space => 0, quote_binary => 0 }); |
| 43 | +# always quote if you want to filter in LibreOffice and compare results |
| 44 | +my $csvout = Text::CSV->new ({ binary => 1, always_quote => 1 }); |
| 45 | +$csvout->say($fhout, ["type","read","address","date","body"]); |
| 46 | + |
| 47 | +my ($type,$read,$addr,$date,$text); |
| 48 | +my ($year,$mon,$day,$hour,$min); |
| 49 | + |
| 50 | +while (my $row = $csvin->getline ($fhin)) { |
| 51 | + my %type = map { $_ => 1 } (split /,/, $row->[1]); |
| 52 | + if ($type{'RECEIVED'}) { |
| 53 | + $type=1; |
| 54 | + $addr=$row->[2]; |
| 55 | + } elsif ($type{'SENT'}) { |
| 56 | + $type=2; |
| 57 | + $addr=$row->[3]; |
| 58 | + } else { |
| 59 | + print STDERR "unknown type ",$row->[1],"\n"; |
| 60 | + next; |
| 61 | + } |
| 62 | + if ($type{'READ'}) { |
| 63 | + $read=1; |
| 64 | + } else { |
| 65 | + $read=0; |
| 66 | + } |
| 67 | + ($min,$hour,$day,$mon,$year) = |
| 68 | + (POSIX::strptime($row->[5], "%Y.%m.%d %H:%M"))[1,2,3,4,5]; |
| 69 | + $date=timelocal_posix(0,$min,$hour,$day,$mon,$year); |
| 70 | + $date*=1000; |
| 71 | + $text=$row->[7]; |
| 72 | + $csvout->say($fhout,[$type,$read,$addr,$date,$text]); |
| 73 | +} |
| 74 | +close $fhin; |
| 75 | + |
| 76 | +close $fhout; |
0 commit comments