Created
January 10, 2013 15:58
-
-
Save dummied/4503161 to your computer and use it in GitHub Desktop.
Hacked together metaweblog api to generate posts in nanoc, circa 2009. Only bit missing was generating/uploading the relevant content. Could have also looked at reading/writing directly to the text files, but a small sql db didn't seem like a big burden at the time.
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
# Erik Kastner 2008-02-16 small blog engine with XMLRPC, hAtom and S3 upload (through xlmrpc) support | |
require 'rubygems' | |
require 'sinatra' | |
require 'xmlrpc/marshal' | |
require 'active_record' | |
require 'aws/s3' | |
require 'stringex' | |
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "sin.db") | |
NANOC_DIR = "/Users/cvannoy/personal/dummied/content/blog" | |
begin | |
ActiveRecord::Schema.define do | |
create_table :posts do |t| | |
t.string :title | |
t.string :author | |
t.text :description | |
t.string :tags | |
t.timestamps | |
end | |
end | |
rescue ActiveRecord::StatementInvalid | |
end | |
class Post < ActiveRecord::Base | |
def permalink; "#{to_param}"; end | |
def full_permalink; "http://www.dummied.org/#{created_at.year}/#{created_at.month}/#{created_at.day}/#{permalink}"; end | |
def to_metaweblog | |
{ | |
:dateCreated => created_at, | |
:userid => 1, | |
:postid => id, | |
:description => description, | |
:title => title, | |
:link => "#{full_permalink}", | |
:permaLink => "#{full_permalink}", | |
:categories => ["General"], | |
:date_created_gmt => created_at.getgm, | |
:mt_keywords => tags.split(",").collect{|t| t.strip} | |
} | |
end | |
def to_param | |
title.to_url | |
end | |
end | |
# metaweblog api handler | |
post '/xml' do | |
xml = @request.body | |
raise "Nothing supplied" if xml.blank? | |
call = XMLRPC::Marshal.load_call(xml) | |
method = call[0].gsub(/metaWeblog\.(.*)/, '\1').gsub(/([A-Z])/, '_\1').downcase | |
header 'Content-Type' => 'text/xml' | |
send(method, call) | |
end | |
def get_post(xmlrpc_call) | |
begin | |
post = Post.find(xmlrpc_call[1][0]) | |
rescue ActiveRecord::RecordNotFound | |
post = Post.find(xmlrpc_call[1][0].gsub(/^.*posts\/(\d+)[^\d].*$/, '\1')) | |
end | |
XMLRPC::Marshal.dump_response(post.to_metaweblog) | |
end | |
def get_recent_posts(xmlrpc_call) | |
posts = Post.find(:all, :limit => 10, :order => "created_at DESC") | |
XMLRPC::Marshal.dump_response(posts.map{|p| p.to_metaweblog}) | |
end | |
def new_post(xmlrpc_call) | |
data = xmlrpc_call[1] | |
# blog_id = data[0]; user = data[1]; pass = data[2] | |
post_data = data[3] | |
post = Post.create(:author => data[1], :title => post_data["title"], :description => post_data["description"], :tags => post_data['mt_keywords']) | |
entry = "#{post.description}" | |
metadata = {"filters_pre" => "redcloth", "created_at" => post.created_at, "title" => post.title, "author" => post.author, "tags" => post_data['mt_keywords'].split(",").collect{|u| u.strip}, "kind" => "article", "layout" => "article"}.to_yaml | |
destination_dir = NANOC_DIR + "/#{post.created_at.year}/#{post.created_at.month}/#{post.created_at.day}/#{post.to_param}" | |
FileUtils.mkdir_p destination_dir | |
File.open(destination_dir + "/meta.yaml", "w"){|f| f.write metadata} | |
File.open(destination_dir + "/index.html", "w"){|f| f.write entry} | |
XMLRPC::Marshal.dump_response(post.to_metaweblog) | |
end | |
def edit_post(xmlrpc_call) | |
data = xmlrpc_call[1] | |
post = Post.find(data[0]) | |
# user = data[1]; pass = data[2] | |
post_data = data[3] | |
post.update_attributes!(:title => post_data["title"], :description => post_data["description"]) | |
entry = "#{post.description}" | |
metadata = {"created_at" => post.created_at, "title" => post.title, "author" => post.author, "tags" => post_data['mt_keywords'].split(",").collect{|u| u.strip}, "kind" => "article", "layout" => "article"}.to_yaml | |
destination_dir = NANOC_DIR + "/#{post.created_at.year}/#{post.created_at.month}/#{post.created_at.day}/#{post.to_param}" | |
FileUtils.mkdir_p destination_dir | |
File.open(destination_dir + "/meta.yaml", "w"){|f| f.write metadata} | |
File.open(destination_dir + "/index.html", "w"){|f| f.write entry} | |
XMLRPC::Marshal.dump_response(post.to_metaweblog) | |
end | |
def get_post_categories(xmlrpc_call) | |
XMLRPC::Marshal.dump_response([]) | |
end | |
def get_category_list(xmlrpc_call) | |
#res = [{ :categoryId => 1,:parentId => 0,:description => "General",:categoryName => "General",:htmlUrl => "http://test.com/categories/1",:rssUrl => "http://test.com/categories/1/feed"}] | |
XMLRPC::Marshal.dump_response([]) | |
end | |
def supported_text_filters(xmlrpc_call) | |
XMLRPC::Marshal.dump_response([]) | |
end | |
def get_categories(xmlrpc_call) | |
#res = [{ :categoryId => 1,:parentId => 0,:description => "General",:categoryName => "General",:htmlUrl => "http://test.com/categories/1",:rssUrl => "http://test.com/categories/1/feed"}] | |
XMLRPC::Marshal.dump_response(res) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment