public

Post from Google Reader to Wordpress blog

  • Download Gist
postfromgooglereader.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#!/usr/local/bin/ruby
 
# Get from your personal Google Reader public share.
# Post to your WordPress blog.
# Written by Dan Woolley on 9/8/07 for http://tzetzefly.com
 
# Command line: ruby postfromgooglereader.rb <post_type> <days_back>
# where <post_type> is 'draft', 'live', or 'test' (default is draft)
# and <days_back> is number of days back to get from Google Reader (default is 1)
 
# Stuff you should change
blogurl = ''
bloguser = ''
blogpass = ''
# Find this in your Google Reader by choosing 'Shared items' and choosing the 'feed' link
googlereader_shareurl = ''
# End Stuff to change
 
require 'Time'
require 'feed_tools' # gem install feedtools
require 'xmlrpc/client'
 
# Second argument passed is 'live' or 'draft' or 'test'.
# where 'live' posts immediately on your blog, 'draft' will sit in your draft posts,
# and 'test' just displays the output on the console.
# Default is 'draft'.
post_live = ARGV[0]
post_live = 'draft' if post_live == nil
 
# First argument passed is the days back to pull.
# Default is 1.
days_back = ARGV[1]
days_back = 1 if days_back == nil
 
_time = Time.now.utc - (86400 * days_back.to_i)
 
# Change format of title here
#title = "items from #{_time.strftime('%m.%d.%Y')}"
title = "items for #{Time.now.strftime('%m.%d.%Y')}"
 
# Change categories/tags for post here
# This must be an array of strings
categories = ['items']
 
content = ''
 
begin
puts "Getting shared Google Reader entries since " + _time.to_s + " for " + post_live + " posting."
feed = FeedTools::Feed.open(googlereader_shareurl)
feed.entries.each do |x|
if x.published > _time
content << " <li>\n"
content << " <p><a href='#{x.link}'>#{x.title}</a> - #{x.find_node("source/title/text()").to_s}</p>\n"
content << " <p>#{x.summary.gsub('[...]', '...')}</p>\n"
content << " </li>\n"
end
end
rescue => e
puts "Error: " + e.to_s
end
 
puts title
if content.length > 0
content = "<ul>\n" + content
content << "<ul>\n"
puts content
 
if post_live != 'test'
puts "Posting entry to " + blogurl
begin
# Post To Blog Via XMLRPC
server = XMLRPC::Client.new(blogurl, "/xmlrpc.php")
newPost = Hash.new
newPost['title'] = title
newPost['description'] = content
newPost['categories'] = categories
result = server.call("metaWeblog.newPost", 1, bloguser, blogpass, newPost, post_live == 'live')
rescue XMLRPC::FaultException => e
puts "XMLRPC Error: "
puts e.faultCode
puts e.faultString
rescue StandardError => e
puts "Error: " + e.to_s
end
end
else
puts "No new items since " + _time.to_s
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.