Instantly share code, notes, and snippets.

Embed
What would you like to do?
Update user pages (adopter or adoptee of the Wikipedia:Adopt-a-user project) according to their availability according to their activity and edit count.
# Script written by Gabriel Lee Chi Hong
# English Wikipedia user page: https://en.wikipedia.org/wiki/User:Gabrielchihonglee
#
# Written in March, 2018 (Just before my public exam, DSE)
# BRFA: https://en.wikipedia.org/wiki/Wikipedia:Bots/Requests_for_approval/Gabrielchihonglee-Bot_6
#
# Published under the terms of MIT License
# https://choosealicense.com/licenses/mit/
from __future__ import unicode_literals
import pywikibot
import mwparserfromhell
import datetime
import time
import traceback
import re
import json
import requests
site = pywikibot.Site('en','wikipedia','Gabrielchihonglee-Bot')
# Function to check last edit
# Function to get edit count
#
# Remove {{adoptme}} / {{adopting}} for all user whos last edit > 30 days
# If removed, add new section in talk pages
#
# Talk page message draft:
# TEST: Hi there,\nI'm messaging you to let you know that I've removed <<<Template name>>> from your user page because <<<Reason>>>. If you are active again, you are welcome to add the template to your userpage anytime.
def inactive(username):
username = username.split('/')[0]
contribs = site.usercontribs(username,total=1)
for contrib in contribs:
lastedit = datetime.datetime.strptime(contrib.get('timestamp'),'%Y-%m-%dT%H:%M:%SZ')
limit = datetime.datetime.now() - datetime.timedelta(days=30)
inactive = limit > lastedit
return inactive
def not_enough_edits(username):
not_enough_edits = False
username = username.split('/')[0]
response = requests.get('https://xtools.wmflabs.org/api/user/simple_editcount/en.wikipedia/' + username[5:])
edit_count = json.loads(response.text)['liveEditCount']
if edit_count < 500:
not_enough_edits = True
return not_enough_edits
def leave_message(username,template_name,reason):
username = username.split('/')[0]
page = pywikibot.Page(site, username[:4] + ' talk' + username[4:])
print(page.title())
old_code = mwparserfromhell.parse(page.text)
code = mwparserfromhell.parse(page.text)
code.append("\n\n== Changes in your adoption status ==\nHi there,\nI'm messaging you to let you know that I've removed [[Template:" + template_name + "]] from your user page because " + reason + ". If you are active again, you are welcome to add the template to your userpage anytime. -- [[User:Gabrielchihonglee-Bot|Gabrielchihonglee-Bot]] ([[User talk:Gabrielchihonglee-Bot|talk]]) " + datetime.datetime.utcnow().strftime('%H:%M, %-d %B %Y (UTC)'))
pywikibot.showDiff(old_code, code)
page.text = code
summary_text = 'BOT (in trial): Notifying user about change in his/her adoption status'
page.save(summary_text)
########################
##### ADOPTEE PART #####
########################
adoptee_cat = pywikibot.Category(site, title='Category:Wikipedians seeking to be adopted in Adopt-a-user')
adoptee_list = adoptee_cat.members(namespaces=2)
adoptme_template = re.compile(r'(?i)Adopt( )?me')
for adoptee in adoptee_list:
removed = False
username = adoptee.title()
print(username)
if inactive(username) == True:
page = pywikibot.Page(site, username)
old_code = mwparserfromhell.parse(page.text)
code = mwparserfromhell.parse(page.text)
for template in code.filter_templates(recursive=False):
template_name = template.name.strip()
if adoptme_template.match(template_name):
code.remove(template)
pywikibot.showDiff(old_code, code)
page.text = code
summary_text = 'BOT (in trial): Remove [[Template:Adopt me]] from inactive users (no edits in 30 days)'
page.save(summary_text)
removed = True
break
if removed == True: leave_message(username,template_name,'you have been inactive for at least 30 days')
########################
##### ADOPTOR PART #####
########################
adoptor_cat = pywikibot.Category(site, title='Category:Wikipedians seeking to adopt in Adopt-a-user')
adoptor_list = adoptor_cat.members(namespaces=2)
adopting_template = re.compile(r'(?i)Adopting')
for adoptor in adoptor_list:
removed = False
username = adoptor.title()
print(username)
if inactive(username) == True or not_enough_edits(username) == True:
page = pywikibot.Page(site, username)
old_code = mwparserfromhell.parse(page.text)
code = mwparserfromhell.parse(page.text)
for template in code.filter_templates(recursive=False):
template_name = template.name.strip()
if adopting_template.match(template_name):
code.remove(template)
pywikibot.showDiff(old_code, code)
page.text = code
summary_text = 'BOT (in trial): Remove [[Template:Adopting]] from inactive users (no edits in 30 days / edit count less than 500)'
page.save(summary_text)
removed = True
break
if removed == True:
inactive_text = ''
not_enough_edits_text = ''
if inactive(username) == True:
inactive_text = 'you have been inactive for at least 30 days'
if not_enough_edits(username) == True:
not_enough_edits_text = 'you have less than 500 edit count'
if inactive(username) == True and not_enough_edits(username) == True:
reason_text = inactive_text + ' and ' + not_enough_edits_text
else:
reason_text = inactive_text + not_enough_edits_text
leave_message(username,template_name,reason_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment