| 1 | |||
| 2 | package NOLookup::Patent::DataLookup; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use base qw(Class::Accessor::Chained); ## Provides a new() method | ||
| 7 | use POSIX qw(locale_h); | ||
| 8 | use NOLookup::Patent::Entry; | ||
| 9 | use WWW::Mechanize; | ||
| 10 | use JSON; | ||
| 11 | use URI::Encode qw(uri_encode); | ||
| 12 | use Data::Dumper; | ||
| 13 | $Data::Dumper::Indent=1; | ||
| 14 | |||
| 15 | my $PATENT_TIMEOUT = 60; # secs (default is 180 secs but we want shorter time). | ||
| 16 | |||
| 17 | #### | ||
| 18 | # API ref: https://ws.patentstyret.no/search/metadata | ||
| 19 | # https://ws.patentstyret.no/search/json/metadata?op=Search | ||
| 20 | ### | ||
| 21 | |||
| 22 | # Free text search gives too many hits, so use the trademark-text-search | ||
| 23 | my $PATENT_SEARCH = "https://ws.patentstyret.no/search/json/reply/search/trademark/?trademarktext"; | ||
| 24 | my $PATENT_ID = "https://ws.patentstyret.no/search/json/reply/DetailedInfoRequest/trademark/detailedinfo/?ApplicationNumber"; | ||
| 25 | |||
| 26 | my @module_methods = qw / | ||
| 27 | |||
| 28 | lookup_tm_text | ||
| 29 | lookup_tm_applid | ||
| 30 | |||
| 31 | status | ||
| 32 | error | ||
| 33 | warning | ||
| 34 | |||
| 35 | data | ||
| 36 | size | ||
| 37 | total_size | ||
| 38 | |||
| 39 | raw_json_decoded | ||
| 40 | /; | ||
| 41 | |||
| 42 | # The new method and also the accessor methods | ||
| 43 | __PACKAGE__->mk_accessors( | ||
| 44 | @module_methods | ||
| 45 | ); | ||
| 46 | |||
| 47 | ######## L o o k u p | ||
| 48 | |||
| 49 | sub lookup_tm_text { | ||
| 50 | my ($self, $tm_text, $max_no_pages, $page_ix, $page_sz) = @_; | ||
| 51 | |||
| 52 | die "mandatory parameter 'tm_text' not specified" unless ($tm_text); | ||
| 53 | |||
| 54 | my $max_pg_to_fetch = $max_no_pages || 10; | ||
| 55 | my $pg_ix = $page_ix || 1; | ||
| 56 | my $max_pg_elems = $page_sz || 100; | ||
| 57 | |||
| 58 | my $tmt_e = uri_encode($tm_text, {encode_reserved => 1}); | ||
| 59 | |||
| 60 | # set size and total size so we get the first lookup | ||
| 61 | $self->size(0); | ||
| 62 | $self->total_size(1); | ||
| 63 | my $bsize = -1; | ||
| 64 | |||
| 65 | # Fetch the requested pages | ||
| 66 | my $pix = $pg_ix; | ||
| 67 | my $pcnt = 1; | ||
| 68 | |||
| 69 | while ($pcnt <= $max_pg_to_fetch && $bsize < $self->size && $self->size < $self->total_size) { | ||
| 70 | #print STDERR "Patent page count is: $pcnt of $max_pg_to_fetch, fetching page $pix...\n"; | ||
| 71 | |||
| 72 | # Remember size before next lookup. If size has not increased, stop the loop. | ||
| 73 | $bsize = $self->size; | ||
| 74 | |||
| 75 | #print STDERR ("$PATENT_SEARCH=$tmt_e&page=$pix&MaximumSearchResults=$max_pg_elems\n"); | ||
| 76 | $self->_lookup_tm_entries("$PATENT_SEARCH=$tmt_e&page=$pix&MaximumSearchResults=$max_pg_elems"); | ||
| 77 | ++$pcnt; | ||
| 78 | ++$pix; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | sub lookup_tm_applid { | ||
| 83 | my ($self, $applid) = @_; | ||
| 84 | |||
| 85 | $self->_lookup_tm_entries(uri_encode("$PATENT_ID=$applid")); | ||
| 86 | |||
| 87 | } | ||
| 88 | |||
| 89 | ##### | ||
| 90 | # The lookup itself, internal method only. | ||
| 91 | |||
| 92 | sub _lookup_tm_entries { | ||
| 93 | my ($self, $URL) = @_; | ||
| 94 | |||
| 95 | print STDERR "URL: $URL\n" if ($self->{debug}); | ||
| 96 | |||
| 97 | my $mech = WWW::Mechanize->new( | ||
| 98 | timeout => $PATENT_TIMEOUT, | ||
| 99 | autocheck => 0 | ||
| 100 | ); | ||
| 101 | |||
| 102 | my $resp = $mech->get($URL); | ||
| 103 | |||
| 104 | unless ($mech->success) { | ||
| 105 | $self->status($resp->status_line); | ||
| 106 | $self->error(1); | ||
| 107 | # If a content exists, try a decode of it before return is done | ||
| 108 | return $self unless ($mech->content); | ||
| 109 | } | ||
| 110 | |||
| 111 | my $json; | ||
| 112 | { | ||
| 113 | local $@; # protect existing $@ | ||
| 114 | eval { | ||
| 115 | $json = decode_json($mech->content(format=>'text')); | ||
| 116 | }; | ||
| 117 | if ($@) { | ||
| 118 | # Some error | ||
| 119 | $self->status("Invalid response"); | ||
| 120 | $self->error(1); | ||
| 121 | $self->next_page(undef); | ||
| 122 | return $self; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | if ($json) { | ||
| 127 | $self->raw_json_decoded($json); | ||
| 128 | |||
| 129 | # From above: For errors we now have a decoded content, time to return. | ||
| 130 | #print STDERR "TM lookup error: ", Dumper $mech; | ||
| 131 | return $self if ($self->error); | ||
| 132 | |||
| 133 | # Map the json data structure to | ||
| 134 | # internal entry objects | ||
| 135 | my $eo = NOLookup::Patent::Entry->new; | ||
| 136 | my $entries = $eo->map_json_entries($json); | ||
| 137 | |||
| 138 | # Collect any accumulated problems | ||
| 139 | $self->status($eo->status) if ($eo->status); | ||
| 140 | $self->warning($eo->warning) if ($eo->warning); | ||
| 141 | $self->error($eo->error) if ($eo->error); | ||
| 142 | |||
| 143 | # Save the found data | ||
| 144 | if (@$entries) { | ||
| 145 | push @{$self->{data}}, @$entries; | ||
| 146 | $self->size($self->size + $eo->result_count); | ||
| 147 | $self->total_size($eo->total_result_count); | ||
| 148 | } else { | ||
| 149 | $self->total_size(0); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | return $self; | ||
| 153 | } | ||
| 154 | |||
| 155 | =pod | ||
| 156 | |||
| 157 | =encoding ISO-8859-1 | ||
| 158 | |||
| 159 | =head1 NAME | ||
| 160 | |||
| 161 | NOLookup::Patent::DataLookup - Lookup Patentstyret trademark data from the JSON | ||
| 162 | formatted service offered by the JSON API. | ||
| 163 | |||
| 164 | =head1 DESCRIPTION | ||
| 165 | |||
| 166 | Use WWW::Mechanize and JSON to lookup data from the JSON API. | ||
| 167 | Use NOLookup::Patent::Entry module to map the resulting json data structure | ||
| 168 | to a NOLookup::Patent::Entry objects. | ||
| 169 | |||
| 170 | The timeout period $PATENT_TIMEOUT is used to | ||
| 171 | trap and return the call if lookup is hanging for some reason. | ||
| 172 | |||
| 173 | =head1 METHODS | ||
| 174 | |||
| 175 | The module provides the below methods. | ||
| 176 | |||
| 177 | =head3 lookup_tm_text() | ||
| 178 | |||
| 179 | Lookup based on a complete or part of a trademark text. | ||
| 180 | |||
| 181 | Mandatory parameters are: | ||
| 182 | $tm_text: the trademark text (or part of) to search for | ||
| 183 | |||
| 184 | Optional parameters are: | ||
| 185 | $max_no_pages: max. number of pages to return, default 10. | ||
| 186 | $page_ix : index to the first page to fetch, default 1. | ||
| 187 | $page_sz : number of entries per page, default 100. | ||
| 188 | |||
| 189 | Returns 0 entries if none found. | ||
| 190 | |||
| 191 | Returns an array of NOLookup::Patent::Entry objects in the data | ||
| 192 | when matches are found. | ||
| 193 | |||
| 194 | =head3 lookup_applid() | ||
| 195 | |||
| 196 | Lookup based on an application id. | ||
| 197 | |||
| 198 | Returns 0 entries if none found, or some error. | ||
| 199 | |||
| 200 | Returns an array of on single NOLookup::Patent::Entry object in the data | ||
| 201 | when matches are found. | ||
| 202 | |||
| 203 | =head3 raw_json_decoded() | ||
| 204 | |||
| 205 | The raw JSON data structure, as returned by the JSON service, | ||
| 206 | and then decoded to a perl data structure with decode_json(). | ||
| 207 | |||
| 208 | =head3 error() | ||
| 209 | |||
| 210 | Set if an error has occured. | ||
| 211 | On errors, the lookup terminates, with more info | ||
| 212 | in the status(). | ||
| 213 | |||
| 214 | =head3 warning() | ||
| 215 | |||
| 216 | Set if a warning has occured. | ||
| 217 | On warnings, the lookup continues, but warnings | ||
| 218 | are accumulated in the status(). | ||
| 219 | |||
| 220 | =head3 status() | ||
| 221 | |||
| 222 | Further description of error/warning situations. | ||
| 223 | |||
| 224 | =head3 data() | ||
| 225 | |||
| 226 | Found data, an array of NOLookup::Patent::Entry data objects. | ||
| 227 | |||
| 228 | See doc. for C<NOLookup::Patent::Entry> for details. | ||
| 229 | |||
| 230 | =head3 size() | ||
| 231 | |||
| 232 | The number of objects in data(). | ||
| 233 | |||
| 234 | =head3 total_size() | ||
| 235 | |||
| 236 | The total number of matches for the search. So, if size() is less | ||
| 237 | than total_size(), there is more data which is not fetched. | ||
| 238 | |||
| 239 | =head1 SUPPORT | ||
| 240 | |||
| 241 | For now, support questions should be sent to: | ||
| 242 | |||
| 243 | E<lt>(nospam)info(at)norid.noE<gt> | ||
| 244 | |||
| 245 | Please also see the SUPPORT file in the distribution. | ||
| 246 | |||
| 247 | =head1 SEE ALSO | ||
| 248 | |||
| 249 | C<NOLookup::Patent::Entry> | ||
| 250 | L<http://www.patentstyret.no> | ||
| 251 | L<http://ws.patentstyret.no/search> | ||
| 252 | L<https://dbsearch2.patentstyret.no/AdvancedSearch.aspx?Category=Mark> | ||
| 253 | |||
| 254 | =head1 AUTHOR | ||
| 255 | |||
| 256 | Trond Haugen, E<lt>(nospam)info(at)norid.noE<gt> | ||
| 257 | |||
| 258 | =head1 COPYRIGHT | ||
| 259 | |||
| 260 | Copyright (c) 2017 Trond Haugen <(nospam)info(at)norid.no>. | ||
| 261 | All rights reserved. | ||
| 262 | |||
| 263 | This program is free software; you can redistribute it and/or modify | ||
| 264 | it under the terms of the GNU General Public License as published by | ||
| 265 | the Free Software Foundation; either version 2 of the License, or | ||
| 266 | (at your option) any later version. | ||
| 267 | |||
| 268 | =head1 LICENSE | ||
| 269 | |||
| 270 | This library is free software. You can redistribute it and/or modify | ||
| 271 | it under the same terms as Perl itself. | ||
| 272 | |||
| 273 | =cut | ||
| 274 | |||
| 275 | 1; |