User:Eidobot

From NetHackWiki
Jump to: navigation, search

Eidobot is written by Eidolos. Please talk to him if you have any issues.

Source code

#!/usr/bin/perl

# Copyright (c) 2006 Shawn M Moore

# Permission is hereby granted, free of charge, to any person obtaining a copy 
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights 
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
# copies of the Software, and to permit persons to whom the Software is 
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in al
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

use strict;
use warnings;

use WWW::Mechanize;

# Eidobot 1.1
# Automates upload of new articles to NetHackWiki.

# It uploads every file it can in the directory called new_articles under the 
# current working directory.
# The filename corresponds exactly to the new article name.

# Change the name and pass variables to your bot's name/pass.

# If the summary variable is empty or undefined, it will instead use the first
# line of each article for its summary, removing it from the article body.

# TODO: More configuration options, error-checking.

our $name    = 'name';
our $pass    = 'pass';
our $summary = 'Automated article upload';
our $minor   = 1; # mark every edit as minor

our $sleep   = 30; # seconds to sleep after each new article

opendir(NEW_ARTICLES, 'new_articles') or die "Unable to open new_articles directory: $!";

our $mech = WWW::Mechanize->new(agent => "Eidobot 1.0");

# LOGIN
$mech->get("http://nethackwiki.com/index.php?title=Special:Userlogin");
$mech->submit_form
(
  form_name => 'userlogin',
  fields    => 
               {
                 wpName     => $name, 
                 wpPassword => $pass,
               },
);

# SUBMIT LOOP
foreach my $article (sort readdir(NEW_ARTICLES))
{
  next if -d $article;

  my $contents = do {local (@ARGV, $/) = "new_articles/$article"; <>};

  $article =~ s/ /_/g;

  if ($mech->get("http://nethackwiki.com/index.php?title=$article&action=edit")->content()
      !~ /To create the page, start typing in the box below/)
  {
    warn "Existing article at $article. Skipping!";
    next;
  }

  $contents =~ s{ ^ (.*) $ \n }{}xm
    and local $summary = $1 
      if !defined($summary) || $summary eq '';

  print "Submitting new article \"$article\" (length " . length($contents) . ").\n";

  $mech->submit_form
  (
    form_name => 'editform',
    fields    =>
                 {
                   wpTextbox1  => $contents,
                   wpSummary   => $summary,
                   wpMinoredit => $minor,
                 },
  );

  sleep($sleep) if defined($sleep) && $sleep > 0;
}