11import pywikibot
22import re
33import requests
4-
4+ #import datetime
5+ from datetime import *
6+ from dateutil import relativedelta
57
68class RemoveBase :
79 def __init__ (self , log_name , category , trial = False ):
@@ -50,10 +52,11 @@ def log(self, page):
5052
5153
5254class RemoveBlocked (RemoveBase ):
53- def __init__ (self , log_name , category , target , brfa , trial = False , count = 50 ):
55+ def __init__ (self , log_name , category , target , brfa , span = None , trial = False , count = 50 ):
5456 super (RemoveBlocked , self ).__init__ (log_name , category , trial )
5557 self .target = target
5658 self .brfa = brfa
59+ self .span = span
5760 if trial :
5861 self .count = count
5962
@@ -71,28 +74,28 @@ def run(self):
7174 except AttributeError :
7275 print ("Failed" + page .title ())
7376 continue
74- summary = "Removing [[Category:" + self .cat_name + "]] as user is "
77+ summary = "Removing [[Category:" + self .cat_name + "]] as "
7578 #print(user.username)
7679 try :
7780 if self .isLocked (user .username ):
7881 self .log (page )
7982 self .category_remove (self .target , page )
8083 page .save (
81- summary = summary + "locked." +
84+ summary = summary + "user is locked." +
8285 " ([[Wikipedia:Bots/Requests for approval/" + self .brfa + "|BRFA]])" , minor = True ,
8386 botflag = True , force = True )
84- print ("Saved " + str (page .title ()))
87+ # print("Saved " + str(page.title()))
8588 if self .trial :
8689 counter += 1
8790 print (counter )
8891 elif user .isBlocked ():
8992 self .log (page )
9093 self .category_remove (self .target , page )
9194 page .save (
92- summary = summary + "blocked." +
95+ summary = summary + "user is blocked." +
9396 " ([[Wikipedia:Bots/Requests for approval/" + self .brfa + "|BRFA]])" , minor = True ,
9497 botflag = True , force = True )
95- print ("Saved " + str (page .title ()))
98+ # print("Saved " + str(page.title()))
9699 if self .trial :
97100 counter += 1
98101 print (counter )
@@ -102,13 +105,90 @@ def run(self):
102105 self .log (page )
103106 self .category_remove (self .target , page )
104107 page .save (
105- summary = summary + "blocked." +
108+ summary = summary + "user is blocked." +
106109 " ([[Wikipedia:Bots/Requests for approval/" + self .brfa + "|BRFA]])" , minor = True ,
107110 botflag = True , force = True )
108- print ("Saved " + str (page .title ()))
111+ # print("Saved " + str(page.title()))
109112 if self .trial :
110113 counter += 1
111114 print (counter )
115+ if self .span is not None and type (self .span ) != int :
116+ #print(user.isAnonymous())
117+ span1 = datetime .strptime (self .span , "%Y-%m-%dT%H:%M:%SZ" )
118+ if user .isAnonymous ():
119+ page_date = datetime .strptime (str (page .latest_revision .timestamp ), "%Y-%m-%dT%H:%M:%SZ" )
120+ #print(span1 > dt)
121+ #print(self.calc_difference(page.latest_revision.timestamp))
122+ #sample = {'year':2018,'month':02,'day':01}
123+ #b2 = date(sample[0], sample[1], d2)
124+ if page_date <= span1 :#int(str(page.latest_revision.timestamp)[0:4]) <= self.span:
125+ self .log (page )
126+ self .category_remove (self .target , page )
127+ page .save (
128+ summary = summary + "talk page not edited in " +
129+ self .calc_difference (page .latest_revision .timestamp ) +
130+ " and notice considered stale." + " ([[Wikipedia:Bots/Requests for approval/" +
131+ self .brfa + "|BRFA]])" , minor = True ,
132+ botflag = True , force = True )
133+ else :
134+ #print(user.username)
135+ results = []
136+ if user .last_event is not None :
137+ event_timestamp = user .last_event .timestamp ()
138+ ev_ts = datetime .strptime (str (event_timestamp ), "%Y-%m-%dT%H:%M:%SZ" )
139+ results .append (ev_ts )
140+
141+ if user .last_edit is not None :
142+ user_edit = user .last_edit [2 ]
143+ ed_ts = datetime .strptime (str (user_edit ), "%Y-%m-%dT%H:%M:%SZ" )
144+ results .append (ed_ts )
145+
146+ try :
147+ dcontribs = next (iter (user .deleted_contributions ()))
148+ del_ts = datetime .strptime (str (dcontribs [1 ]['timestamp' ]), "%Y-%m-%dT%H:%M:%SZ" )
149+ results .append (del_ts )
150+ except StopIteration :
151+ pass # No deleted contribs
152+
153+ #Compare last event edit, last edit,
154+ #last deleted edit to figure the most recent
155+ newest_contrib_time = max (results )
156+ #print(newest_contrib_time)
157+
158+ if newest_contrib_time <= span1 :
159+ self .log (page )
160+ self .category_remove (self .target , page )
161+ page .save (
162+ summary = summary + "user has not edited in " +
163+ self .calc_difference (newest_contrib_time ) +
164+ " and notice considered stale." + " ([[Wikipedia:Bots/Requests for approval/" +
165+ self .brfa + "|BRFA]])" , minor = True ,
166+ botflag = True , force = True
167+ )
168+
169+
170+ def calc_difference (self , old_date ):
171+ now = datetime .utcnow ()
172+ diff = relativedelta .relativedelta (now , old_date )
173+ result = ""
174+ if diff .years > 1 :
175+ result += "%s years, " % diff .years
176+ elif diff .years == 1 :
177+ result += "%s year, and " % diff .years
178+ if diff .months > 1 :
179+ result += "%s months, " % diff .months
180+ elif diff .months == 1 :
181+ result += "%s month, and " % diff .months
182+ if diff .days > 1 :
183+ result += "%s days, " % diff .days
184+ elif diff .days == 1 :
185+ result += "%s day, and " % diff .days
186+ if diff .hours > 1 :
187+ result += "%s hours " % diff .hours
188+ elif diff .hours == 1 :
189+ result += "%s hour " % diff .hours
190+
191+ return result # %(diff.years, diff.months, diff.days, diff.hours)
112192
113193
114194class RemoveUnblocked (RemoveBase ):
@@ -138,4 +218,4 @@ def run(self):
138218 summary = "Removing [[Category:" + self .cat_name + "]] as user is unblocked." +
139219 " ([[Wikipedia:Bots/Requests for approval/" + self .brfa + "|BRFA]])" , minor = True ,
140220 botflag = True , force = True )
141- print ("Saved " + str (page .title ()))
221+ # print("Saved " + str(page.title()))
0 commit comments