#!/usr/bin/python

import MySQLdb
import re

links = dict()
content = dict()

def get_data():
	db = MySQLdb.connect(user='my_user', passwd='my_password', host='localhost', db='my_db')

	cursor = db.cursor()
	query = "select id, linkUrl, contentUrl from wp_shashin_photo"

	cursor.execute(query)

	res = cursor.fetchall()


	for (id, linkurl, contenturl) in res:
		links[str(id)] = linkurl
		content[str(id)] = contenturl	

	cursor.close()
	db.close()

get_data()

blogexport = open('oltcuisine.wordpress.2015-06-04-with-gallery-links.xml', 'r')
blogexportfixed =  open('oltcuisine.wordpress.2015-06-04-with-gallery-links-fixed.xml', 'w')
for line in blogexport:
	repline = line
	m = re.search('\[simage=([0-9]+).*?\]', line)
	if not m is None:
		tag = m.group(0)
		id = m.group(1)
		if(id in links and id in content):
			repline = repline.replace(tag, '<p><a href="'+links[id]+'"><img src="'+ content[id] +'"/></a></p>')
	m2 = re.search('\[shashin type="photo" id="([0-9]+)".*?\]', line)
	if not m2 is None:
		tag = m2.group(0)
		id = m2.group(1)
		if(id in links and id in content):
                        repline = repline.replace(tag, '<p><a href="'+links[id]+'"><img src="'+ content[id] +'"/></a></p>')

	blogexportfixed.write(repline)

blogexportfixed.close()
