Skip to content

Commit 4dbfddb

Browse files
committed
Implement JSON output
1 parent 8205fa1 commit 4dbfddb

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

Portscout/SQL.pm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@ $sql{portdata_uncheck} =
231231

232232
#$sql{portdata_genresults}
233233

234+
$sql{portdata_selectall} =
235+
q(SELECT *
236+
FROM portdata
237+
WHERE moved != true
238+
ORDER BY cat,name);
239+
234240
$sql{portdata_selectmaintainer} =
235241
q(SELECT *
236242
FROM portdata

portscout.pl

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use MIME::Lite;
4646
use Net::FTP;
4747
use URI;
48+
use JSON;
4849

4950
use DBI;
5051

@@ -1245,7 +1246,7 @@ sub GenerateHTML
12451246
$dbh = connect_db();
12461247

12471248
prepare_sql($dbh, \%sths,
1248-
qw(portdata_genresults portdata_selectmaintainer portdata_selectall_limited)
1249+
qw(portdata_genresults portdata_selectall portdata_selectmaintainer portdata_selectall_limited)
12491250
);
12501251

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

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

1381+
print "Creating JSON dump of all data...\n";
1382+
1383+
open my $jf, '>', $settings{html_data_dir} . '/dump.json'
1384+
or die 'Cannot open JSON output';
1385+
1386+
print $jf '[';
1387+
1388+
my $firstitem = 1;
1389+
1390+
$sths{portdata_selectall}->execute();
1391+
while (my $row = $sths{portdata_selectall}->fetchrow_hashref) {
1392+
if ($row->{ignore}) {
1393+
$row->{method} = 'X';
1394+
$row->{newver} = '';
1395+
$row->{newurl} = '';
1396+
} else {
1397+
if ($row->{method} == METHOD_LIST) {
1398+
$row->{method} = 'L';
1399+
} elsif ($row->{method} == METHOD_GUESS) {
1400+
$row->{method} = 'G';
1401+
} else {
1402+
$row->{method} = '';
1403+
}
1404+
}
1405+
1406+
if ($row->{newver} && ($row->{ver} ne $row->{newver})) {
1407+
$row->{newdistfile} = 'updated';
1408+
} else {
1409+
next if ($settings{hide_unchanged});
1410+
$row->{newdistfile} = '';
1411+
}
1412+
$row->{updated} =~ s/:\d\d(?:\.\d+)?$/ $settings{local_timezone}/;
1413+
$row->{checked} =~ s/:\d\d(?:\.\d+)?$/ $settings{local_timezone}/;
1414+
1415+
$row = { map { $_ => $row->{$_} } qw(name cat maintainer ver method newver newurl checked updated discovered) };
1416+
1417+
print $jf ',' unless $firstitem;
1418+
print $jf encode_json($row);
1419+
$firstitem = 0;
1420+
}
1421+
print $jf ']';
1422+
close $jf;
1423+
13801424
finish_sql($dbh, \%sths);
13811425
$dbh->disconnect;
13821426
}

portscout.pod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Plus we need a few Perl modules:
4545
- MIME::Lite
4646
- XML::XPath
4747
- XML::XPath::XMLParser
48+
- JSON
4849

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

0 commit comments

Comments
 (0)