Skip to content

Instantly share code, notes, and snippets.

@nanis
Last active November 8, 2015 18:06
Show Gist options
  • Save nanis/0f15bffc9b31df57069f to your computer and use it in GitHub Desktop.
Save nanis/0f15bffc9b31df57069f to your computer and use it in GitHub Desktop.
A quick'n'dirty Perl script to download the entire TPP and combine it into a single PDF document
#!/usr/bin/env perl
=for comment
Office of the Unites States Trade Representative released the full text of the
Trans-Pacific Partnership trade agreement on November 5, 2015. For some reason,
it was released in 239 individual PDF documents, instead of a single file.
ToC at https://ustr.gov/trade-agreements/free-trade-agreements/trans-pacific-partnership/tpp-full-text
This script downloads the individual parts in order, and assembles them into a
single document.
It sleeps a few seconds between requests so as to avoid hammering the server too
hard. If you decide to use this to download your own copy of the text, do keep the
sleep statement.
=cut
use utf8;
use strict;
use warnings;
use HTML::TokeParser::Simple;
use PDF::Reuse;
run('tpp-full-text');
sub run {
my $toc = shift;
my $parser = HTML::TokeParser::Simple->new(file => $toc);
my $partno;
my @files;
while (my $anchor = $parser->get_tag('a')) {
my $href = $anchor->get_attr('href');
next unless $href;
next unless $href =~ / [.] pdf \z/xi;
my $file = sprintf '%03d-%s', ++$partno, (split qr{/}, $href)[-1];
if (-e $file) {
push @files, $file;
next;
}
my @cmd = (wget => $href => '-O' => $file);
warn "@cmd";
system @cmd;
if (-e $file) {
push @files, $file;
}
my $duration = int(1 + rand(7));
warn "Sleeping $duration seconds\n";
sleep $duration;
}
print "TPP has $partno parts\n";
printf "Successfully downloaded %d of them\n", scalar @files;
print "Combining them into a single document\n";
prFile('TPP.pdf');
prDoc($_) for @files;
prEnd();
return;
}
=for LICENSE
The MIT License (MIT)
Copyright © 2015 A. Sinan Unur
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
=cut
@nanis
Copy link
Author

nanis commented Nov 8, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment