Rosetta Code/Count examples: Difference between revisions

no edit summary
m (→‎{{header|D}}: Using works with template)
No edit summary
Line 61:
Stderr (client.getResponse);
}
}
</lang>
 
=={{header|Perl}}==
<lang Perl>
#!/usr/bin/perl -w
use strict ;
use LWP::UserAgent ;
use HTML::Parser ;
use constant DOCROOT => "http://www.rosettacode.org/wiki" ;
use constant SOLUTIONROOT => "http://www.rosettacode.org/w/index.php?title=" ;
my %tasklist = ( ) ; #key: last part of solution list URL, value: title of solution
my $ua = new LWP::UserAgent ;
my $url = DOCROOT . "/Category:Programming_Tasks" ;
my $request = HTTP::Request->new( 'GET' => "$url" ) ;
my $response = $ua->request( $request ) ;
my $counted = 0 ;
my $total_examples = 0 ;
my $solresponse ;
my $p = HTML::Parser->new( api_version => 3 ) ; #parser for list of tasks
my $q = HTML::Parser->new( api_version => 3 ) ; #parser for solutions by task
$p->handler( start => \&process , "tagname , attr" ) ;
$q->handler( text => \&langfinder, "text" ) ;
 
if ( $response->is_success( ) ) {
$p->parse( $response->content( ) ) ;
foreach my $task( keys %tasklist ) {
$request->uri( SOLUTIONROOT . "$task" . "&action=edit" ) ;
$solresponse = $ua->request( $request ) ;
if ( $solresponse->is_success( )) {
$q->parse( $solresponse->content( ) ) ;
if ( $tasklist{$task} ) {
print "$tasklist{$task} : $counted examples!\n" ;
}
$counted = 0 ;
$q->eof( ) ;
}
else {
print "Error: " . $solresponse->code( ) . " " . $solresponse->message( ) . "\n" ;
}
}
$p->eof( ) ;
print "\nTotal: $total_examples examples.\n" ;
}
else {
print "Error " . $response->code( ) . " " . $response->message( ) . "\n" ;
}
sub process( ) {
return if shift ne "a" ;
my $props = shift ;
if ( $props->{href} && $props->{href} =~ m,/wiki/([^:]+), ) {
if ( $1 !~ /Category/ ) {
$tasklist{ $1 } = $props->{title} ;
}
}
}
sub langfinder( ) {
my $text = shift ;
while ( $text =~ /header\|.+\}/g ) {
$counted++ ;
$total_examples++ ;
}
}
</lang>
Line 152 ⟶ 214:
 
This is the ScreenScrape class imported in the above class.
 
<lang java>
import java.io.*;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
 
public class ScreenScrape {
 
public String read(String sUrl) throws Exception {
String lastHtml = "";
BufferedReader reader;
if (sUrl.startsWith("https://")) { // The URL class doesn't like HTTPS so we have to check for it.
reader = new BufferedReader(
new InputStreamReader(
getSSLOutputCon(sUrl).getInputStream()));
} else if (sUrl.startsWith("http://")) { // for nonsecure http
reader = new BufferedReader(
new InputStreamReader(
new URL(sUrl).openStream()));
} else {
return "Protocol not supported. Please verify that your URL starts with "
+ "\"http://\" or \"https://\" and try again.";
}
String line = reader.readLine();
while (line != null) {
lastHtml += line;
line = reader.readLine();
}
return lastHtml;
}
private HttpsURLConnection httpsUrlCon;
private HttpsURLConnection getSSLOutputCon(String sUrl) throws Exception {
try {
URL url = new URL(sUrl);
httpsUrlCon = (HttpsURLConnection) url.openConnection();
httpsUrlCon.setHostnameVerifier(new javax.net.ssl.HostnameVerifier() {
public boolean verify(String hostname, javax.net.ssl.SSLSession certHostName) {
return true;
}
});
} catch(Exception e) {
throw e;
}
return httpsUrlCon;
}
}
</lang>
260

edits