Jump to content
Wiki Home
EPrints Home
Bazaar
Files
Main menu
Main menu
move to sidebar
hide
Navigation
Front Page
EPrints 3 Manual
EPrints Training Course
EPrints Glossary
Installing EPrints
How-to Configure EPrints
EPrints 3.2+ API
EPrints 3 Reference
EPrints Plugins
Wiki
All categories
Recent changes
Main Page
Help
EPrints Documentation
Search
Search
Appearance
Log in
Request account
Personal tools
Log in
Request account
Pages for logged out editors
learn more
Contributions
Talk
Editing
Contribute: Plugins/ImportPluginsAWS
(section)
Page
Discussion
British English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
= In More Detail = We will use the URI::Escape module in this plugin. As it is included with EPrints we don't need to check if it exists first. <pre> use URI::Escape; </pre> Here we setup a number of values for parameters that will be part of our web service requests. The endpoint variable determines which server will be sent the request. Here we have used the UK server, but by changing the [http://en.wikipedia.org/wiki/TLD TLD] we can use the US, Canadian, German or French servers. The accesskey variable stores the access key you will have gained from signing up to Amazon earlier. You should use the normal access key and not the secret one. Here we use the ItemLookup operation of the AWSECommerceService with the 2007-07-16 version of the API. Other operations allow searching for items, but here we want to look up specific products. Finally the variable responsegroup determines the amount and nature of the information returned, we select "Large" in this case, which gives us more information about the item. <pre> my $endpoint = 'http://ecs.amazonaws.co.uk/onca/xml'; my $accesskey = '<YOURAMAZONWSKEY>'; my $service = 'AWSECommerceService'; my $operation = 'ItemLookup'; my $version = '2007-07-16'; my $responsegroup = 'Large'; </pre> == Constructor == The constructor is similar to the one used for the [Contribute:_Plugins/ImportPluginsCSV CSV plugin], except this one will import individual eprints, given an [http://en.wikipedia.org/wiki/ASIN ASIN]. <pre> $self->{produce} = [ 'list/eprint' , 'dataobj/eprint']; </pre> Like we imported Text::CSV in the [Contribute:_Plugins/ImportPluginsCSV last tutorial], here we import LWP::UserAgent which will be used for making requests to the web service. <pre> my $rc = EPrints::Utils::require_if_exists('LWP::UserAgent'); unless ($rc) { $self->{visible} = ''; $self->{error} = 'Unable to load required module LWP::UserAgent'; } </pre> == Input == === input_fh === This method is similar to the one used in the [Contribute:_Plugins/ImportPluginsCSV CSV plugin], but doesn't have to do quite so much work. First we create the array to hold our imported eprint ids. <pre> my @ids; </pre> Next we read all the lines in the supplied file handle into our records array. <pre> my $fh = $opts{fh}; my @records = <$fh>; </pre> Then we iterate over each record, running convert_input on it, importing it into our repository and adding the id to our array. <pre> foreach my $input_data (@records) { my $epdata = $plugin->convert_input($input_data); next unless defined $epdata; my $dataobj = $plugin->epdata_to_dataobj($opts{dataset},$epdata); if( defined $dataobj ) { push @ids, $dataobj->get_id; } } </pre> Then we return a List object of the items imported. <pre> return EPrints::List->new( dataset => $opts{dataset}, session => $plugin->{session}, ids=>\@ids ); </pre> === convert_input === [http://en.wikipedia.org/wiki/ASIN ASINs] are strings which identify a product. Here we remove any non-alphanumerical characters which are surrounding the [http://en.wikipedia.org/wiki/ASIN ASIN]. <pre> $input =~ m/([A-Za-z0-9]+)/; $input = $1; </pre> We form the request from the variables we created earlier and the [http://en.wikipedia.org/wiki/ASIN ASIN] we have just obtained. <pre> my $request = "$endpoint?". "Service=$service&". "AWSAccessKeyId=$accesskey&". "Operation=$operation&". "ItemId=$input&". "Version=$version&". "ResponseGroup=$responsegroup"; </pre> We now send the request, by creating a new LWP::UserAgent object, setting its timeout to 30 seconds and then performing the request using HTTP GET. <pre> my $ua = LWP::UserAgent->new; $ua->timeout(30); my $response = $ua->get($request); </pre> We then create a [http://en.wikipedia.org/wiki/Document_Object_Model DOM] object from the XML document returned. <pre> my $dom = EPrints::XML::parse_xml_string($response->content); </pre> Each request contains an Items element within the root element of the document which contains a Request element. This element contains an element IsValid. This element will contain the value True or False depending on whether a valid request was made or not. Here we obtain the Request element and check that the IsValid element within it contains the value True. If it doesn't we call the error method and return undef. <pre> my $rep = $dom->getElementsByTagName("Items")->item(0)-> getElementsByTagName('Request')->item(0); my $reptext = EPrints::Utils::tree_to_utf8($rep->getElementsByTagName('IsValid')->item(0)); unless ($reptext eq 'True') { $plugin->error('Invalid AWS Request'); return undef; } </pre> The product found with the ItemLookup method is contained within an Item element within the Items element. Here we attempt to get that element and raise the error and return undef if we can't. <pre> my $item = $dom->getElementsByTagName('Items')->item(0)-> getElementsByTagName('Item')->item(0); unless (defined $item) { $plugin->error('No Item element found'); return undef; } </pre> Each item contains an ItemAttributes element which contains most of the metadata about an item. <pre> my $attr = $item->getElementsByTagName('ItemAttributes')->item(0); </pre> For some specialised repositories it might make sense to import DVDs, computer games and electronic equipment, but we're just going to deal with books. The ProductGroup element within the ItemAttributes element tells you what sort of item we're dealing with. We're looking for the value 'Book'. <pre> my $pg = EPrints::Utils::tree_to_utf8($attr->getElementsByTagName('ProductGroup')->item(0)); unless ($pg eq 'Book') { $plugin->error('Product is not a book.'); return undef; } </pre> Here we set a few fields without consulting the imported data. We know this is a book, so we set the type. We assume that it has not been refereed. We also assume it has been published, because we can buy it. <pre> $output{type} = 'book'; $output{refereed} = 'FALSE'; $output{ispublished} = 'pub'; </pre> We get and set the title. <pre> my $title = $attr->getElementsByTagName('Title')->item(0); $output{title} = EPrints::Utils::tree_to_utf8($title); </pre> Here we set the official URL to the Amazon product page. We have to do a bit of extra work using the uri_unescape method from the URI::Escape package to convert URI escape codes into characters. <pre> my $url = $item->getElementsByTagName('DetailPageURL')->item(0); $output{official_url} = uri_unescape(EPrints::Utils::tree_to_utf8($url)); </pre> We can set the [http://en.wikipedia.org/wiki/ISBN ISBN]. Note that the [http://en.wikipedia.org/wiki/ISBN ISBN] is often the same as the [http://en.wikipedia.org/wiki/ASIN ASIN]. <pre> my $isbn = $attr->getElementsByTagName('ISBN')->item(0); if (defined $isbn) { $output{isbn} = EPrints::Utils::tree_to_utf8($isbn); } </pre> We can set the number of pages. <pre> my $pages = $attr->getElementsByTagName('NumberOfPages')->item(0); if (defined $pages) { $output{pages} = EPrints::Utils::tree_to_utf8($pages); } </pre> We can set the publisher. <pre> my $publisher = $attr->getElementsByTagName('Publisher')->item(0); if (defined $publisher) { $output{publisher} = EPrints::Utils::tree_to_utf8($publisher); } </pre> We can set the publication date and finally return our output hash. <pre> my $pubdate = $attr->getElementsByTagName('PublicationDate')->item(0); if (defined $pubdate) { $output{date} = EPrints::Utils::tree_to_utf8($pubdate); } return \%output; </pre>
Summary:
Please note that all contributions to EPrints Documentation may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
EPrints Documentation:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)