import mwapi
import pywikibot
from IPython import display, utils
import os
target_user = 'Praxidicae'
def get_accounts(target_user):
sys_info = platform.python_version()
user_agent = (f'Global_block_history.ipynb on PAWS '
f'(User:{os.environ["USER"]}) mwapi/{mwapi.__version__} '
f' python/{sys_info}')
session = mwapi.Session('https://meta.wikimedia.org', user_agent)
request = {'action': 'query', 'format': 'json',
'meta': 'globaluserinfo', 'guiuser': target_user,
'guiprop': 'merged'}
api_result = session.get(request)
result_details = api_result['query']['globaluserinfo']['merged']
accounts = []
for entry in result_details:
accounts.append(entry['url'])
return accounts
def list_blocks(target_user, accounts):
logtime = '%H:%M, %d %B %Y'
count_blocks = 0
for account in accounts:
num_entries = 0
try:
site = pywikibot.Site(url=f'{account}/wiki/')
except (pywikibot.SiteDefinitionError, pywikibot.NoUsername):
continue
blocklog = site.logevents(logtype='block', page=f'User:{target_user}')
block_string = ''
for block in blocklog:
num_entries += 1
if num_entries == 1:
count_blocks += 1
display.display_markdown(f'#### {site.dbName()}', raw=True)
if block.duration():
expiry = f'with an expiration time of {block.duration()}'
elif block.action() != 'unblock':
expiry = 'with an expiration time of indefinite'
else:
expiry = ''
if block.flags():
flags = '('
for flag in block.flags():
flags += flag
flags += ', '
flags = flags[0:(len(flags) - 2)]
flags += ')'
else:
flags = ''
block_string += (
f' - {block.timestamp().strftime(logtime)} {block.user()} '
f'{block.action()}ed {expiry} {flags} '
f'({block.comment()})\n')
if block_string:
display.display_markdown(block_string, raw=True)
accounts = get_accounts(target_user)
list_blocks(target_user, accounts)
Copyright 2019 AntiCompositeNumber
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.