Skip to content

Instantly share code, notes, and snippets.

@brentsimmons
Created March 6, 2014 20:25
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brentsimmons/9398899 to your computer and use it in GitHub Desktop.
Save brentsimmons/9398899 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -wKU
# Comments added 6 March 2014.
# Implementation of the MetaWeblog API for my personal Ruby static blog generator.
# This is not even nearly idiomatic Ruby. There are all kinds of issues.
# (What's with the method interiors all being mushed-up together?)
# But -- it's also worked flawlessly for five years without my having to edit it.
# It won't work for anyone else as-is — but if it helps anyone
# to do a MetaWeblog API implementation, then cool.
require 'rubygems'
require 'xmlrpc/server.rb'
require '../wildcatRenderer.rb'
class WeblogAPIUtilities
def self.checkAuth(username, password)
# You should do better than this.
if username != "myusername" || password != "mypassword"
raise XMLRPC::FaultException.new(0, "login invalid")
end
end
def self.folderForBlogID(blogID)
# This should be in a configuration thing somewhere.
return "/Users/brent/Dropbox/Websites/#{blogID}/"
end
def self.hashForPost(post)
h = Hash.new()
h["title"] = post.atts["title"]
link = post.atts["link"]
if link != nil
h["link"] = link
# else
# h["link"] = post.atts["permalink"]
end
h["postid"] = post.webServicesID()
categories = post.atts["categoryArray"]
if categories != nil && !categories.empty?()
h["categories"] = categories
end
h["description"] = post.pageText()
h["dateCreated"] = post.atts["pubDate"]
return h
end
def self.hashForCategory(categoryName, weblog)
h = Hash.new()
h["description"] = categoryName
h["htmlUrl"] = weblog.urlForCategory(categoryName)
h["xmlUrl"] = weblog.urlForFeed(categoryName)
return h
end
def self.splitPostID(postID)
postIDArray = postID.split(":")
blogID = postIDArray[0]
blogFolder = WeblogAPIUtilities.folderForBlogID(blogID)
website = Website.new(blogFolder, false)
filePath = blogFolder + postIDArray[1]
h = Hash.new()
h["blogID"] = blogID
h["blogFolder"] = blogFolder
h["website"] = website
h["filePath"] = filePath
return h
end
def self.rawTextWithStruct(h)
s = String.new()
s += ExportUtils.attLine(h["title"], "title")
link = h["link"]
if link != nil && link != ""
s += ExportUtils.attLine(link, "link")
end
categories = h["categories"]
if categories != nil && !categories.empty?()
s += ExportUtils.attLine(categories.join(", "), "categoryArray")
end
d = Time.new()
pubDate = h["pubDate"]
if pubDate == nil then pubDate = d end
modDate = h["modDate"]
if modDate == nil then modDate = d end
s += ExportUtils.attLine("#{pubDate}", "pubDate")
s += ExportUtils.attLine("#{modDate}", "modDate")
s += h["description"]
return s
end
def self.addPost(website, h)
titleCopy = String.new(h["title"])
filename = ExportUtils.baseFilenameWithTitle(titleCopy)
filename += ".markdown"
postsFolder = website.projectSubfolder("posts/")
newPostFolder = postsFolder + ExportUtils.relativeFolderWithDate(Time.now())
filePath = newPostFolder + filename
rawText = rawTextWithStruct(h)
ExportUtils.writeFileToDisk(rawText, filePath)
return WeblogPost.new(website, filePath)
end
def self.editPost(website, filePath, h)
if !FileTest.exist?(filePath)
raise XMLRPC::FaultException.new(0, "post doesn’t exist")
end
existingWeblogPost = WeblogPost.new(website, filePath)
h["pubDate"] = existingWeblogPost.atts["pubDate"]
rawText = rawTextWithStruct(h)
ExportUtils.writeFileToDisk(rawText, filePath)
end
end
class BloggerAPI
def deletePost(appKey, postID, username, password, publish)
WeblogAPIUtilities.checkAuth(username, password)
h = WeblogAPIUtilities.splitPostID(postID)
filePath = h["filePath"]
if !FileTest.exist?(filePath) then return false end
File.delete(filePath)
return true
end
end
class MetaWeblogAPI
def getRecentPosts(blogID, username, password, numberOfPosts)
WeblogAPIUtilities.checkAuth(username, password)
blogFolder = WeblogAPIUtilities.folderForBlogID(blogID)
website = Website.new(blogFolder, false)
paths = PostFetcher.allPostsInFolderSortedByDate(blogFolder + "posts/", website.projectFolder)
if (paths.length > numberOfPosts) then paths = paths[0, numberOfPosts] end
paths.map!{|onePath| WeblogPost.new(website, onePath)}
paths.map!{|onePost| WeblogAPIUtilities.hashForPost(onePost)}
# puts("#{paths}")
return paths
end
def getCategories(blogID, username, password)
WeblogAPIUtilities.checkAuth(username, password)
blogFolder = WeblogAPIUtilities.folderForBlogID(blogID)
website = Website.new(blogFolder, false)
categories = website.weblog.categories()
categories.map!{|oneCategory| WeblogAPIUtilities.hashForCategory(oneCategory, website.weblog)}
return categories
end
def getPost(postID, username, password)
WeblogAPIUtilities.checkAuth(username, password)
h = WeblogAPIUtilities.splitPostID(postID)
weblogPost = WeblogPost.new(h["website"], h["filePath"])
return WeblogAPIUtilities.hashForPost(weblogPost)
end
def newPost(blogID, username, password, struct, publish)
WeblogAPIUtilities.checkAuth(username, password)
blogFolder = WeblogAPIUtilities.folderForBlogID(blogID)
website = Website.new(blogFolder, false)
post = WeblogAPIUtilities.addPost(website, struct)
return post.webServicesID()
end
def editPost(postID, username, password, struct, publish)
WeblogAPIUtilities.checkAuth(username, password)
h = WeblogAPIUtilities.splitPostID(postID)
WeblogAPIUtilities.editPost(h["website"], h["filePath"], struct)
return true
end
end
s = XMLRPC::CGIServer.new()
s.add_handler("blogger", BloggerAPI.new())
s.add_handler("metaWeblog", MetaWeblogAPI.new())
s.serve()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment