Created
July 10, 2009 05:34
-
-
Save abhiyerra/144253 to your computer and use it in GitHub Desktop.
mepisto to posterous
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
require "rubygems" | |
require "java" | |
require "jdbc/mysql" | |
require 'RedCloth' | |
require 'net/http' | |
require 'uri' | |
PosterousUsername = '' | |
PosterousPassword = '' | |
@posts = {} | |
def post_entries | |
begin | |
# Prep the connection | |
Java::com.mysql.jdbc.Driver | |
userurl = "jdbc:mysql://localhost/jasonwong" | |
connSelect = java.sql.DriverManager.get_connection(userurl, "root", "") | |
stmtSelect = connSelect.create_statement | |
# Define the query | |
selectquery = "SELECT * FROM content_versions;" | |
# Execute the query | |
q = stmtSelect.execute_query(selectquery) | |
# For each row returned do some stuff | |
while (q.next) do | |
id = q.getObject("article_id") | |
title = q.getObject("title") | |
body = q.getObject("body") | |
version = q.getObject("version") | |
date = q.getObject("created_at") | |
if !@posts[id] || @posts[id][:version] > version.to_i | |
@posts[id] = {} unless @posts[id] | |
@posts[id][:title] = title | |
@posts[id][:body] = body | |
@posts[id][:version] = version.to_i | |
@posts[id][:date] = date | |
@posts[id][:tags] = [] | |
q2q = "select name from tags inner join taggings where tags.id = taggings.tag_id and taggings.taggable_id = #{id};" | |
stmtSelect2 = connSelect.create_statement | |
q2 = stmtSelect2.execute_query(q2q) | |
while(q2.next) do | |
@posts[id][:tags] << q2.getObject("name") | |
end | |
stmtSelect2.close | |
end | |
end | |
@posts.keys.each do |key| | |
puts @posts[key][:title] | |
post_to_site key | |
sleep 1 | |
end | |
end | |
# Close off the connection | |
stmtSelect.close | |
connSelect.close | |
end | |
def post_to_site key | |
site_id = '237669' | |
#3: Detailed control | |
url = URI.parse('http://posterous.com/api/newpost') | |
req = Net::HTTP::Post.new(url.path) | |
req.basic_auth PosterousUsername, PosterousPassword | |
req.set_form_data({'title' => @posts[key][:title], | |
'body' => @posts[key][:body], | |
'site_id' => site_id, | |
'date' => @posts[key][:date], | |
'tags' => @posts[key][:tags].join(',')}) | |
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) } | |
case res | |
when Net::HTTPSuccess, Net::HTTPRedirection | |
# OK | |
else | |
puts res.error! | |
end | |
end | |
post_entries |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment