Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 82 lines (76 sloc) 1.735 kb
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
#!/usr/bin/env ruby

require "rubygems"
require "pathname"
require "hpricot"

class GooglePlusDocument
  def initialize(path)
    @path = path
  end
  def doc
    @doc ||= Hpricot(File.open(@path, 'rb', &:read))
  end
  def published
    @published ||= doc.at(".published")["title"]
  end
  def published_pretty
    @published_pretty ||= doc.at(".published").inner_text
  end
  def content
    @original_content ||= doc.at(".entry-content").to_s
  end
  def permalink
    @permalink ||= doc.at(".permalink a")["href"]
  end
  def style
    doc.at("style").to_s
  end
  def remove_doc!
    # Free memory
    @doc = nil
  end
  def attachments
    @attachments ||= (doc/".attachment").to_a.map(&:to_s)
  end
  def parse!
    permalink
    content
    attachments
    published
    published_pretty
  end
end

def process_gplus_takeout!(stream_dir, output_html)
  docs = []
  style = nil
  Pathname(stream_dir).children.each do |file|
    puts file
    doc = GooglePlusDocument.new(file)
    doc.parse!
    style ||= doc.style
    doc.remove_doc!
    docs << doc
  end
  File.open(output_html, 'wb') do |fh|
    fh.puts "<html>"
    fh.puts "<head>"
    fh.puts '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'
    fh.puts style
    fh.puts "</head>"
    fh.puts "<body>"
    docs.sort_by(&:published).reverse.each do |doc|
      fh.puts "<h3><a href='#{doc.permalink}'>#{doc.published_pretty}</a></h3>"
      fh.puts doc.content
      doc.attachments.each{|at|
        fh.puts at
      end
    end
    fh.puts "</body>"
    fh.puts "</html>"
  }
end

unless ARGV.size == 2
  STDERR.puts "Usage: #{$0} Stream/ output.html"
  exit
end

stream_dir, output_html = *ARGV
process_gplus_takeout!(stream_dir, output_html)
Something went wrong with that request. Please try again.