Back to WordPress.com
2009/07/27
호스팅 만료 한 달을 앞두고, 다시 wordpress.com 으로 전격 이사를 왔다.
국내의 정치적 핍박을 참지 못하고 사이버 망명길에 오르기엔, 여긴 그저 조회수 낮은 힘없는 일개 프로그래머의 해우소일 뿐이지만, 그래도 최근 MB족들의 하는 꼬라지를 보면 언제 호스팅이 날아가고 데이터가 악의 손아귀에 들어갈 지 모를 터다.
어쨌거나 설치형 워드프레스(이하 WP)에서 호스팅형으로 돌아갈 때 가장 걸리는 것은 바로 첨부 파일이다. 비록 WP가 XML 기반의 Export/Import 를 지원하긴 하지만 첨부 파일까지 자동으로 옮겨주지는 않기 때문이다. 그래서 처음 생각한 건 각종 이미지들을 구글의 피카사웹으로 올린 후, 그 URL을 찾아서 XML파일을 교체한다는 아이디어였다.
일단 첨부파일들을 모두 다운받아서 피카사 업로더를 이용해서 올린 후, 구글 피카사웹 API 샘플을 이용하니 손쉽게 URL을 알아낼 수 있었다. 주의할 점이라면 한글로된 앨범 이름을 위해서 유니코드를 써야 한다는 것 정도가 샘플과의 유일한 차이였다.
def get_picasa_urls(email,password,wp_album):
gd_client = gdata.photos.service.PhotosService()
gd_client.email = email
gd_client.password = password
gd_client.source = 'wordpress_attachment_exporter'
gd_client.ProgrammaticLogin()
print 'picasa web connected'
dic = {}
albums = gd_client.GetUserFeed(user='default')
print len(albums.entry), 'albums found'
for album in albums.entry:
if album.title.text != wp_album:
print 'skipping album', unicode(album.title.text,'utf-8')
break
print unicode(album.title.text), 'album found'
#print 'title: %s, number of photos: %s, id: %s' % (album.title.text,album.numphotos.text, album.gphoto_id.text)
photos = gd_client.GetFeed('/data/feed/api/user/default/albumid/%s?kind=photo' % (album.gphoto_id.text))
for photo in photos.entry:
#print ' Photo:', photo.title.text, ' id:', photo.gphoto_id.text, ' url:', photo.content.src
dic[unicode(photo.title.text,'utf-8')] = unicode(photo.content.src,'utf-8')
print len(dic), 'images in picasa'
return dic
그다음 한 일은 내보내기한 XML 파일의 기존 URL을 위에서 찾아낸 새 URL로 교체하는 것인데, 정규식을 활용해보기로 했다. 문제는 이렇게 했는데도 실제로 미리보기가 안된다는 것이었는데, 아마도 flickr나 피카사 모두 직접 링크를 지원하지 않는 모양이었다. 그래서 어쩔 수 없이 모든 첨부파일을 손으로 하나씩 WP.com 으로 업로드한 후 그 URL을 적용하기로 했다.
아래는 최종 버전.
import re
import codecs
# configuration
wp_export_xml = '/Volumes/data/Downloads/wordpress.2009-07-25.xml'
attachment_ext = 'jpg|JPG|gif|GIF|png|PNG|bmp|BMP|doc|docx'
attachment_url_pattern = 'http://reiot[^"\'<>]+?\.(%s)'%attachment_ext
attachment_filename_pattern = re.compile('[^/=]+?\.(%s)'%attachment_ext)
wpcom_prefix = 'http://boxcatstudio.files.wordpress.com/2009/07/'
attachments = {}
# faster than open().read().encode('utf-8')
lines = codecs.open(wp_export_xml,"r","utf-8").read()
itr = re.compile(attachment_url_pattern).finditer(lines)
for m in itr:
orig_url = m.group()
new_url = wpcom_prefix + attachment_filename_pattern.search(orig_url).group()
attachments[orig_url] = new_url
print len(attachments), 'attachments found'
i = 1
for orig_url in attachments.keys():
new_url = attachments[orig_url]
print '[%d/%d] %s => %s'%(i,len(attachments),orig_url,new_url)
lines = lines.replace(orig_url,new_url)
i = i + 1
out_xml = codecs.open(wp_export_xml+".converted.xml","w","utf-8")
out_xml.write(lines)
out_xml.close()
정규식은 항상 볼 때마다 새로운 느낌이 들어서, 꽤 시간이 많이 걸렸다. 또 큰 파일에서 유니코드로 빠르게 읽으려면 codecs 모듈이 좋댄다.
Entry Filed under: programming. 태그: export, picasa, wordpress.
5 Comments Add your own
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed

1.
manToman | 2009/07/29 at 7:57 오전
레욧님이 고냥이를 매우 좋아하는군요~ .
길잃은 길냥이까지 주어다 기르세요???
2.
레이옷 | 2009/07/29 at 1:48 오후
원래는 개를 더 좋아하는데, 요즘은 고양이와 인연이 더 많은 것 같습니다. 저 오형제들도 불쌍해서 업어 오려고 했는데, 이미 집에 세 마리나 있어서.. 거의 포기네요. OTL
3.
Nod | 2009/07/29 at 11:13 오후
WordPress.com으로 Import시키면 첨부파일을 링크 상태로 가져오는군요.
몰랐습니다.
컴퓨터 언어는 모르겠지만,
다음에 혹 필요할지도 몰라 북마크하고 갑니다.
행복한 하루 되세요. ^^
4.
mikasfuncash | 2009/09/09 at 5:01 오전
우연히 검색하다가 리플을 쓰게 됩니다 ^^
아래 코드는 파이썬으로 보이는데요, 혹시 사용하시고 있는 IDE가 뭔지 알려주실수 있으신지요?
지금 PyScripter라는 IDE로 혼자서 개발하고 있는데, 유니코드 관련해서 자꾸 오류를 내더라고요. 뷰티플스프 이런것도 에러가 나고요;;
그래서 작정하고 갈아탈려는데 적당한게 보이지않는 차에 이렇게 발견하고 연락드립니다.
5.
레이옷 | 2009/09/17 at 11:39 오전
답변이 많이 늦었습니다. 윈도우에서는 vim 또는 notepad++ 을 쓰고, 맥에서는 textmate 를 쓰고 있습니다. ^^;;;