Livespace要在明年關閉了,搭建了micolog在google app engine,方便以后自己掙騰。上次用生拙的C#寫個live space到livespace的博客遷移工具,使用metaweblog接口,見?Live Spaces新舊空間遷移方法 。上次使用getRecentPosts函數依次取得最近的一篇,然后存檔后發表后,刪除。這次通過正則表達式分析網頁內容,獲取到postid后,再使有getPost接口獲取文章,再進行發表,而且這次使用python寫成的。
metaweblog的內容不再敘述,其實這個協議寫得真不怎么樣,沒有檢索文章的接口,要讓人硬生生地從網頁中分析出postid來。因此解析postid是這個遷移工具的重要內容。
#獲取www.shnenglu.com樣式的postid列表
def getCppblogId(blog):
url='http://www.shnenglu.com/'+blog['user']+'/default.html?page=1&OnlyTitle=1'
urlfile = urllib.urlopen(url)
html = urlfile.read()
#獲取存檔頁碼數
pattern = re.compile(r'http://www.shnenglu.com/'+blog['user']+'/default.html\?page=(\d+)&OnlyTitle=1')
pages = [1]
pages += pattern.findall(html)
ids=[]
for p in pages:
url= 'http://www.shnenglu.com/'+blog['user']+'/default.html?page='+str(p)+'&OnlyTitle=1'
urlfile = urllib.urlopen(url)
html = urlfile.read()
pattern = re.compile(r'http://www.shnenglu.com/'+blog['user']+'/admin/EditPosts.aspx\?postid=(\d+)')
id = pattern.findall(html)
ids += id
return ids
利用存檔頁面得到總共頁數(或許頁數多了或有問題,未驗證),然后在每頁解析出postid,cppblog較簡單
def getLivespaceId(blog):
ids=[]
url=blog['user']+'.spaces.live.com/blog/'
i=0
while True:
url='http://'+url
urlfile = urllib.urlopen(url)
html = urlfile.read()
#print html
pattern = re.compile(r'entrycns!'+'([a-zA-Z0-9!]*)')
id = pattern.findall(html)
ids += id
pattern = re.compile(blog['user']+'.{1,50}pagedir=Next[^"]*')
urls = pattern.findall(html)
i=i+1
if len(urls) ==0:
break
url = unescape(urls[0])
return ids
live space沒有總共的頁數,只能一直next下去,發現沒有next按鈕了就停止,在每頁再解析出postid來,unescape是自定義函數,目的是將html編碼轉換為像!等符號。
在遷移post時出現未micolog中定義的目錄(category)會出錯,因此遷移工具里如果碰到未定義過的類別,會自動舍棄掉。因此在使用
時需要在micolog里定義原先blog的類別,以致不會出現目錄丟失的現象。此遷移工具在python2.5下完成,只要在源碼中修改中開頭的
srcBlog和dstBlog定義里的用戶名,密碼,webapi即可使用。源碼中還實現BlogXML類,用于存檔為xml格式,但未用于主程序中。
遷移工具源碼