使用者:下一次登錄/mwpush.pl
外觀
這是一個自動向維基百科提交編輯內容的機器人(程序),用Perl腳本語言寫成。
使用詳細說明
[編輯]我的測試是在WinXP操作系統下進行的。步驟:
- 自http://www.activestate.com/Products/ActivePerl/下載ActivePERL,是一種PERL語言的開發環境。大概8M,安裝。
- 自http://www.cygwin.com/下載Cygwin,可以在Windows下模擬Unix的腳本語言功能(Shell)。大概17M,安裝。
- 把Perl/bin/中的內容拷貝至Cygwin/bin/中,Perl/lib/至Cygwin/lib/,其他Perl/下的目錄拷貝至Cygwin/。
- 在Cygwin/目錄下把下面的代碼保存為「mwpush.pl」文件,將要提交的內容保存至「1.txt」文件。
- 運行cygwin.bat。
- 輸入
mwpush.pl -l (你的用户名,最好是全英文) -p (登录密码) -w baike.710302.xyz -t (目标条目名称) 1.txt
注意:可能覆蓋以前的條目版本!!!一定要小心!!!
代碼
[編輯]#!/usr/bin/perl # mwpush.pl - Push page data to a Wikimedia server # By WikiPedia:User:KeithTyler # Portions largely taken or based on upload.pl by WikiPedia:User:Eloquence # call requirements use Getopt::Std; use LWP::Simple; use LWP::UserAgent; use HTTP::Request; use HTTP::Response; use HTTP::Cookies; #use warnings; getopts('l:p:w:t:he'); if ($opt_h) { die "Usage: mwpush.pl -w <wiki host/path> -t <wiki page name> -l <wiki login> -p <wiki password> [-e] [files ...]\n"; } if (!$opt_l) { die "Provide Wiki login with -l"; } if (!$opt_p) { die "Provide Wiki password with -p"; } if (!$opt_w) { die "Provide Wiki hostname and path with -w"; } if (!$opt_t) { die "Provide Wiki page name with -t"; } my $username=$opt_l; my $password=$opt_p; my $WIKI_PATH=$opt_w; my $WIKI_PAGE=$opt_t; my $onlyifempty=$opt_e; ### Login to wiki # Set up connection data my $browser=LWP::UserAgent->new(); my @ns_headers = ( 'User-Agent' => 'MediaWiki Pusher 0.1 by KeithTyler', #Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20041107 Firefox/1.0', 'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*', 'Accept-Charset' => 'iso-8859-1,*,utf-8', 'Accept-Language' => 'en-US', ); # Hold cookies $browser->cookie_jar( {} ); # Make login request $response=$browser->post("http://".$WIKI_PATH."/w/index.php?title=Special:Userlogin&action=submitlogin", @ns_headers, Content=>[wpName=>$username,wpPassword=>$password,wpRemember=>"1",wpLoginAttempt=>"Log in"]); # After logging in, we should be redirected to another page. # If we aren't, something is wrong. # if($response->code!=302) { print "We weren't able to login. This could have the following causes: * The username ($username) or password may be incorrect. Solution: Re-run script with correct credentials. * The MediaWiki software on the target host has been upgraded. Solution: Go to http://commons.wikimedia.org/wiki/Commons:File_upload_service and get a new version of the upload script. * You are trying to hack this script for other wikis. The wiki you are uploading to has cookie check disabled. Solution: Try setting \$ignore_login_error to 1. Regardless, we will now try to write the output from the server to mwpush.debug.out....\n\n"; open(DEBUG,">mwpush.debug.out") or die "Could not write file.\n"; print DEBUG $response->as_string; print "This seems to have worked. Take a look at the file for further information or send it to moeller AT scireview DOT de if you need help debugging the script.\n"; close(DEBUG); exit 1; } ### Get a wpEditToken # We need to load our target page first in edit mode to capture a # wpEditToken. Without this the submit page will not submit. $response=$browser -> get("http://".$WIKI_PATH."/w/index.php?title=".$WIKI_PAGE."&action=edit", @ns_headers); my $editToken; my $content; $content = $response->as_string; # Get EditToken ($editToken) = ( $content =~ m/value\=\"([0-9a-f\\]*)\" name\=\"wpEditToken\"/ ); ($editTime) = ( $content =~ m/value\=\"([0-9a-f]*)\" name\=\"wpEdittime\"/ ); # Determine page existence state # If we find "selected new" (a CSS class set), it is a new (empty) page # This is preferable to depending on message text (which is changeable) if ($onlyifempty) { if ( ! ($content =~ m/class=\"selected new\"/ )) { print "Existing page is not empty. Aborting due to -e.\n"; exit 1; } } ### Collect input data into string my $INPUT_DATA; # Read data while (<>) { $INPUT_DATA.=$_; } ### Post data to Wiki $response=$browser -> post("http://".$WIKI_PATH."/w/index.php?title=".$WIKI_PAGE."&action=submit", @ns_headers, Content_Type=>'form-data',Content=> [ wpTextbox1 => $INPUT_DATA, wpSummary => "Automated page entry using MWPush.pl", wpSave => "Save page", wpSection => "", wpEdittime => $editTime, wpEditToken => $editToken, ]); ### Evaluate response from Wiki if($response->code!=302 && $response->code!=200) { print "Upload failed! Will try again. Output was:\n"; print $response->as_string; goto uploadfile; } else { print "Uploaded successfully.\n"; } # Evaluate operation print "Everything seems to be OK. Log will be written to mwpush.log.\n"; open(LOG,">mwpush.log") or die "Could not write file.\n"; print LOG $response->as_string;
參見
[編輯]- en:User:KeithTyler/mwpush.pl,程序是從那個頁面拷貝過來的,由於版本的不同,做了一些修正。