Class: BuntoImport::Importers::WordpressDotCom
- Inherits:
-
BuntoImport::Importer
- Object
- BuntoImport::Importer
- BuntoImport::Importers::WordpressDotCom
- Defined in:
- lib/bunto-import/importers/wordpressdotcom.rb
Defined Under Namespace
Classes: Item
Class Method Summary collapse
-
.download_images(title, post_hpricot, assets_folder) ⇒ Object
Will modify post DOM tree.
- .process(options) ⇒ Object
- .require_deps ⇒ Object
- .sluggify(title) ⇒ Object
- .specify_options(c) ⇒ Object
Methods inherited from BuntoImport::Importer
inherited, run, stringify_keys, subclasses
Class Method Details
.download_images(title, post_hpricot, assets_folder) ⇒ Object
Will modify post DOM tree
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/bunto-import/importers/wordpressdotcom.rb', line 25 def self.download_images(title, post_hpricot, assets_folder) images = (post_hpricot/"img") if images.length == 0 return end puts "Downloading images for " + title images.each do |i| uri = i["src"] i["src"] = "{{ site.baseurl }}/%s/%s" % [assets_folder, File.basename(uri)] dst = File.join(assets_folder, File.basename(uri)) puts " " + uri if File.exist?(dst) puts " Already in cache. Clean assets folder if you want a redownload." next end begin open(uri, allow_redirections: :safe) {|f| File.open(dst, "wb") do |out| out.puts f.read end } puts " OK!" rescue => e puts " Error: #{e.message}" puts e.backtrace.join("\n") end end end |
.process(options) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/bunto-import/importers/wordpressdotcom.rb', line 124 def self.process() source = .fetch('source', "wordpress.xml") fetch = !.fetch('no_fetch_images', false) assets_folder = .fetch('assets_folder', 'assets') FileUtils.mkdir_p(assets_folder) import_count = Hash.new(0) doc = Hpricot::XML(File.read(source)) # Fetch authors data from header = Hash[ (doc/:channel/'wp:author').map do || [.at("wp:author_login").inner_text.strip, { "login" => .at("wp:author_login").inner_text.strip, "email" => .at("wp:author_email").inner_text, "display_name" => .at("wp:author_display_name").inner_text, "first_name" => .at("wp:author_first_name").inner_text, "last_name" => .at("wp:author_last_name").inner_text }] end ] rescue {} (doc/:channel/:item).each do |node| item = Item.new(node) categories = node.search('category[@domain="category"]').map(&:inner_text).reject{|c| c == 'Uncategorized'}.uniq = node.search('category[@domain="post_tag"]').map(&:inner_text).uniq = Hash.new node.search("wp:postmeta").each do || key = .at('wp:meta_key').inner_text value = .at('wp:meta_value').inner_text [key] = value end = item.text_for('dc:creator').strip header = { 'layout' => item.post_type, 'title' => item.title, 'date' => item.published_at, 'type' => item.post_type, 'published' => item.published?, 'status' => item.status, 'categories' => categories, 'tags' => , 'meta' => , 'author' => [] } begin content = Hpricot(item.text_for('content:encoded')) header['excerpt'] = item.excerpt if item.excerpt if fetch download_images(item.title, content, assets_folder) end FileUtils.mkdir_p item.directory_name File.open(File.join(item.directory_name, item.file_name), "w") do |f| f.puts header.to_yaml f.puts '---' f.puts Util.wpautop(content.to_html) end rescue => e puts "Couldn't import post!" puts "Title: #{item.title}" puts "Name/Slug: #{item.file_name}\n" puts "Error: #{e.message}" next end import_count[item.post_type] += 1 end import_count.each do |key, value| puts "Imported #{value} #{key}s" end end |
.require_deps ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/bunto-import/importers/wordpressdotcom.rb', line 6 def self.require_deps BuntoImport.require_with_fallback(%w[ rubygems fileutils safe_yaml hpricot time open-uri open_uri_redirections ]) end |
.sluggify(title) ⇒ Object
202 203 204 |
# File 'lib/bunto-import/importers/wordpressdotcom.rb', line 202 def self.sluggify(title) title.gsub(/[^[:alnum:]]+/, '-').downcase end |
.specify_options(c) ⇒ Object
18 19 20 21 22 |
# File 'lib/bunto-import/importers/wordpressdotcom.rb', line 18 def self.(c) c.option 'source', '--source FILE', 'WordPress export XML file (default: "wordpress.xml")' c.option 'no_fetch_images', '--no-fetch-images', 'Do not fetch the images referenced in the posts' c.option 'assets_folder', '--assets_folder FOLDER', 'Folder where assets such as images will be downloaded to (default: assets)' end |