Guest User

Untitled

a guest
May 28th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require "rubygems"
  3. require "highline/import"
  4. require "flareshow"
  5. require "erb"
  6. require "time"
  7. require "open-uri"
  8.  
  9. class ShareflowExporter
  10.  
  11. def initialize(domain, username, password)
  12. @domain = domain
  13. @username = username
  14. @password = password
  15. @flow_template = load_template
  16. end
  17.  
  18. def export
  19. Flareshow::Service.configure(@domain)
  20. Flareshow::Service.authenticate(@username, @password)
  21. flows = Flow.find(:limit => 300)
  22. cleanup_output_directory
  23. flows.each {|flow| export_flow(flow)}
  24. end
  25.  
  26. def export_flow(flow)
  27. puts "*"*80
  28. puts "exporting flow: #{flow.name}"
  29. puts "*"*80
  30. has_more_posts = true
  31. offset = 0
  32. posts = []
  33. while(has_more_posts)
  34. retrieved_posts = Post.find(:offset => offset, :limit => 30, :flow_id => flow.id)
  35. has_more_posts = false if !retrieved_posts || retrieved_posts.size == 0
  36. if retrieved_posts
  37. posts.concat(retrieved_posts)
  38. offset += retrieved_posts.size
  39. end
  40. end
  41. File.open(output_file_for_flow(flow), "w") { |f|
  42. f << @flow_template.result(binding)
  43. }
  44. end
  45.  
  46. def export_post(post)
  47. puts post.content
  48. end
  49.  
  50. def cleanup_output_directory
  51. FileUtils.rm_rf("#{@domain}")
  52. end
  53.  
  54. def localized_original_url_for_file(file,key)
  55. puts "downloading #{file.original_url}?pk=#{key}"
  56. f = open("#{file.original_url}?pk=#{key}")
  57. path = File.join(output_path_for_files, "#{file.id[0..5]}_#{file.file_name}")
  58. File.open(path, "w") do |ff|
  59. ff << f.read
  60. end
  61. path.gsub(/#{@domain}\//,"")
  62. rescue
  63. puts "*"*80
  64. puts "error downloading file #{file.name}"
  65. puts "*"*80
  66. end
  67.  
  68. def localized_thumbnail_url_for_file(file, key)
  69. download_path = "#{file.thumbnail_url}"
  70. if download_path.match(/\?/)
  71. download_path += "&pk=#{key}"
  72. else
  73. download_path += "?pk=#{key}"
  74. end
  75. puts "downloading #{download_path}"
  76. f = open("#{download_path}")
  77. FileUtils.mkdir_p("#{output_path_for_files}/thumbs")
  78. path = File.join(output_path_for_files, "thumbs", "#{file.id}_thumb.jpg")
  79. File.open(path, "w") do |ff|
  80. ff << f.read
  81. end
  82. path.gsub(/#{@domain}\//,"")
  83. rescue
  84. puts "*"*80
  85. puts "error downloading thumbnail for #{file.name}"
  86. puts "*"*80
  87. end
  88.  
  89. def output_path_for_files
  90. path = "#{@domain}/files"
  91. FileUtils.mkdir_p(path)
  92. path
  93. end
  94.  
  95. def output_file_for_flow(flow)
  96. FileUtils.mkdir_p("#{@domain}")
  97. File.join(@domain, flow.name.gsub(/["'<>\s]/,"_") + "_#{flow.id[0..5]}.html")
  98. end
  99.  
  100. def load_template
  101. ERB.new(DATA.read)
  102. end
  103.  
  104. def pretty_date(date=Time.now)
  105. date = Time.parse(date.to_s)
  106. date.getlocal.strftime("%b %d, %Y ~ %l:%M %p")
  107. end
  108. end
  109.  
  110. if (__FILE__ == $0)
  111. domain = ask("Enter the domain? ")
  112. username = ask("Enter the username? ")
  113. password = ask("Enter the password? ")
  114. exporter = ShareflowExporter.new(domain, username, password)
  115. exporter.export
  116. end
  117.  
  118. ###############################
  119. ## BEGIN TEMPLATE FOR OUTPUT ##
  120. ###############################
  121. __END__
  122. <html>
  123. <head>
  124. <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  125. <title><%= flow.name %></title>
  126. <style type="text/css" media="screen">
  127. * {font-family:"helvetica neue", helvetica, arial, sans-serif;}
  128.  
  129. a, a:hover, a:active, a:visited {text-decoration:none;}
  130.  
  131. body{
  132. background:#E3F4F4;
  133. }
  134.  
  135. .wrapper{
  136. width:800px;
  137. margin:auto;
  138. }
  139.  
  140. h1 {
  141. color:#103d4a;
  142. }
  143.  
  144. /* ================== */
  145. /* = Shareflow Post = */
  146. /* ================== */
  147. .shareflow_post{
  148. position:relative;
  149. margin-bottom:10px;
  150. background:#fff;
  151. -moz-border-radius:4px;
  152. -webkit-border-radius:4px;
  153. padding:5px;
  154. -moz-box-shadow:2px 2px 6px #B3C0C3;
  155. -webkit-box-shadow:2px 2px 6px #B3C0C3;
  156. }
  157. .shareflow_post .shareflow_post {
  158. -moz-box-shadow:0px 0px 0px #B3C0C3;
  159. -webkit-box-shadow:0px 0px 0px #B3C0C3;
  160. margin-bottom:5px;
  161. }
  162. .shareflow_post *, .shareflow_post {
  163. font-size:12px;
  164. }
  165. .shareflow_post .shareflow_avatar{
  166. position:absolute;
  167. top:5px; left:5px;
  168. -moz-border-radius:4px;
  169. -webkit-border-radius:4px;
  170. height:50px;
  171. width:50px;
  172. }
  173. .shareflow_post .post_body{
  174. min-height:34px;
  175. margin-left:60px;
  176. }
  177. .shareflow_post .post_title{
  178. margin-left:60px;
  179. font-size:12px;
  180. font-family:"Trebuchet MS";
  181. font-style:italic;
  182. }
  183. .shareflow_post .post_title a,
  184. .older_comments_link a{
  185. color:#ab1921; font-size:12px;
  186. }
  187. .older_comments_link {padding-left:5px;}
  188. .comments {
  189. margin-left:54px;
  190. }
  191. .file_thumbnail_link{border:0px solid #fff;}
  192. .file_thumbnail{margin:10px; display:block; float:left;
  193. -moz-box-shadow:2px 2px 6px #B3C0C3;
  194. -webkit-box-shadow:2px 2px 6px #B3C0C3;
  195. border:0px solid #fff;
  196. }
  197. .file_icon{margin:10px; display:block; float:left;
  198. border:0px solid #fff;
  199. }
  200. </style>
  201. </head>
  202. <body>
  203. <h1><%= flow.name %></h1>
  204. <% posts.each do |post| %>
  205. <div class="shareflow_post">
  206. <div class="shareflow_avatar" style="background:url(<%= post.user.avatar_url %>)">
  207. </div>
  208. <div class="post_title">
  209. <%= post.user_full_name %> &mdash; <%= pretty_date(post.created_at) %>
  210. </div>
  211. <div class="post_body">
  212. <%= post.content.to_s %>
  213. <div class="files" style="clear:both">
  214. <% post.files.each do |f| %>
  215. <a href="<%=localized_original_url_for_file(f, post.post_key)%>" class="file_thumbnail_link" target="_blank" title="<%=f.file_name%>">
  216. <% if f.width && f.height %>
  217. <img src="<%=localized_thumbnail_url_for_file(f, post.post_key)%>" class="file_thumbnail" height="80" width="80" />
  218. <% else %>
  219. <img src="<%=localized_thumbnail_url_for_file(f, post.post_key)%>" height="80" width="80" class="file_icon" />
  220. <% end %>
  221. </a>
  222. <% end %>
  223. <div style="clear:both"></div>
  224. </div>
  225. </div>
  226. <div class="comments">
  227. <% post.comments.each do |comment| %>
  228. <div class="shareflow_post">
  229. <div class="shareflow_avatar" style="background:url(<%= comment.user.avatar_url %>)">
  230. </div>
  231. <div class="post_title">
  232. <%= comment.user_full_name %> &mdash; <%= pretty_date(comment.created_at) %>
  233. </div>
  234. <div class="post_body">
  235. <%= comment.content.to_s %>
  236. <div class="files" style="clear:both">
  237. <% comment.files.each do |f| %>
  238. <a href="<%=localized_original_url_for_file(f, post.post_key)%>" class="file_thumbnail_link" target="_blank" title="<%=f.file_name%>">
  239. <% if f.width && f.height %>
  240. <img src="<%=localized_thumbnail_url_for_file(f, post.post_key)%>" class="file_thumbnail" height="80" width="80" />
  241. <% else %>
  242. <img src="<%=localized_thumbnail_url_for_file(f, post.post_key)%>" height="80" width="80" class="file_icon" />
  243. <% end %>
  244. </a>
  245. <% end %>
  246. <div style="clear:both"></div>
  247. </div>
  248. </div>
  249. </div>
  250. <% end %>
  251. </div>
  252. </div>
  253. <% end %>
  254. </body>
  255. </html>
Add Comment
Please, Sign In to add comment