daily pastebin goal
4%
SHARE
TWEET

Untitled

a guest Sep 21st, 2018 70 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. require 'rubygems'
  2. require 'jekyll'
  3. require 'fileutils'
  4. require 'net/http'
  5. require 'uri'
  6. require "json"
  7.  
  8. module Jekyll
  9.   module Posterous
  10.     def self.fetch(uri_str, limit = 10)
  11.       # You should choose better exception.
  12.       raise ArgumentError, 'Stuck in a redirect loop. Please double check your email and password' if limit == 0
  13.  
  14.       response = nil
  15.       Net::HTTP.start('posterous.com') do |http|
  16.         req = Net::HTTP::Get.new(uri_str)
  17.         req.basic_auth @email, @pass
  18.         response = http.request(req)
  19.       end
  20.  
  21.       case response
  22.         when Net::HTTPSuccess     then response
  23.         when Net::HTTPRedirection then fetch(response['location'], limit - 1)
  24.         else response.error!
  25.       end
  26.     end
  27.  
  28.     def self.fetch_images(directory, imgs)
  29.       def self.fetch_one(url, limit = 10)
  30.         raise ArgumentError, 'HTTP redirect too deep' if limit == 0
  31.         response = Net::HTTP.get_response(URI.parse(url))
  32.         case response
  33.         when Net::HTTPSuccess     then response.body
  34.         when Net::HTTPRedirection then self.fetch_one(response['location'], limit - 1)
  35.         else
  36.           response.error!
  37.         end
  38.       end
  39.  
  40.       FileUtils.mkdir_p directory
  41.       urls = Array.new
  42.       imgs.each do |img|
  43.         fullurl = img["full"]["url"]
  44.         uri = URI.parse(fullurl)
  45.         imgname = uri.path.split("/")[-1]
  46.         imgdata = self.fetch_one(fullurl)
  47.         open(directory + "/" + imgname, "wb") do |file|
  48.           file.write imgdata
  49.         end
  50.         urls.push(directory + "/" + imgname)
  51.       end
  52.  
  53.       return urls
  54.     end
  55.  
  56.     def self.process(email, pass, api_token, blog = 'primary', base_path = '/')
  57.       @email, @pass, @api_token = email, pass, api_token
  58.       FileUtils.mkdir_p "_posts"
  59.  
  60.       posts = JSON.parse(self.fetch("/api/v2/users/me/sites/#{blog}/posts?api_token=#{@api_token}").body)
  61.       page = 1
  62.  
  63.       while posts.any?
  64.         posts.each do |post|
  65.           title = post["title"]
  66.           slug = title.gsub(/[^[:alnum:]]+/, '-').downcase
  67.           date = Date.parse(post["display_date"])
  68.           content = post["body_html"]
  69.           published = !post["is_private"]
  70.           basename = "%02d-%02d-%02d-%s" % [date.year, date.month, date.day, slug]
  71.           name = basename + '.html'
  72.  
  73.           # Images:
  74.           post_imgs = post["media"]["images"]
  75.           if post_imgs.any?
  76.             img_dir = "imgs/%s" % basename
  77.             img_urls = self.fetch_images(img_dir, post_imgs)
  78.  
  79.             img_urls.map! do |url|
  80.               '<li><img src="' + base_path + url + '"></li>'
  81.             end
  82.             imgcontent = "<ol>\n" + img_urls.join("\n") + "</ol>\n"
  83.  
  84.             # filter out "posterous-content", replacing with imgs:
  85.            
  86.             content = content.sub(/\<p\>\[\[posterous-content:[^\]]+\]\]\<\/p\>/, imgcontent)
  87.           end
  88.  
  89.           # Get the relevant fields as a hash, delete empty fields and convert
  90.           # to YAML for the header
  91.           data = {
  92.              'layout' => 'post',
  93.              'title' => title.to_s,
  94.              'published' => published
  95.            }.delete_if { |k,v| v.nil? || v == ''}.to_yaml
  96.  
  97.           # Write out the data and content to file
  98.           File.open("_posts/#{name}", "w") do |f|
  99.             f.puts data
  100.             f.puts "---"
  101.             f.puts content
  102.           end
  103.         end
  104.  
  105.         page += 1
  106.         posts = JSON.parse(self.fetch("/api/v2/users/me/sites/#{blog}/posts?api_token=#{@api_token}&page=#{page}").body)
  107.       end
  108.     end
  109.   end
  110. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top