Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

THANK YOU. (no bug) - Here is a (much shorter) perl version #3

Open
Tux opened this issue Jul 18, 2019 · 2 comments
Open

THANK YOU. (no bug) - Here is a (much shorter) perl version #3

Tux opened this issue Jul 18, 2019 · 2 comments

Comments

@Tux
Copy link

Tux commented Jul 18, 2019

This perl script is much shorter and
• generates correct CSV
• Allows short names: adb-export.pl -e contacts
• Gathers exports in a single exports folder

#!/usr/bin/perl

use 5.14.2;
use warnings;

# Based on adb-export.sh from github.com:sromku/adb-export
# author: Roman Kushnarenko @sromku
# license: Apache License 2.0

our $VERSION = "0.01 - 20190718";
our $CMD = $0 =~ s{.*/}{}r;

sub usage {
    my $err = shift and select STDERR;
    say "usage: $CMD [--debug=[#]] --export[=URL]";
    say " URL list available on github: https://github.com/snatik/adb-export";
    exit $err;
    } # usage

use Text::CSV_XS qw( csv );
use Getopt::Long qw(:config bundling);
GetOptions (
    "help|?"            => sub { usage (0); },
    "V|version"         => sub { say "$CMD [$VERSION]"; exit 0; },

    "e|export:s"        => \ my @exp,

    "d|debug:1"         => \(my $opt_d = 0),
    "v|verbose:1"       => \(my $opt_v = 1),
    ) or usage (1);

my @d = localtime;
my $stamp = sprintf "%4d%02d%02d-%02d%02d", $d[5] + 1900, $d[4] + 1, @d[3,2,1];

my %all = (
    aggregation_exceptions      => "com.android.contacts/aggregation_exceptions",
    attendees                   => "com.android.calendar/attendees",
    audio_albums                => "media/external/audio/albums",
    audio_artists               => "media/external/audio/artists",
    audio_genres                => "media/external/audio/genres",
    audio_media                 => "media/external/audio/media",
    audio_playlists             => "media/external/audio/playlists",
    bookmarks                   => "settings/bookmarks",
    calendars                   => "com.android.calendar/calendars",
    contacts                    => "com.android.contacts/contacts",
    data                        => "com.android.contacts/data",
    data_phones                 => "com.android.contacts/data/phones",
    deleted_contacts            => "com.android.contacts/deleted_contacts",
    directories                 => "com.android.contacts/directories",
    events                      => "com.android.calendar/events",
    extendedproperties          => "com.android.calendar/extendedproperties",
    global                      => "settings/global",
    groups                      => "com.android.contacts/groups",
    images_media                => "media/external/images/media",
    images_thumbnails           => "media/external/images/thumbnails",
    photo_dimensions            => "com.android.contacts/photo_dimensions",
    raw_contact_entities        => "com.android.contacts/raw_contact_entities",
    raw_contacts                => "com.android.contacts/raw_contacts",
    reminders                   => "com.android.calendar/reminders",
    secure                      => "settings/secure",
    status_updates              => "com.android.contacts/status_updates",
    system                      => "settings/system",
    video_media                 => "media/external/video/media",
    video_thumbnails            => "media/external/video/thumbnails",
    words                       => "user_dictionary/words",
    );
my %lla = reverse %all;

-d "export" or mkdir "export", 0775;
@exp or @exp = sort values %all;
foreach my $url (@exp) {
    exists $all{$url} and $url = $all{$url};

    $url =~ s{^\w+://}{};

    my $base = $lla{$url} or die "Unsupported URL: '$url'\n";
    $base = "export/$base-$stamp";

    my $rawf = "$base.raw";
    $opt_v and warn sprintf "Exporting %-45s to %s\n", $url, $rawf;

    my $data = `adb shell content query --uri content://$url`;
    open my $fh, ">", $rawf;
    print   $fh $data;
    close   $fh;

    my $csvf = "$base.csv";
    $opt_v and warn sprintf "Exporting %-30s to %s\n", $url, $csvf;
    $data =~ m/^Row:\s*\d/ or next;
    my @h;
    my @r;
    for (split m/(?:^|\n)(?=Row:\s*\d+\s+\w+=)/ => $data) {
        s/^Row:\s*\d+\s+/, /;
        my @v = m/,\s+(\w+)=(.*?)(?=$|, \w+=)/g;
        unless (@h) {
            @h = map { $v[$_] } grep { $_ % 2 == 0 } 0 .. $#v;
            }
        my %v = @v;
        s/^NULL$// for values %v;
        push @r, \%v;
        }
    csv (in => \@r, out => $csvf, headers => \@h);
    $opt_v > 1 and warn sprintf "%17d records\n", scalar @r;
    }
@z3r0r4
Copy link

z3r0r4 commented Oct 6, 2021

what kinda license would you put this under @Tux ?

@Tux
Copy link
Author

Tux commented Oct 7, 2021

You can redistribute and/or modify it under the same terms as Perl itself:
http://dev.perl.org/licenses/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants