Rosetta Code/List authors of task descriptions: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Oops, months should not be zero indexed)
m (→‎{{header|Perl 6}}: work around wonky syntax highlighting)
Line 290: Line 290:
my $html = $ua.get($page).content;
my $html = $ua.get($page).content;
my $xmldoc = parse-html($html, :TAG<div>, :id<mw-pages>);
my $xmldoc = parse-html($html, :TAG<div>, :id<mw-pages>);
my @tasks = parse-html($xmldoc[0].Str, :TAG<li>).Str.comb( /'/wiki/' <-["]>+ / )».substr(6); #'
my @tasks = parse-html($xmldoc[0].Str, :TAG<li>).Str.comb( /'/wiki/' <-["]>+ / )».substr(6); #'"
my $f = open("./RC_{$category}.txt", :w) or die "$!\n";
my $f = open("./RC_{$category}.txt", :w) or die "$!\n";
note "Writing $category file...";
note "Writing $category file...";
Line 347: Line 347:


# Parse out human readable title
# Parse out human readable title
$line ~~ m| '<a href="/mw/index.php?title=' $title '&amp;' .+? 'title="'(<-["]>+)'"' |;
$line ~~ m| '<a href="/mw/index.php?title=' $title '&amp;' .+? 'title="'(<-["]>+)'"' |; #"'
%tasks{$title}{'title'} = $0.Str;
%tasks{$title}{'title'} = $0.Str;



Revision as of 00:42, 11 October 2017

Rosetta Code/List authors of task descriptions is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

In this task, the goal is to compile an authorship list for task descriptions. A pseudocode example (in imperative style) that should accomplish this is as follows:

<lang pseudocode>for each task page

 grab page source, discard everything after the first ==section==.

Cache as $previous. Note $author.

 for each revision
   grab page source, discard everything after first ==section==.

Cache as $previous2. Note $author2

   compare $previous2 to $previous. If different, record $author to $list.
   replace $previous with $previous2
   replace $author with $author2</lang>

The following resources for HTTP interface information for MediaWiki may prove to be useful:

Conversely, some languages have libraries which abstract these interfaces into language-native idioms. Use of these abstractions is perfectly fine.


Please DO NOT add a full output for each programming language; just show a representative sample. One full list is useful. Multiple full lists just use space and bandwidth.

As of 2017-10-10 | Total: 1067 / Tasks: 859 / Draft Tasks: 208
UserAuthored
    1. 2008-09-25 - Task: HTTP
    1. 2007-01-23 - Task: SOAP
    1. 2010-04-08 - Task: A+B
    1. 2011-01-20 - Draft: VList

Perl 6

Works with: Rakudo version 2017.08

The pseudocode above is no longer really useful as the page format has changed significantly sine ths task was written. Rather than checking every edit to see if it was a change to the task description, we'll just assume the user that created the page is the task author. This isn't 100% accurate; a very few pages got renamed and recreated by someone other than the original author without preserving the history, so they are misreported (15 Puzzle Game for instance,) but is as good as it is likely to get without extensive manual intervention. Subsequent edits to the task description are not credited. As it is, we must still make thousands of requests and pound the server pretty hard. Checking every edit would make the task several of orders of magnitude more abusive of the server (and my internet connection.)

The task names and author information are saved to local files so it can pick up where it left off if it gets interrupted during processing. As the task creation time (and original editor) never change, don't bother to re-download every time. Just update the category (Draft or Task) as that is the only thing that really changes. If a task name gets edited, manual intervention is required. Either edit the JSON file with the task information or just delete it and recreate it from scratch.

<lang perl6>use HTTP::UserAgent; use Gumbo; use Sort::Naturally; use JSON::Fast;

my $ua = HTTP::UserAgent.new;

for 'Programming_Tasks', 'Draft_Programming_Tasks' -> $category { # Get lists of Tasks & Draft Tasks

   #last; # Uncomment to skip this step
   say "Updating $category list...";
   my $page   = "http://rosettacode.org/wiki/Category:$category";
   my $html   = $ua.get($page).content;

my $xmldoc = parse-html($html, :TAG

, :id<mw-pages>); my @tasks = parse-html($xmldoc[0].Str, :TAG
  • ).Str.comb( /'/wiki/' <-["]>+ / )».substr(6); #'" my $f = open("./RC_{$category}.txt", :w) or die "$!\n"; note "Writing $category file..."; $f.print( @tasks.join("\n") ); $f.close; } my %cat = ( # Friendlier descriptions for task categories 'Programming_Tasks' => 'Task: ', 'Draft_Programming_Tasks' => 'Draft:' );
    1. Month names for date manipulations
    my %months = <January February March April May June July August September October November December> Z=> 1..12; my $hashfile = './RC_hash.json'; my $htmlfile = './RC_Authors.html'; note "Reading JSON hash file..."; my %tasks = $hashfile.IO.e ?? $hashfile.IO.slurp.&from-json !! ( ); for 'Programming_Tasks', 'Draft_Programming_Tasks' -> $category { # Scrape info from each page. #last; # Uncomment to skip this step note "Loading $category file..."; my @entries = "./RC_{$category}.txt".IO.slurp.lines; for @entries -> $title { # Update the category as that is the only thing that can really change. %tasks{$title}{'category'} = %cat{$category}; # Otherwise skip if it has already been indexed. The creation date can't change # the task name *can* change, but it is exceedinly rare if %tasks{$title}{'title'}:exists { note $title; next; } # Get the earliest edit my $html = $ua.get: "http://rosettacode.org/mw/index.php?title={$title}&dir=prev&limit=1&action=history"; # Filter out the actual history links $html.content ~~ m|'
  • ' (.+?) ''|;
           # Only interested in the oldest (last in the list)
           my $line = $0.lines.tail;
    
           # Parse out the User name
           $line ~~ m| 'title="User:' <-[>]>+? '>' (.+?) '</a>' |;
           my $auth = $0 ?? $0.Str !! ;
           # Oops, no user name, must be anonymous, get IP address instead
           unless $auth {
               $line ~~ m| '"mw-userlink mw-anonuserlink">' (.+?) '</a>' |;
               $auth = $0.Str;
           }
           %tasks{$title}{'author'} = $auth;
    
           # Parse out human readable title
           $line ~~ m| '<a href="/mw/index.php?title=' $title '&' .+? 'title="'(<-["]>+)'"' |; #"'
           %tasks{$title}{'title'} = $0.Str;
    
           # Parse out date task was added, convert date to ISO format
           $line ~~ m| 'class="mw-changeslist-date">' <-[\s]>+  (<-[<]>+) '</a>‎' |;
           %tasks{$title}{'date'} = $0.Str.trim.&toISO8601;
    
           # report progress
           note $title;
    
           # save to a file
           $hashfile.IO.spurt(%tasks.&to-json);
    
           sleep 3; # Don't pound the server
       }
    

    }

    1. Convert saved task / author info to an HTML table

    note "Building HTML table..."; my $count = +%tasks; my $taskcnt = +%tasks.grep: *.value.<category> eq %cat<Programming_Tasks>; my $draftcnt = $count - $taskcnt;

    1. Dump an HTML table to a file

    my $out = open($htmlfile, :w) or die "$!\n";

    1. Add table boilerplate and header
    $out.say( '' );
    1. Get sorted unique list of task authors

    for %tasks{*}».<author>.unique.sort(*.&naturally) -> $author {

    $out.print( '' );

    }

    $out.say( '
    As of ', Date.today, ' | Total: ',
     "$count / Tasks: $taskcnt / Draft Tasks: $draftcnt",
    
    '
    UserAuthored
        ' );
           # Get list of tasks by this author, sorted by name
           for %tasks.grep( { $_.value.<author> eq $author } ).sort(*.key.&naturally) -> $task {
               # and add them
        
        $out.print( "
      1. {$task.value.<date>} - {$task.value.<category>}", " [[{$task.key}|{$task.value.<title>}]]
      2. " ); } $out.say( '
    ' );

    $out.close;

    say "HTML table file saved as: {$htmlfile.IO.absolute}";

    sub toISO8601 ($date) { # convert day month year to YYYY-MM-DD

       my @dmy = $date.split: ' ';
       sprintf "%4d-%02d-%02d", @dmy[2].Int, %months{@dmy[1]}, @dmy[0].Int;
    

    } </lang>

    Sample output
    As of 2017-10-10 | Total: 1067 / Tasks: 859 / Draft Tasks: 208
    UserAuthored

    Many rows omitted...