Rosetta Code/Rank languages by popularity: Difference between revisions

Updated for changes in the MediaWiki API and the JDK
(Updated for changes in the MediaWiki API and the JDK)
Line 2,577:
 
=={{header|Java}}==
Tested with Java 1.722. Uses the api.<br/>
<syntaxhighlight lang="java">import java.net.URL;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.io.*;
Line 2,625 ⟶ 2,627:
{
 
URL url = new URLURI( path ).toURL();
URLConnection rc = url.openConnection();
// Rosetta Code objects to the default Java user agant so use a blank one
rc.setRequestProperty( "User-Agent", "" );
BufferedReader bfr = new BufferedReader( new InputStreamReader( rc.getInputStream() ) );
Line 2,636:
while( line != null )
{
line = line.trim().replaceAll( "[\",]", "" );
if ( line.startsWith( "[title]" ) )
{
// have a programming language - should look like "[title] =>: Category:languageName"
languageName = after( after( line, ':' ), ':' ).trim();
}
else if( line.startsWith( "[pages]" ) )
{
// number of pages the language has (probably)
String pageCount = after( line, '>:' ).trim();
if( pageCount.compareTo( "Array{" ) != 0 )
{
// haven't got "[pages]: => Array{" - must be a number of pages
languageList.add( ( (char) Integer.parseInt( pageCount ) ) + languageName );
languageName = "?";
} // if [pageCount.compareTo( "Array{" ) != 0
}
else if( line.startsWith( "[gcmcontinue]" ) )
{
// have an indication of wether there is more data or not
gcmcontinue[0] = after( line, '>:' ).trim().replaceAll( "[|]", "%7C" );
} // if various line starts
line = bfr.readLine();
Line 2,676:
do
{
String path = ( "httphttps://www.rosettacode.org/mww/api.php?action=query"
+ "&generator=categorymembers"
+ "&gcmtitle=Category:Programming%20Languages"
Line 2,682:
+ ( gcmcontinue[0].compareTo( "" ) == 0 ? "" : ( "&gcmcontinue=" + gcmcontinue[0] ) )
+ "&prop=categoryinfo"
+ "&format=txtjsonfm"
);
parseContent( path, gcmcontinue, languageList );
Line 2,708:
} // for lPos
} // main
} // GetRCLanguages</syntaxhighlight>
</syntaxhighlight>
{{out}}
Top 10 languages as at 27th1st AugustJune 20152024
<pre>
1: 8831675: TclPhix
21: 8751675: RacketWren
3: 8371650: PythonJulia
4: 7991620: JRaku
5: 7721576: RubyNim
6: 7631549: Perl 6Go
7: 7561542: CPerl
8: 7421514: GoPython
9: 7371413: DJ
10: 7071346: PerlJava
...
</pre>
3,048

edits