|
| 1 | +import pywikibot |
| 2 | +import requests |
| 3 | +from datetime import datetime |
| 4 | +from collections import Counter |
| 5 | + |
| 6 | +BASE_URL = "https://en.wikipedia.org/w/api.php" |
| 7 | +timestamp_counts = Counter() |
| 8 | + |
| 9 | +def get_timestamps_from_category(category_name): |
| 10 | + PARAMS = { |
| 11 | + "action": "query", |
| 12 | + "format": "json", |
| 13 | + "list": "categorymembers", |
| 14 | + "cmtitle": category_name, |
| 15 | + "cmprop": "ids|title|timestamp", |
| 16 | + "cmnamespace": "3", |
| 17 | + "cmsort": "timestamp", |
| 18 | + "cmdir": "newer", |
| 19 | + "cmlimit": "500" # Fetch up to 500 results; adjust as needed |
| 20 | + } |
| 21 | + |
| 22 | + while True: |
| 23 | + response = requests.get(BASE_URL, params=PARAMS) |
| 24 | + data = response.json() |
| 25 | + |
| 26 | + members = data.get('query', {}).get('categorymembers', []) |
| 27 | + |
| 28 | + for member in members: |
| 29 | + timestamp = member['timestamp'] |
| 30 | + formatted_timestamp = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ').strftime('%B %Y') |
| 31 | + timestamp_counts[formatted_timestamp] += 1 |
| 32 | + print(member['title'], formatted_timestamp) |
| 33 | + |
| 34 | + # If 'continue' field exists in the response, set the 'cmcontinue' parameter for the next request |
| 35 | + if 'continue' in data and 'cmcontinue' in data['continue']: |
| 36 | + PARAMS['cmcontinue'] = data['continue']['cmcontinue'] |
| 37 | + else: |
| 38 | + break # If there's no 'continue' field, break the loop |
| 39 | + |
| 40 | +def display_counts(): |
| 41 | + for month_year, count in timestamp_counts.items(): |
| 42 | + print(f"{month_year}: {count} occurrences") |
| 43 | +# Fetch timestamps for members of the specified category |
| 44 | +get_timestamps_from_category("Category:Wikipedia usernames with possible policy issues") |
| 45 | +display_counts() |
0 commit comments