Skip to content

Instantly share code, notes, and snippets.

@willbailey
Created June 11, 2010 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willbailey/434930 to your computer and use it in GitHub Desktop.
Save willbailey/434930 to your computer and use it in GitHub Desktop.
Shareflow Export Script
#!/usr/bin/env ruby
require "rubygems"
require "highline/import"
require "flareshow"
require "erb"
require "time"
require "open-uri"
class ShareflowExporter
def initialize(domain, username, password)
@domain = domain
@username = username
@password = password
@flow_template = load_template
end
def export
Flareshow::Service.configure(@domain)
Flareshow::Service.authenticate(@username, @password)
flows = Flow.find(:limit => 300)
cleanup_output_directory
flows.each {|flow| export_flow(flow)}
end
def export_flow(flow)
puts "*"*80
puts "exporting flow: #{flow.name}"
puts "*"*80
has_more_posts = true
offset = 0
posts = []
while(has_more_posts)
retrieved_posts = Post.find(:offset => offset, :limit => 30, :flow_id => flow.id)
has_more_posts = false if !retrieved_posts || retrieved_posts.size == 0
if retrieved_posts
posts.concat(retrieved_posts)
offset += retrieved_posts.size
end
end
File.open(output_file_for_flow(flow), "w") { |f|
f << @flow_template.result(binding)
}
end
def export_post(post)
puts post.content
end
def cleanup_output_directory
FileUtils.rm_rf("#{@domain}")
end
def localized_original_url_for_file(file,key)
puts "downloading #{file.original_url}?pk=#{key}"
f = open("#{file.original_url}?pk=#{key}")
path = File.join(output_path_for_files, "#{file.id[0..5]}_#{file.file_name}")
File.open(path, "w") do |ff|
ff << f.read
end
path.gsub(/#{@domain}\//,"")
rescue
puts "*"*80
puts "error downloading file #{file.name}"
puts "*"*80
end
def localized_thumbnail_url_for_file(file, key)
download_path = "#{file.thumbnail_url}"
if download_path.match(/\?/)
download_path += "&pk=#{key}"
else
download_path += "?pk=#{key}"
end
puts "downloading #{download_path}"
f = open("#{download_path}")
FileUtils.mkdir_p("#{output_path_for_files}/thumbs")
path = File.join(output_path_for_files, "thumbs", "#{file.id}_thumb.jpg")
File.open(path, "w") do |ff|
ff << f.read
end
path.gsub(/#{@domain}\//,"")
rescue
puts "*"*80
puts "error downloading thumbnail for #{file.name}"
puts "*"*80
end
def output_path_for_files
path = "#{@domain}/files"
FileUtils.mkdir_p(path)
path
end
def output_file_for_flow(flow)
FileUtils.mkdir_p("#{@domain}")
File.join(@domain, flow.name.gsub(/["'<>\s]/,"_") + "_#{flow.id[0..5]}.html")
end
def load_template
ERB.new(DATA.read)
end
def pretty_date(date=Time.now)
date = Time.parse(date.to_s)
date.getlocal.strftime("%b %d, %Y ~ %l:%M %p")
end
end
if (__FILE__ == $0)
domain = ask("Enter the domain? ")
username = ask("Enter the username? ")
password = ask("Enter the password? ")
exporter = ShareflowExporter.new(domain, username, password)
exporter.export
end
###############################
## BEGIN TEMPLATE FOR OUTPUT ##
###############################
__END__
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title><%= flow.name %></title>
<style type="text/css" media="screen">
* {font-family:"helvetica neue", helvetica, arial, sans-serif;}
a, a:hover, a:active, a:visited {text-decoration:none;}
body{
background:#E3F4F4;
}
.wrapper{
width:800px;
margin:auto;
}
h1 {
color:#103d4a;
}
/* ================== */
/* = Shareflow Post = */
/* ================== */
.shareflow_post{
position:relative;
margin-bottom:10px;
background:#fff;
-moz-border-radius:4px;
-webkit-border-radius:4px;
padding:5px;
-moz-box-shadow:2px 2px 6px #B3C0C3;
-webkit-box-shadow:2px 2px 6px #B3C0C3;
}
.shareflow_post .shareflow_post {
-moz-box-shadow:0px 0px 0px #B3C0C3;
-webkit-box-shadow:0px 0px 0px #B3C0C3;
margin-bottom:5px;
}
.shareflow_post *, .shareflow_post {
font-size:12px;
}
.shareflow_post .shareflow_avatar{
position:absolute;
top:5px; left:5px;
-moz-border-radius:4px;
-webkit-border-radius:4px;
height:50px;
width:50px;
}
.shareflow_post .post_body{
min-height:34px;
margin-left:60px;
}
.shareflow_post .post_title{
margin-left:60px;
font-size:12px;
font-family:"Trebuchet MS";
font-style:italic;
}
.shareflow_post .post_title a,
.older_comments_link a{
color:#ab1921; font-size:12px;
}
.older_comments_link {padding-left:5px;}
.comments {
margin-left:54px;
}
.file_thumbnail_link{border:0px solid #fff;}
.file_thumbnail{margin:10px; display:block; float:left;
-moz-box-shadow:2px 2px 6px #B3C0C3;
-webkit-box-shadow:2px 2px 6px #B3C0C3;
border:0px solid #fff;
}
.file_icon{margin:10px; display:block; float:left;
border:0px solid #fff;
}
</style>
</head>
<body>
<h1><%= flow.name %></h1>
<% posts.each do |post| %>
<div class="shareflow_post">
<div class="shareflow_avatar" style="background:url(<%= post.user.avatar_url %>)">
</div>
<div class="post_title">
<%= post.user_full_name %> &mdash; <%= pretty_date(post.created_at) %>
</div>
<div class="post_body">
<%= post.content.to_s %>
<div class="files" style="clear:both">
<% post.files.each do |f| %>
<a href="<%=localized_original_url_for_file(f, post.post_key)%>" class="file_thumbnail_link" target="_blank" title="<%=f.file_name%>">
<% if f.width && f.height %>
<img src="<%=localized_thumbnail_url_for_file(f, post.post_key)%>" class="file_thumbnail" height="80" width="80" />
<% else %>
<img src="<%=localized_thumbnail_url_for_file(f, post.post_key)%>" height="80" width="80" class="file_icon" />
<% end %>
</a>
<% end %>
<div style="clear:both"></div>
</div>
</div>
<div class="comments">
<% post.comments.each do |comment| %>
<div class="shareflow_post">
<div class="shareflow_avatar" style="background:url(<%= comment.user.avatar_url %>)">
</div>
<div class="post_title">
<%= comment.user_full_name %> &mdash; <%= pretty_date(comment.created_at) %>
</div>
<div class="post_body">
<%= comment.content.to_s %>
<div class="files" style="clear:both">
<% comment.files.each do |f| %>
<a href="<%=localized_original_url_for_file(f, post.post_key)%>" class="file_thumbnail_link" target="_blank" title="<%=f.file_name%>">
<% if f.width && f.height %>
<img src="<%=localized_thumbnail_url_for_file(f, post.post_key)%>" class="file_thumbnail" height="80" width="80" />
<% else %>
<img src="<%=localized_thumbnail_url_for_file(f, post.post_key)%>" height="80" width="80" class="file_icon" />
<% end %>
</a>
<% end %>
<div style="clear:both"></div>
</div>
</div>
</div>
<% end %>
</div>
</div>
<% end %>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment