App-wdq

 view release on metacpan or  search on metacpan

script/wdq  view on Meta::CPAN

    my ( $url, %query ) = @_;

    if ( $OPT{response} ) {
        local ( @ARGV, $/ ) = $OPT{response};
        return <>;
    }

    require HTTP::Tiny;
    my $http = HTTP::Tiny->new(
        default_headers => { agent => "wdq/$VERSION" },
        timeout         => 30,
    );

    $query{format} = 'json';

    $url .= '?' . $http->www_form_urlencode( \%query );
    my $res = $http->get($url);

    if ( !$res->{success} ) {
        if ( $OPT{ignore} ) {
            return;
        }
        else {
            warning("HTTP request failed");
            say STDERR $res->{content};
            exit 1;
        }
    }

    $res->{content};
}

sub get_qid_from_sitelink {
    my $api   = "https://$_[0]/w/api.php";
    my $title = $_[1];

    my $res = http_get(
        $api,
        action    => 'query',
        prop      => 'pageprops',
        titles    => $title,
        redirects => 1
    );

    my $data = JSON->new->decode($res);
    my ($page) = values %{ $data->{query}->{pages} };
    return unless $page->{pageprops};
    return $page->{pageprops}->{wikibase_item};
}

sub get_lookup_query {
    my $id = shift;

    my $entity_id;

    if ( $id =~ $ENTITY_PATTERN ) {
        $entity_id = $+{id};
        $entity_id =~ s/Property://i;
    }
    else {
        # URL could be percent-encoded or not, so normalize
        my $uri = URI->new($id);
        if ( $uri and $uri->canonical =~ $SITELINK_PATTERN ) {
            my ( $base, $title ) = ( $+{base}, $+{title} );

            # unescape to UTF-8 octets
            $title =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
            $title = decode_utf8($title);

            if ( $OPT{'no-mediawiki'} ) {
                warning("MediaWiki API disabled");
                exit 1;
            }
            $entity_id = get_qid_from_sitelink( $base, $title );
            unless ($entity_id) {
                warning("Wikidata item not found: $id");
                return;
            }
        }
    }

    unless ($entity_id) {
        warning("unknown identifier: $id");
        return;
    }

    my $uri = "http://www.wikidata.org/entity/" . uc($entity_id);

    return <<SPARQL;
SELECT ?id ?label ?description WHERE {
    BIND(<$uri> AS ?id)
    SERVICE wikibase:label {
        bd:serviceParam wikibase:language "$OPT{language}" .
        ?id rdfs:label ?label ; schema:description ?description
    }
}
SPARQL
}

sub literal { RDF::Trine::Node::Literal->new(@_) }

sub map_ids {
    my $row = shift;
    foreach my $name ( keys %$row ) {
        my $v = $row->{$name};
        next unless $v->isa('RDF::Trine::Node::Resource');
        my $id;
        $id           = $+{id}       if $v->uri_value =~ $ENTITY_PATTERN;
        $id           = $+{id}       if $v->uri_value =~ $WIKIDATA_ID_PATTERN;
        $row->{$name} = literal($id) if $id;
    }
    $row;
}

sub get_sparql {
    my $json = http_get( $OPT{api}, query => shift );
    require RDF::Trine::Iterator::JSONHandler;
    $json
      ? RDF::Trine::Iterator::JSONHandler->new->parse($json)
      : RDF::Trine::Iterator->new( [], 'bindings', [] );
}



( run in 1.727 second using v1.01-cache-2.11-cpan-437f7b0c052 )