Forked from danielgomezrico/blogspot_to_jekyll.rb
Last active
December 18, 2015 10:33
-
-
Save Frodo45127/600043b73956ec9e5faf to your computer and use it in GitHub Desktop.
Updated documentation, the content is now converted from html to markdown, fixed problems with files with some characters in their names and in their categories and fixed categories with spaces.
This file contains 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
#!/usr/bin/env ruby | |
# | |
# Convert blogger (blogspot) posts to jekyll posts | |
# | |
# Basic Usage | |
# ----------- | |
# In Windows, go to the folder where this file is and execute: | |
# ruby blogspot_to_jekyll.rb feed_url | |
# | |
# In Linux, go to the folder where this file is and execute: | |
# ./blogspot_to_jekyll.rb feed_url | |
# | |
# where `feed_url` can have the following format: | |
# http://{your_blog_name}.blogspot.com/feeds/posts/default | |
# | |
# The new posts will be created in "where_this_file_is/_posts" | |
# | |
# Requirements | |
# ------------ | |
# | |
# * feedjira: https://github.com/feedjira/feedjira | |
# * reverse_markdown: https://github.com/xijo/reverse_markdown | |
# | |
# Notes | |
# ----- | |
# | |
# * Make sure Blogger shows full output of article in feeds. | |
# * Make sure the feed shows all the posts you want to import. If not, add "?max-results=500" to your feed url. | |
# * If you have more than 500 post to import, check this: | |
# http://too-clever-by-half.blogspot.kr/2011/12/blog-feed-500-post-limit-for-more-than.html | |
# * Commenting on migrated articles will be set to false by default. | |
# * If your title or your categories have some characters like "á", "é", "í",... you have to replace them manually | |
# or your page will have problems to load. | |
# * The following characters will be deleted from the title when importing: / : " , and .. | |
# In case of categories, only the colon will be deleted. | |
require 'rubygems' #if RbConfig['host_os'].start_with? "darwin" | |
require 'feedjira' | |
require 'date' | |
require 'optparse' | |
require 'reverse_markdown' | |
def parse_post_entries(feed, verbose) | |
posts = [] | |
feed.entries.each do |post| | |
obj = Hash.new | |
created_datetime = post.published | |
creation_date = Date.parse(created_datetime.to_s) | |
title = post.title.delete('\/|\:|\"|\,|\.') | |
file_name = creation_date.to_s + "-" + title.split(/ */).join("-").delete('\/|\:|\"|\,|\.') + ".md" | |
content = ReverseMarkdown.convert(post.content, unknown_tags: :drop, github_flavored: true) | |
obj["file_name"] = file_name | |
obj["title"] = title | |
obj["creation_datetime"] = created_datetime | |
obj["updated_datetime"] = post.updated | |
obj["content"] = content | |
obj["categories"] = "[" + post.categories.join(", ").delete('\:') + "]" | |
posts.push(obj) | |
end | |
return posts | |
end | |
def write_posts(posts, verbose) | |
Dir.mkdir("_posts") unless File.directory?("_posts") | |
total = posts.length, i = 1 | |
posts.each do |post| | |
file_name = "_posts/".concat(post["file_name"]) | |
header = %{--- | |
title: #{post["title"]} | |
layout: post | |
date: #{post["creation_datetime"]} | |
updated: #{post["updated_datetime"]} | |
comments: false | |
categories: #{post["categories"]} | |
--- | |
} | |
File.open(file_name, "w+") {|f| | |
f.write(header) | |
f.write(post["content"]) | |
f.close | |
} | |
if verbose | |
puts " [#{i}/#{total[0]}] Written post #{file_name}" | |
i += 1 | |
end | |
end | |
end | |
def main | |
options = {} | |
opt_parser = OptionParser.new do |opt| | |
opt.banner = "Usage: ./blogspot_to_jekyll.rb FEED_URL [OPTIONS]" | |
opt.separator "" | |
opt.separator "Options" | |
opt.on("-v", "--verbose", "Print out all.") do | |
options[:verbose] = true | |
end | |
end | |
opt_parser.parse! | |
if ARGV[0] | |
feed_url = ARGV.first | |
else | |
puts opt_parser | |
exit() | |
end | |
puts "Fetching feed #{feed_url}..." | |
feed = Feedjira::Feed.fetch_and_parse(feed_url) | |
puts "Parsing feed..." | |
posts = parse_post_entries(feed, options[:verbose]) | |
puts "Writing posts to _posts/..." | |
write_posts(posts, options[:verbose]) | |
puts "Done!" | |
end | |
main() |
This file contains 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
#!/bin/bash | |
# | |
# This is an example of how to run it, you need to put the size otherwise it will load just 25 posts and | |
# have a max of 500 posts every time | |
# http://too-clever-by-half.blogspot.kr/2011/12/blog-feed-500-post-limit-for-more-than.html | |
# | |
ruby blogspot_to_jekyll.rb http://<blog>.com/feeds/posts/default?max-results=200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment