Перенос постов из wordpress в livejournal
Автор - http://blog.simonchik.net/2008/03/wordpress-livejournal_6914.html
# -*- coding: utf-8 -*-
#!/usr/bin/env python
#Boa:PyApp:main
import xmlrpclib
import datetime
import time
import xml.dom.minidom
modules ={}
lj_username = "dimychs"
lj_password = "*********"
wp_exportfile = "wordpress.2008-03-26.xml"
wp_datefrom = "01.01.1980" #date format is DD.MM.YYYY
wp_datetill = "01.01.2010"
lj_disablecomments = True
lj_addoriginallink = True
def getNodeValue(node):
value = ""
for textnode in node.childNodes:
value = value + textnode.nodeValue
return value
def getPostData(xmlNode):
postdata = {};
tagline = "";
postdata["ver"] = 1 #this means that all data is sent in unicode
posttext = "";
postdtime = "";
for node in xmlNode.childNodes:
if(node.nodeType != xml.dom.Node.ELEMENT_NODE):
continue;
if(node.tagName == "title"):
value = getNodeValue(node)
postdata["subject"] = value
if(node.tagName == "content:encoded"):
value = getNodeValue(node).replace("", "")
posttext += value
if(node.tagName == "category"):
tag = getNodeValue(node)
if(tagline != ""):
tagline += ", "
tagline += tag;
if(node.tagName == "pubDate"):
postdtime = mytime = getNodeValue(node)
time_format = "%a, %d %b %Y %H:%M:%S +0000"
posttime = time.strptime(mytime, time_format)
if(posttime[0] == 1970):
posttime = time.localtime()
postdata["year"] = posttime[0]
postdata["mon"] = posttime[1]
postdata["day"] = posttime[2]
postdata["hour"] = posttime[3]
postdata["min"] = posttime[4]
if(node.tagName == "wp:status"):
status = getNodeValue(node)
if(status != "publish"):
postdata["security"] = "private"
if(node.tagName == "link"):
value = getNodeValue(node)
if(posttext != ""):
posttext += "\n"
posttext += "This is a crosspost from " + value + "\n\n";
options = {"taglist": tagline, "opt_backdated": True}
if(lj_disablecomments):
options["opt_nocomments"] = True
if(postdata["subject"] == ""):
postdata["subject"] = postdtime
postdata["props"] = options;
postdata["event"] = posttext;
postdata["username"] = lj_username
postdata["password"] = lj_password
return postdata
def main():
timestart = time.strptime(wp_datefrom, "%d.%m.%Y")
timeend = time.strptime(wp_datetill, "%d.%m.%Y")
server = xmlrpclib.ServerProxy("http://www.livejournal.com/interface/xmlrpc")
dom = xml.dom.minidom.parse(wp_exportfile)
i = 1;
for node in dom.documentElement.childNodes:
if(node.nodeType == xml.dom.Node.ELEMENT_NODE):
if(node.tagName == "channel"):
print "Channel found:", node;
for item in node.childNodes:
if(item.nodeType != xml.dom.Node.ELEMENT_NODE):
continue;
if(item.tagName == "item"):
args = getPostData(item)
#checking date
posttime = time.strptime(str(args["day"])+"."+
str(args["mon"])+"."+str(args["year"]),"%d.%m.%Y")
if(posttime >= timestart and posttime < timeend):
response = server.LJ.XMLRPC.postevent(args)
print "Item: ", i, response
i += 1
break
if __name__ == '__main__':
main()
Все параметры задаются вначале файла:
lj_username = "dimychs"
lj_password = "*********"
wp_exportfile = "wordpress.2008-03-26.xml"
wp_datefrom = "01.01.1980" #date format is DD.MM.YYYY
wp_datetill = "01.01.2010"
lj_disablecomments = True
lj_addoriginallink = True
По-моему, их смысл понятен из названий.
Вот так будет выглядеть результат: http://dimychs.livejournal.com/
Комментариев нет:
Отправить комментарий