User:PLbot/PLbot 10

#!/usr/bin/python
# -*- coding: UTF-8 -*-
#licensed under CC-Zero: https://creativecommons.org/publicdomain/zero/1.0

import pywikibot
from pywikibot.data import api

def merge(fromId,toId):
	params1 = {
		'action': 'wbmergeitems',
		'fromid': fromId,
		'toid': toId,
		'ignoreconflicts': 'label|description'
	}
	req1 = api.Request(**params1)
	data1 = req1.submit()
	if data1['success']==1:
		redirect(fromId,toId)

def redirect(fromId,toId):
	#clear item
	params2 = {
		'action': 'wbeditentity',
		'id': fromId,
		'clear': 1,
		'data':'{}',
		'summary': 'clearing item to prepare for redirect'
	}
	req2 = api.Request(**params2)
	data2 = req2.submit()
	if data2['success']==1:
		#create redirect
		params3 = {
			'action': 'wbcreateredirect',
			'from': fromId,
			'to': toId
		}
		req3 = api.Request(**params3)
		data3 = req3.submit()

def main():
	f1 = open('candidates.dat','r') #Format: bigQ1|smallQ1\nbigQ2|smallQ2\n...
	for line in f1:
		m = line.strip().split('|')
		merge(m[0],m[1])
	f1.close()

if __name__ == "__main__":
	main()