Class: JekyllImport::Importers::Tumblr

Inherits:
JekyllImport::Importer show all
Defined in:
lib/jekyll-import/importers/tumblr.rb

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.process(options) ⇒ Object



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
# File 'lib/jekyll-import/importers/tumblr.rb', line 25

def self.process(options)
  url            = options.fetch('url')
  format         = options.fetch('format', "html")
  grab_images    = options.fetch('grab_images', false)
  add_highlights = options.fetch('add_highlights', false)
  rewrite_urls   = options.fetch('rewrite_urls', false)

  @grab_images = grab_images
  FileUtils.mkdir_p "_posts/tumblr"
  url += "/api/read/json/"
  per_page = 50
  posts = []
  # Two passes are required so that we can rewrite URLs.
  # First pass builds up an array of each post as a hash.
  begin
    current_page = (current_page || -1) + 1
    feed_url = url + "?num=#{per_page}&start=#{current_page * per_page}"
    puts "Fetching #{feed_url}"
    feed = open(feed_url)
    json = feed.readlines.join("\n")[21...-2]  # Strip Tumblr's JSONP chars.
    blog = JSON.parse(json)
    puts "Page: #{current_page + 1} - Posts: #{blog["posts"].size}"
    batch = blog["posts"].map { |post| post_to_hash(post, format) }

    # If we're rewriting, save the posts for later.  Otherwise, go ahead and
    # dump these to disk now
    if rewrite_urls
      posts += batch
    else
      batch.each {|post| write_post(post, format == "md", add_highlights)}
    end

  end until blog["posts"].size < per_page

  # Rewrite URLs, create redirects and write out out posts if necessary
  if rewrite_urls
    posts = rewrite_urls_and_redirects posts
    posts.each {|post| write_post(post, format == "md", add_highlights)}
  end
end

.require_depsObject



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/jekyll-import/importers/tumblr.rb', line 4

def self.require_deps
  JekyllImport.require_with_fallback(%w[
    rubygems
    fileutils
    open-uri
    nokogiri
    json
    uri
    time
    jekyll
  ])
end

.specify_options(c) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/jekyll-import/importers/tumblr.rb', line 17

def self.specify_options(c)
  c.option 'url', '--url URL', 'Tumblr URL'
  c.option 'format', '--format FORMAT', 'Output format (default: "html")'
  c.option 'grab_images', '--grab_images', 'Whether to grab images (default: false)'
  c.option 'add_highlights', '--add_highlights', 'Whether to add highlights (default: false)'
  c.option 'rewrite_urls', '--rewrite_urls', 'Whether to rewrite URLs (default: false)'
end