User:ZackBot/nfl deprecation cleanup

From Wikipedia, the free encyclopedia
#!/usr/bin/env ruby
# encoding: utf-8

require 'mediawiki_api'
require 'HTTParty'
require 'csv'
require 'open-uri'
require './helper'
require 'fileutils'

INFOBOX_REGEX = /(?=\{\{[Ii]nfobox\sNFL\s(?:player|biography))(\{\{(?>[^{}]++|\g<1>)*}})/

QUERY_URL = "https://petscan.wmflabs.org/?psid=612427&format=json"

Helper.read_env_vars

client = MediawikiApi::Client.new 'https://en.wikipedia.org/w/api.php'
client.log_in ENV['USERNAME'], ENV['PASSWORD']

json = JSON.load(open(QUERY_URL))
titles = json["*"].first["a"]["*"].map{ | page| page["title"].gsub("_"," ")}
puts titles.size

titles.each do |title|
  title.strip!
  puts title

  full_text = client.get_wikitext(title).body

  next if Helper.no_bots?(full_text)
  next unless (Helper.exactly_one_time(full_text, "Infobox NFL", INFOBOX_REGEX))

  infobox_text = full_text.match(INFOBOX_REGEX)[0]

  infobox_text.gsub!(/\|(\s*)currentpositionplain(\s*)=/, '|\1position\2=')
  infobox_text.gsub!(/\|(\s*)currentposition(\s*)=/,      '|\1position\2=')
  infobox_text.gsub!(/\|(\s*)currentteam(\s*)=/,          '|\1current_team\2=')
  infobox_text.gsub!(/\|(\s*)currentnumber(\s*)=/,        '|\1number\2=')
  infobox_text.gsub!(/\|(\s*)heightft(\s*)=/,             '|\1height_ft\2=')
  infobox_text.gsub!(/\|(\s*)heightin(\s*)=/,             '|\1height_in\2=')
  infobox_text.gsub!(/\|(\s*)weight(\s*)=/,               '|\1weight_lbs\2=')
  infobox_text.gsub!(/\|(\s*)weight_lb(\s*)=/,            '|\1weight_lbs\2=')

  full_text.gsub!(INFOBOX_REGEX, infobox_text)

  client.edit(title: title, text: full_text, summary: "Fixing infobox not to use deprecated parameters")
  puts "- SUCCESS"
end

puts "DONE"