User:Oliver3669/statusbot

use strict; # 'strict' insists that all variables be declared use Perlwikipedia; # Call the Perlwikipedia module. my $editor=Perlwikipedia->new('Chris G Bot 3'); my $loop = 0; while($loop != 1){ #If bot is enabled... my $enabled = checkEnabled; print "Checking if bot is enabled.\n"; if ($enabled eq 't') { print "Bot is enabled will now run.\n"; my $command = 'perl StatusBotRun.pl'; system($command); }   else { print "Bot is disabled.\n"; }    my $command = 'perl StatusBotRun.pl'; system($command); #Sleep for 3 minutes print "Sleeping for 3 minutes.\n"; sleep 180; } sub checkEnabled { #In this sub the bot checks if it is enabled my $page = 'User:Chris G Bot 3/enabled'; print "\tGetting text from $page\n"; my $text=$editor->get_text($page) or die "ERROR getting text from $page: $!"; print "\tText retrevied\n"; if ($text =~ m/enabled/i) {       print "Bot is enabled.\n"; return 't'; }    else{ print "Bot is disabled.\n"; return 'f'; } }
 * 1) !/usr/bin/perl
 * 1) Loop forever (1 will never be equal to 0)

Main Script:

use strict; # 'strict' insists that all variables be declared use Perlwikipedia; # Call the Perlwikipedia module. use LWP::Simple; use LWP::UserAgent; use HTTP::Request; use HTTP::Request::Common qw(GET); open (LOG, ">>logfile.txt") or die "ERROR opening logfile : $!"; open (ERROR, ">>errorlogfile.txt") or die "ERROR opening logfile : $!"; logs("Creating Perlwikipedia object.\n"); my $editor=Perlwikipedia->new('Chris G Bot 3'); logs("Done.\n"); login; my $cat = 'Category:Wikipedians who use StatusBot'; logs("Listing users in $cat.\n"); my @users = getCat($cat); my $i = 0; for($i .. $#users){ logs("Running check on : $users[$i]\n"); checkUser($users[$i]); logs("Check complated\n"); $i = $i + 1; } sub login { #In the sub the bot logs in to wikipedia #Dim the username and password my $username = 'Chris G Bot 3'; my $password = '***'; #Login logs("Logging in as $username.\n"); $editor->login($username, $password); logs("Done, bot is now logged in as $username.\n"); } sub checkUser { #This sub checks if a users status needs updating and updates if needed #Get users name from the args my $name = $_[0]; #Check what the status should be   my $status; my @userPrefs = getUserPrefs($name); logs("\tGetting the current time and the time of $name 's last edit.\n"); my @lastEdit = getLastEdit($name); my @currentTime = getCurrentTime; logs("\tTimes retreved.\n"); logs("\t\tCurrent date is : $currentTime[1] and current time is : $currentTime[0].\n"); logs("\t\tUsers last edit was at : $lastEdit[0] on : $lastEdit[1].\n"); #If the edit was made today... if ($currentTime[1] == $lastEdit[1]) { #... Then check how long ago that was logs("\tLast edit was made today, checking if it was made in the last 15 minutes.\n"); #If the edit was less than the users set offline time... if ($currentTime[0] - $lastEdit[0] < 15) { #...then set the status to 'online' logs("\tEdit was made in the last 15 minutes setting status to online\n"); $status = $userPrefs[0]; }       #Else... else { #... set the status to 'offline' logs("\tEdit was not made is the last 15 minutes setting status to offline\n"); $status = $userPrefs[1]; }   }    else { logs("\tLast edit was not made today setting status to offline.\n"); $status = $userPrefs[1]; }   #Check if the status should be updated logs("Checking if user's current status is correct or if it should be updated.\n"); my $page = 'User:StatusBot/Status/'. $name; logs("\tGetting text from $page.\n"); my $text=$editor->get_text($page) or die "ERROR getting text from $page: $!"; logs("\tText retreved.\n"); #If the status needs updating... logs("\tChecking if user's current status is correct or if it needs updating.\n"); if ($text ne $status) { #...then update it       logs("\tUser's current status is incorrect and will be updated.\n"); updateStatus($name,$status,$lastEdit[0],$lastEdit[1], $userPrefs[1]); }   #else else {       #... leave it alone logs("\tUser's current status is correct and does not need to be updated.\n"); } } sub updateStatus { #This sub updates the status of a user accorting to the args logs("Updating user's status.\n"); #Dim the args my $name = $_[0]; my $status = $_[1]; my @lastEdit = ($_[2], $_[3]); my $offline = $_[4]; my $res=0; #Edit the page my $pagename = 'User:StatusBot/Status/'. $name; my $edit_summary = 'Updating users status to : '. $status. ' (BOT EDIT)'; my $is_minor = 1; #make the edit minor logs("\tUpdating $pagename.\n"); my $res=$editor->edit($pagename,$status,$edit_summary,$is_minor) or print ERROR "Error editing $pagename : $res\n"; logs("\tDone.\n"); #Now Sleep for 1 sec sleep 1; #Reformat the date and time stamp my @lastEdit = getLastEdit2($name); my $lastEdit = "$lastEdit[1] $lastEdit[0] (UTC)"; if ($status eq $offline) { #Edit the page my $pagename = 'User:StatusBot/Status/'. $name. '/LastEdit'; my $edit_summary = "Updating user's last edit (BOT EDIT)"; my $is_minor = 1; #make the edit minor logs("\tUpdating $pagename.\n"); my $res=$editor->edit($pagename,$lastEdit,$edit_summary,$is_minor) or print ERROR "Error editing $pagename : $res\n"; logs("\tDone.\n"); sleep 1; } my $offline = ''; } sub getLastEdit { #This sub gets the user's last edit from their contribs and returns #it's date and time stamp as an array in which the first value is the #time in 24 hour formatting (eg. 1900 would = 7 pm) and the second value #is the date in ddmmyy formatting (eg. 209007 = 20/09/07) #Dim the args my $name = $_[0]; my $query = "http://en.wikipedia.org/w/api.php?action=query&list=usercontribs&ucuser=$name&uclimit=1&format=xml"; logs("Getting $name 's last edit.\n"); #Query the api for the users last edit logs("\tQuerying the api for $name 's last edit.\n"); my $xml = get_api($query); logs("\tDone.\n"); #Sleep for 5 sec sleep 1; #Clean up the xml logs("\tCleaning up the XML.\n"); my @temp = split(m/timestamp="/, $xml);   my @temp = split(m/"/, $temp[1]); my @temp = split(m/T/, $temp[0]); my $date = $temp[0]; my $time = $temp[1]; my @temp = split(/[-]/,$date); $date = $temp[2].$temp[1].$temp[0]; my @temp = split(/[:]/,$time); $time = $temp[0].$temp[1]; logs("\tXML cleaned.\n"); logs("Done.\n"); #Return the date and time in an array my @Return; $Return[0] = $time; $Return[1] = $date; return @Return; } sub getLastEdit2 { #This sub gets the user's last edit from their contribs and returns #it's date and time stamp as an array in which the first value is the #time in 24 hour formatting (eg. 1900 would = 7 pm) and the second value #is the date in ddmmyy formatting (eg. 209007 = 20/09/07) #Dim the args my $name = $_[0]; my $query = "http://en.wikipedia.org/w/api.php?action=query&list=usercontribs&ucuser=$name&uclimit=1&format=xml"; logs("Getting $name 's last edit.\n"); #Query the api for the users last edit logs("\tQuerying the api for $name 's last edit.\n"); my $xml = get_api($query); logs("\tDone.\n"); #Sleep for 5 sec sleep 1; #Clean up the xml logs("\tCleaning up the XML.\n"); my @temp = split(m/timestamp="/, $xml);   my @temp = split(m/"/, $temp[1]); my @temp = split(m/T/, $temp[0]); my $date = $temp[0]; my $time = $temp[1]; my @temp = split(/[:]/,$time); $time = "$temp[0]:$temp[1]"; logs("\tXML cleaned.\n"); logs("Done.\n"); #Return the date and time in an array my @Return; $Return[0] = $time; $Return[1] = $date; return @Return; } sub get_api { my $article = $_[0]; $article =~ s/\s/_/g; my $article_url = $article; my $browser = LWP::UserAgent->new; $browser->timeout(60); my $request = HTTP::Request->new(GET => $article_url); my $response = $browser->request($request); if ($response->is_error) {printf "%s\n", $response->status_line;} my $contents = $response->content; sleep 1; # don't hammer the server! One read request every 1 second. return $contents; } sub getCat { my $cat = $_[0]; my $xml = get_api("http://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmlimit=500&cmcategory=Wikipedians_who_use_StatusBot&format=xml"); my @temp = split(m# #, $xml); my @temp = split(m# #, $temp[1]); my @temp = split(m#"#, $temp[0]);    my @users;    my $i = 0;    for ($i .. $#temp) {        if ($temp[$i] =~ m/User:/i || m/User talk:/i) {            $users[$#users+1] = $temp[$i];            $users[$#users] =~ s/User://i; $users[$#users] =~ s/User talk://i; #Remove User: or User talk:            my @temp = split(m#/#,$users[$#users]); #Remove any subpages            $users[$#users] = $temp[0];            print "User number $#users = $users[$#users]\n";        }           $i = $i + 1;      }    return @users;  } sub getCurrentTime {    #This sub gets the current time and date and returns it as an array in     #which the first value is the time in 24 hour formatting (eg. 1900 would    # = 7 pm) and the second value is the date in ddmmyy formatting (eg.     # 209007 = 20/09/07)    my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = gmtime; my $year = 1900 + $yearOffset; my @time; $time[0] = $hour.$minute; my $month = $month + 1; $time[1] = $dayOfMonth.$month.$year; return @time; } sub logs { #This sub outputs the args onto the screen and into the logfile my $output = $_[0]; print $output; print LOG $output; } sub getUserPrefs { #This sub returns the user's prefs from the User:StatusBot/config template(see below) in an array #   #The Array is formated like so: #Value String #0           The ParserFunction name for online status(if none default to online) #1           The ParserFunction name for offline status(if none default to offline) #2           The Length of time without an edit to set to offline status(if none default to 15min) #3           A true or false(t/f) value indicating if there is a other status #4           Name of other status(only set if value 3 == t)    #5            Lenghth of time before setting to other status(only set if value 3 == t)        my @return_array; $return_array[0] = 'online'; $return_array[1] = 'offline'; $return_array[3] = 15; return @return_array;
 * 1) !/usr/bin/perl
 * 1) Subs needing work :
 * 2)   * checkUser - needs to check users logs as well
 * 1) Create a log file
 * 1) Create a log file
 * 1) Create a Perlwikipedia object
 * 1) List all the users in the cat
 * 1) Check all the users in that cat to see if they need updating and update if needed