Skip to content

Commit

Permalink
new options for update and export scripts, bug #2311
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanegigandet committed Sep 11, 2019
1 parent a347c8e commit 5da33b0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 18 deletions.
64 changes: 54 additions & 10 deletions scripts/export_csv_file.pl
Original file line number Diff line number Diff line change
Expand Up @@ -96,40 +96,84 @@

$missing_arg and exit();

# Construct the MongoDB query

use boolean;

foreach my $field (sort keys %$query_ref) {
if ($query_ref->{$field} eq 'null') {
# $query_ref->{$field} = { '$exists' => false };
$query_ref->{$field} = undef;
}
if ($query_ref->{$field} eq 'exists') {
$query_ref->{$field} = { '$exists' => true };
}
}

use Data::Dumper;
print STDERR "MongoDB query:\n" . Dumper($query_ref);

my $count = get_products_collection()->count_documents($query_ref);

print STDERR "$count documents to export.\n";
sleep(2);

my $cursor = get_products_collection()->find($query_ref);
$cursor->immortal(1);


# CSV export

my $csv = Text::CSV->new ( { binary => 1 , sep_char => $separator } ) # should set binary attribute.
or die "Cannot use CSV: ".Text::CSV->error_diag ();

my $fh = *STDOUT;

my $fh = *STDOUT;

my @fields = split(/,/, $fields);

# Print the header line with fields names
$csv->print ($fh, \@fields);
print "\n";

my $cursor = get_products_collection()->query($query_ref);
$cursor->immortal(1);

my $i = 0;

while (my $product_ref = $cursor->next) {

$i++;


my $added_images_urls = 0;

my @values = ();
foreach my $field (@fields) {
my $value;
if (($field =~ /_tags$/) and (defined $product_ref->{$field})) {

if (($field =~ /^image_/) and (not $added_images_urls)) {
ProductOpener::Display::add_images_urls_to_product($product_ref);
$added_images_urls = 1;
}

if ($field =~ /^image_(ingredients|nutrition)_json$/) {
if (defined $product_ref->{"image_$1_url"}) {
$value = $product_ref->{"image_$1_url"};
$value =~ s/\.(\d+)\.jpg/.json/;
}
}
elsif ($field =~ /^image_(.*)_full_url$/) {
if (defined $product_ref->{"image_$1_url"}) {
$value = $product_ref->{"image_$1_url"};
$value =~ s/(\d+)\.jpg/.full.jpg/;
}
}
elsif (($field =~ /_tags$/) and (defined $product_ref->{$field})) {
$value = join(",", @{$product_ref->{$field}});
}
else {
$value = $product_ref->{$field};
}
push @values, $value;
}

$csv->print ($fh, \@values);
print "\n";

Expand Down
22 changes: 14 additions & 8 deletions scripts/update_all_products.pl
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@
# $query_ref->{$field} = { '$exists' => false };
$query_ref->{$field} = undef;
}
if ($query_ref->{$field} eq 'exists') {
$query_ref->{$field} = { '$exists' => true };
}
}

if (defined $key) {
Expand All @@ -191,16 +194,21 @@
$key = "key_" . time();
}

#$query_ref->{code} = "3661112080648";
#$query_ref->{categories_tags} = "en:plant-milks";
#$query_ref->{quality_tags} = "ingredients-fr-includes-fr-nutrition-facts";


# $query_ref->{unknown_nutrients_tags} = { '$exists' => true, '$ne' => [] };

print "Update key: $key\n\n";
print STDERR "Update key: $key\n\n";

use Data::Dumper;
print STDERR "MongoDB query:\n" . Dumper($query_ref);

my $products_collection = get_products_collection();

my $count = $products_collection->count_documents($query_ref);

print STDERR "$count documents to update.\n";
sleep(2);


my $cursor = $products_collection->query($query_ref)->fields({ code => 1 });
$cursor->immortal(1);

Expand All @@ -212,8 +220,6 @@
my $code = $product_ref->{code};
my $path = product_path($code);

#next if $code ne "8480013072645";

if (not defined $code) {
print STDERR "code field undefined for product id: " . $product_ref->{id} . " _id: " . $product_ref->{_id} . "\n";
}
Expand Down

0 comments on commit 5da33b0

Please sign in to comment.