This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zhihu.rb
61 lines (49 loc) · 1.22 KB
/
zhihu.rb
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
# -*- coding: utf-8 -*-
require 'kindlecook'
class Zhihu < KindleCook
def initialize()
argv = ARGV.select { |arg| not arg.start_with?("-") }
if argv.empty?
$stderr.puts "You should specify an ID."
raise RuntimeError
end
@id = argv[0]
end
def root_url
# 只有图片是相对路径
"http://pic1.zhimg.com"
end
def slug
"#{self.class.to_s}_#{@id}"
end
def prepare
posts = []
i = 0
while true do
arr = fetch_json "https://zhuanlan.zhihu.com/api/columns/#{@id}/posts?limit=20&offset=#{i * 20}"
posts.concat(arr)
break if arr.length < 20
i += 1
end
articles = []
posts.reverse.each_with_index do |post, index|
title = post["title"]
file = "#{post["slug"]}.html"
save_article(file) do |f|
f.write("<h1>#{title}</h1><br>")
f.write(post["content"])
end
articles.push({:title => title, :file => file})
end
articles_to_sections(articles)
end
def document
info = fetch_json "https://zhuanlan.zhihu.com/api/columns/#{@id}"
{
"title" => info["name"],
"author" => info["creator"]["name"],
"cover" => save_image(info["avatar"]["id"])
}
end
end
Zhihu.cook("mobi")