Created
April 6, 2010 06:12
-
-
Save jeffrydegrande/357275 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'nokogiri' | |
module Wordpress | |
class Post | |
attr_reader :title, :post_date, :body, :description, :tags, :author | |
WP_NS = { | |
'wp' => 'http://wordpress.org/export/1.0/', | |
'content' => 'http://purl.org/rss/1.0/modules/content/', | |
'dc' => 'http://purl.org/dc/elements/1.1/' | |
} | |
def initialize(node) | |
@node = node | |
end | |
def title | |
@title ||= @node.xpath('title').first.content | |
end | |
def author | |
@author ||= @node.xpath('dc:creator', WP_NS).first.content | |
end | |
def post_date | |
@post_date ||= DateTime.parse(@node.xpath('wp:post_date', WP_NS).first.content) | |
end | |
def body | |
@body ||= @node.xpath('content:encoded').first.content | |
end | |
def description | |
@description ||= postmeta['description'] | |
end | |
def tags | |
@tags ||= postmeta['keywords'] | |
end | |
private | |
def postmeta | |
@postmeta ||= begin | |
meta_data = {} | |
@node.xpath('wp:postmeta').each do |pm| | |
key = pm.children[1].content | |
case key | |
when 'keywords' | |
meta_data['keywords'] = pm.children[3].content.split(',').map {|e| e.strip} | |
when 'description' | |
meta_data['description'] = pm.children[3].content | |
end | |
end | |
meta_data | |
end | |
end | |
end | |
def self.import(path, options={}, &block) | |
return if not File.exists?(path) | |
doc = Nokogiri::XML(File.read(path)) | |
doc.xpath('//item').each do |post| | |
status = post.xpath('wp:status', Post::WP_NS).first.content | |
next if status != 'publish' | |
yield Post.new(post) | |
#content.scan(/\<img.+?src="(.+?)"/m).collect {|x| x.first}.each do |image| | |
#path = File.basename(image).gsub(/\?.*$/, "") | |
#download_path = File.join(images_path, path) | |
#if ! File.exists? download_path | |
#File.open(download_path, "w") do |f| | |
#f.write(open(image).read) | |
#end | |
#end | |
#end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment