🇮🇷 Iran Proxy | https://www.wikipedia.org/wiki/User:DimensionalFusion/userscripts/Mass_edit_counter
Jump to content

User:DimensionalFusion/userscripts/Mass edit counter

From Wikipedia, the free encyclopedia

Script designed to take an input JSON file with users, and output that file with edit counts for each user

import json
import requests
import time
from tqdm import tqdm

USERNAME = "User:Example"
INPUT_FILE = "Users.json"
OUTPUT_FILE = "UsersWithEditCounts.json"
LOG_FILE = "manual_review.log" #For cases of API fail
PROJECT = "en.wikipedia"
NAMESPACE = "0"  # Mainspace
START_DATE = "2001-01-01"

# Load input JSON
with open(INPUT_FILE, "r") as infile:
candidates = json.load(infile)

output = []
manual_review = []

# Use tqdm to wrap the iteration with a progress bar
for candidate in tqdm(candidates, desc="Processing candidates"):
username = candidate["candidate"]
end_date = candidate["date"]

# Updated API endpoint
url = f"https://xtools.wmflabs.org/api/user/simple_editcount/{PROJECT}/{username}/{NAMESPACE}/{START_DATE}/{end_date}"

try:
headers = {
"User-Agent": "EditCountCollector/1.0 ({USERNAME})",
"Accept-Encoding": "gzip"
}

response = requests.get(url, headers=headers, timeout=10)
if response.status_code != 200:
raise Exception(f"HTTP {response.status_code}")

data = response.json()

# Check if valid and contains 'live_edit_count'
if isinstance(data, dict) and "live_edit_count" in data:
candidate["edit_count"] = data["live_edit_count"]
output.append(candidate)
else:
manual_review.append(f"{username}: No edit count found in API response. Response: {data}")

except Exception as e:
manual_review.append(f"{username}: API error or renamed? {str(e)}")

time.sleep(1)  # Rate limiting

# Save successful results
with open(OUTPUT_FILE, "w") as outfile:
json.dump(output, outfile, indent=2)

# Save manual review log
with open(LOG_FILE, "w") as logfile:
logfile.write("Manual review needed for the following candidates:\n")
for line in manual_review:
logfile.write(line + "\n")

print(f"Done. Saved results to {OUTPUT_FILE}, and log to {LOG_FILE}.")