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)

dewikinews

  • 17:10, 09 January 2019 Itu unblocked (sorry...)
  • 17:08, 09 January 2019 Itu blocked with an expiration time of indefinite (nocreate, noemail, nousertalk) ()

dewikisource

  • 16:40, 21 August 2019 A. Wagner unblocked (sorry, global Sysop)
  • 16:30, 21 August 2019 A. Wagner reblocked with an expiration time of indefinite (nocreate) (Einstellen unsinniger Seiten)
  • 14:15, 21 August 2019 Koffeeinist blocked with an expiration time of 3 days, 0:00:00 (nocreate) (Einstellen unsinniger Seiten)

enwiki

  • 11:44, 09 February 2017 JamesBWatson unblocked (Wrong account blocked by mistake.)
  • 11:43, 09 February 2017 JamesBWatson blocked with an expiration time of indefinite (nocreate) (Abusing [[WP:Sock puppetry|multiple accounts]]: [[User:OfficialPankajPatidar]])

frwikisource

  • 15:22, 09 October 2018 Zyephyrus unblocked (Erreur de ma part.)
  • 13:00, 09 October 2018 Zyephyrus blocked with an expiration time of indefinite (nocreate) (spam)

huwikiquote

  • 08:17, 30 April 2019 OsvátA unblocked (Tévedés)
  • 05:48, 30 April 2019 OsvátA blocked with an expiration time of 14 days, 0:00:00 (nocreate) (Vandál)

idwikisource

  • 12:37, 16 November 2018 علاء unblocked (?)
  • 22:06, 14 November 2018 Billinghurst blocked with an expiration time of indefinite (nocreate) ()

nlwiktionary

  • 19:38, 07 August 2019 Cadfaell unblocked (mistake)
  • 15:10, 07 August 2019 Cadfaell blocked with an expiration time of indefinite (nocreate, noemail, nousertalk) (Spam)

tawiktionary

  • 18:42, 03 July 2018 Balajijagadesh unblocked (MISTAKE)
  • 16:04, 02 July 2018 Balajijagadesh blocked with an expiration time of indefinite (nocreate, noemail) ()

testwiki

  • 20:38, 20 August 2019 Tks4Fish blocked with an expiration time of 0:00:01 (nocreate, noemail, nousertalk) (Test)

Licensing

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.

To fork:

  1. Open the raw page and save it to your computer
  2. Go to your PAWS control panel and sign in using OAUTH
  3. Click Upload and upload the file from step 1
  4. To run, open the notebook and click Cell > Run All