Jump to content

User:ZackBot/tv cleanup

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

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

INFOBOX_REGEX = /(?=\{\{[Ii]nfobox\s[Tt]elevision)(\{\{(?>[^{}]++|\g<1>)*}})/

CHANNEL_REGEX = /\|(\s*)channel\s*=\s*(.*)\n/
HOST_REGEX    = /\|(\s*)host\s*=\s*(.*)\n/
NAME_REGEX    = /\|(\s*)name\s*=\s*(.*)\n/
STUDIO_REGEX  = /\|(\s*)studio\s*=\s*(.*)\n/


def exactly_one_time(text, param, regex)
  count = text.scan(regex).size
  if count > 1
    puts "- ERROR: '#{param}' appears more than one time on the page."
    return false
  elsif count == 0
    puts "- ERROR: '#{param}' does not apepar on the page"
    return false
  end
  true
end

QUERY_URL = "https://petscan.wmflabs.org/?psid=596033&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

# For testing
# pages = File.open('tv.txt').read
# pages.each_line do |title|
titles.each do |title|
  title.strip!
  puts title
  full_text = client.get_wikitext(title).body

  next unless (exactly_one_time(full_text, "Infobox Television", INFOBOX_REGEX))

  infobox_text = full_text.match(INFOBOX_REGEX)[0]

  infobox_text.gsub!(CHANNEL_REGEX, "|\\1network = \\2\n")
  infobox_text.gsub!(HOST_REGEX,    "|\\1presenter = \\2\n")
  infobox_text.gsub!(NAME_REGEX,    "|\\1show_name = \\2\n")
  infobox_text.gsub!(STUDIO_REGEX,  "|\\1company = \\2\n")

  full_text.gsub!(INFOBOX_REGEX, infobox_text)

  client.edit(title: title, text: full_text, summary: 'Fixing infobox not to use [[:Category:Pages using infobox television with alias parameters|deprecated parameters]]')
  puts "- SUCCESS"
end

puts "DONE"