Skip to content

Commit

Permalink
Implement JSON output
Browse files Browse the repository at this point in the history
  • Loading branch information
AMDmi3 committed Nov 14, 2018
1 parent 8205fa1 commit 4dbfddb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Portscout/SQL.pm
Expand Up @@ -231,6 +231,12 @@ $sql{portdata_uncheck} =

#$sql{portdata_genresults}

$sql{portdata_selectall} =
q(SELECT *
FROM portdata
WHERE moved != true
ORDER BY cat,name);

$sql{portdata_selectmaintainer} =
q(SELECT *
FROM portdata
Expand Down
46 changes: 45 additions & 1 deletion portscout.pl
Expand Up @@ -45,6 +45,7 @@
use MIME::Lite;
use Net::FTP;
use URI;
use JSON;

use DBI;

Expand Down Expand Up @@ -1245,7 +1246,7 @@ sub GenerateHTML
$dbh = connect_db();

prepare_sql($dbh, \%sths,
qw(portdata_genresults portdata_selectmaintainer portdata_selectall_limited)
qw(portdata_genresults portdata_selectall portdata_selectmaintainer portdata_selectall_limited)
);

if ($Portscout::SQL::sql{portdata_genresults_init}) {
Expand Down Expand Up @@ -1377,6 +1378,49 @@ sub GenerateHTML

$template->output('restricted-ports.html');

print "Creating JSON dump of all data...\n";

open my $jf, '>', $settings{html_data_dir} . '/dump.json'
or die 'Cannot open JSON output';

print $jf '[';

my $firstitem = 1;

$sths{portdata_selectall}->execute();
while (my $row = $sths{portdata_selectall}->fetchrow_hashref) {
if ($row->{ignore}) {
$row->{method} = 'X';
$row->{newver} = '';
$row->{newurl} = '';
} else {
if ($row->{method} == METHOD_LIST) {
$row->{method} = 'L';
} elsif ($row->{method} == METHOD_GUESS) {
$row->{method} = 'G';
} else {
$row->{method} = '';
}
}

if ($row->{newver} && ($row->{ver} ne $row->{newver})) {
$row->{newdistfile} = 'updated';
} else {
next if ($settings{hide_unchanged});
$row->{newdistfile} = '';
}
$row->{updated} =~ s/:\d\d(?:\.\d+)?$/ $settings{local_timezone}/;
$row->{checked} =~ s/:\d\d(?:\.\d+)?$/ $settings{local_timezone}/;

$row = { map { $_ => $row->{$_} } qw(name cat maintainer ver method newver newurl checked updated discovered) };

print $jf ',' unless $firstitem;
print $jf encode_json($row);
$firstitem = 0;
}
print $jf ']';
close $jf;

finish_sql($dbh, \%sths);
$dbh->disconnect;
}
Expand Down
1 change: 1 addition & 0 deletions portscout.pod
Expand Up @@ -45,6 +45,7 @@ Plus we need a few Perl modules:
- MIME::Lite
- XML::XPath
- XML::XPath::XMLParser
- JSON

SQLite support is currently limited to non-forking mode only. That is, if you
decide to use SQLite, Portscout will only check one port at a time; this will
Expand Down

0 comments on commit 4dbfddb

Please sign in to comment.