source: other-projects/is-sheet-music-encore/trunk/dapiclient2-extended-ZIP.pl@ 33010

Last change on this file since 33010 was 33010, checked in by cpb16, 5 years ago

Updated and Renamed PNG retrieving code, ZIP file downlading is also now possible. All code runs as expected

  • Property svn:executable set to *
File size: 3.9 KB
Line 
1#!/usr/bin/env perl
2
3=head1 NAME
4
5dapiclient_2
6
7=head1 DESCRIPTION
8
9This is an example perl 2-legged oauth client that makes a request to
10the full Data API. Replace OAUTH_CONSUMER_SECRET and
11OAUTH_CONSUMER_KEY (below) with the keys you received from
12http://babel.hathitrust.org/cgi/kgs and MY_IP_ADDRESS by the IP
13address of workstation or server running this script.
14
15It uses a hardcoded URL to request a single page image for a public domain item.
16
17It is intended to aid development of a fully function Data API client
18in Perl or other languages that implement HMAC_SHA1 OAuth libraries.
19
20=head1 SYNOPSIS
21
22At the Unix command line:
23
24 % cd /path/to/dapiclient_2/directory
25 % chmod u+x dapiclient_2
26
27then:
28
29 % curl 'http://yourhost/path_to_client/dapiclient_2' > mdp.39015000000128.png
30
31If your png is corrupt, run without i/o redirection to see the error code:
32
33 % curl 'http://yourhost/path_to_client/dapiclient_2'
34
35or paste the URL to your browser.
36
37=head1 OUTPUT
38
39Stream of data for the above mentioned page image file or error code.
40
41=cut
42
43use strict;
44use warnings;
45
46use CGI;
47use OAuth::Lite::Consumer;
48use OAuth::Lite::AuthMethod;
49
50# read_file()
51# Derived from:
52# https://stackoverflow.com/questions/953707/in-perl-how-can-i-read-an-entire-file-into-a-string
53sub read_file
54{
55 my ($file) = @_;
56
57 my $text = do {
58 local $/ = undef;
59 open my $fh, "<", $file
60 || die "could not open $file: $!";
61 <$fh>;
62 };
63
64 chomp($text);
65 return $text;
66}
67
68my $DEBUG=1;
69my $WEB_DISPLAY=0;
70
71my $access_key = 'OAUTH_CONSUMER_KEY'; # replace with your access_key
72my $secret_key = 'OAUTH_CONSUMER_SECRET'; # replace with your secret_key
73my $ip_address = 'MY_IP_ADDRESS'; # replace with the IP address of your workstation or server
74
75if (-f "my_access_key.txt") {
76 $access_key = read_file("my_access_key.txt");
77}
78
79if (-f "my_secret_key.txt") {
80 $secret_key = read_file("my_secret_key.txt");
81}
82
83if (-f "my_ip_address.txt") {
84 $ip_address = read_file("my_ip_address.txt");
85}
86
87my $base_request_url = "https://babel.hathitrust.org/cgi/htd/aggregate/";
88
89my $request_url;
90if (defined $ARGV[0]) {
91 $request_url = $base_request_url.$ARGV[0];
92}
93else {
94 # The following ID is to the example given in the Data API documentation
95 # $request_url = $base_request_url."mdp.39015000000128/1";
96
97 # The following is an example of a 'open-open' sheet music doc
98 $request_url = $base_request_url."mdp.39015070661841/1";
99}
100
101my $consumer = OAuth::Lite::Consumer->new
102 (
103 consumer_key => $access_key,
104 consumer_secret => $secret_key,
105 auth_method => OAuth::Lite::AuthMethod::URL_QUERY,
106 );
107
108if ($DEBUG) {
109 my $params = $consumer->gen_auth_params('GET', $request_url);
110 print STDERR "(Info) Resolved URL for retrieval:\n";
111 print STDERR "$request_url?v=2&";
112 print STDERR "oauth_consumer_key=$params->{'oauth_consumer_key'}&";
113 print STDERR "oauth_timestamp=$params->{'oauth_timestamp'}&";
114 print STDERR "oauth_nonce=$params->{'oauth_nonce'}&";
115 print STDERR "oauth_signature_method=$params->{'oauth_signature_method'}&";
116 print STDERR "oauth_signature=$params->{'oauth_signature'}&";
117 print STDERR "oauth_version=$params->{'oauth_version'}\n";
118}
119
120my $response = $consumer->request
121 (
122 method => 'GET',
123 url => $request_url,
124 params => {
125 v => '2',
126 #cformat => 'png',
127 ip => $ip_address,
128 },
129 );
130
131my $success = $response->is_success;
132my $content_type = 'image/png';
133
134unless ($success) {
135 $content_type = 'text/html';
136}
137
138if ($WEB_DISPLAY) {
139 print CGI::header(
140 Content_type => $content_type,
141 Status => $response->status_line,
142 );
143
144 unless ($success) {
145 print "<p><b>Error: " . $response->content . "</b><br/>";
146 }
147 else {
148 print $response->content;
149 }
150}
151else {
152 print $response->content;
153}
154exit $success;
155
156
157
Note: See TracBrowser for help on using the repository browser.