Migrating from Facebook Page to WordPress.

Facebook has decided to close our group for political reasons, I’m not gonna discuss that here because its their site in the end and they can run it in whatever way they want, all we can do is become less dependent on facebook and migrate our stuff from there, from now on, I will think twice about posting any useful content on facebook and I will recommend everyone else to do just the same.

So we needed to pull out all the notes from the page we had, totalling 454 notes, there is no export functionality in facebook only an RSS feed that would give you the last 10 items.

So I found a post that shows how to use the API test tool to query the FB database, the relevant query is

SELECT note_id, title, created_time, content FROM note WHERE uid = 0000000000

and change the uid into the id of the facebook page you want.

I was able to get all the notes in XML format, now in order to get those into wordpress we need to import them, here is a post that shows a generic xml parser for wordpress, I had to change the script a bit to make is understand fb’s xml format.

here is the file xmltowp.php

Update: 20100402
Now we have migrated our photo albums from facebook aswell, its a bit more complicated but hey, it works :)
here are the queries for the FB API tool, first get your albums

SELECT name,aid FROM album WHERE owner = 00000000

next use the album ID to get the photos info for each album alone

SELECT pid, aid, src_big, src_big_height, src_big_width, link, caption, created, modified, object_id FROM photo WHERE aid= '00000000_00000'

now we have an XML file with all the photos info, lets get the URL and get all the files

$grep '<src_big>' album.xml > urls

in VIM do the following substitutes on the urls file

:%s/    <src_big>//g
:%s/<\/src_big>//g

now feed the urls file to wget through the -i option.
I have used the NextGEN gallery module for WordPress, using that I was able to import whole folders, the folders where wget saved all those photos. Now to get the captions of the original photos in WordPress aswell I have written a small ruby script that parses the albums.xml and generates an SQL file that you need to execute against your wordpress DB, here is the code, ugly but works

#!/usr/bin/ruby

require "rexml/document"
include REXML

file = File.new( "album.xml" )
doc = Document.new file
output = ""
doc.elements.each("*/photo") { |element|
  filename = ''
  caption = ''
  XPath.each( element, "src_big") { |src| filename = File.basename(src.text) }
  XPath.each( element, "caption") { |cap| caption = cap.text }
  output << "update wp_ngg_pictures set description = '#{caption}', alttext='#{caption}' where filename='#{filename}';\n"
}
outfile = File.new("update.sql", "w")
outfile << 'SET CHARACTER_SET_CLIENT=utf8;'
outfile << 'SET CHARACTER_SET_CONNECTION=utf8;'
outfile << 'SET CHARACTER_SET_RESULTS=utf8;'
outfile << output

hope this helps someone in need.

Tags: ,

Leave a comment