-
Notifications
You must be signed in to change notification settings - Fork 8
/
adopters.py
executable file
·49 lines (42 loc) · 1.54 KB
/
adopters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#! /usr/bin/env python
from __future__ import unicode_literals
import mwclient
import mwparserfromhell
import datetime
from theobot import password
from time import mktime
# CC-BY-SA Theopolisme
def active(user):
"""Returns True if a user is active (i.e., last edit withinin
XX months ago), False if inactive.
"""
contribs = site.usercontributions(user,limit=1)
for contrib in contribs:
timestamp = datetime.datetime.fromtimestamp(mktime(contrib[u'timestamp']))
tdelta = now - timestamp
if tdelta > datetime.timedelta(days=30):
print "{0} hasn't edited for {1}, which is greater than 30 days.".format(user,tdelta)
return False # user has not edited in past 30 days
else:
print "{0} last edited {1}, which is less than 30 days.".format(user,tdelta)
return True
def main():
global site,now
site = mwclient.Site('en.wikipedia.org')
site.login(password.username, password.password)
now = datetime.datetime.now()
page = site.Pages["Wikipedia:Adopt-a-user/Adoptee's Area/Adopters"]
wikicode = mwparserfromhell.parse(page.edit())
for template in wikicode.filter_templates():
if "Wikipedia:Adopt-a-user/Adopter Profile" in template.name:
user = template.get('username').value.strip()
if active(user) == False:
template.add('available','no')
template.add('bot-updated','yes')
elif template.has_param('bot-updated') == True:
template.add('available','yes')
new_contents = unicode(wikicode)
page.save(new_contents,summary="[[WP:BOT|Bot]]: Updating availabilities.")
if __name__ == '__main__':
print "Powered on."
main()