-
Notifications
You must be signed in to change notification settings - Fork 3
/
kindle2anki.rb
100 lines (86 loc) · 2.39 KB
/
kindle2anki.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require('open3')
require('json')
require('net/http')
require('digest/sha1')
rwsessionid, _, t = Open3.capture3(
'security',
'find-generic-password',
'-w', # print only the password
'-l', 'readwise-rwsessionid',
)
abort('missing rwsessionid') unless t.success?
def highlights_from_api(rwsessionid)
cookie = "rwsessionid=#{rwsessionid.chomp}"
Net::HTTP.start('readwise.io', 443, use_ssl: true) do |http|
resp = http.get('/munger', 'Cookie' => cookie)
abort('GET failed') unless resp.code.to_i == 200
return JSON.parse(resp.body)
end
end
def format_highlight(highlight:, note:, author:, source:, medium:)
digest = Digest::SHA1.hexdigest(highlight)
[digest, highlight, note, author, source, medium]
.map { |f| f ? f.gsub(/[\t\n]/, ' ') : f }
.join("\t") + "\n"
end
def pdf_data_to_tsv(pdf_data)
out = ''
pdf_data.each do |item|
out << format_highlight(
highlight: item['highlight'],
note: item['note'],
author: item['author'],
source: item['source'],
medium: item['medium']
)
end
out
end
$skipped = 0
$total = 0
$last_run = File.read('last-run').to_i
$prev = $last_run
$highest = $last_run
def readwise_to_tsv(data)
out = ""
data.fetch('data').map do |book|
highlights = book.delete('highlights')
highlights.each do |highlight|
$total += 1
if highlight['created'] > $highest
$highest = highlight['created']
end
if highlight['created'] <= $last_run
$skipped += 1
next
end
out << format_highlight(
highlight: highlight['highlight'],
note: highlight['note'],
author: book['author'],
source: book['source'],
medium: book['medium'],
)
end
end
out
end
data = highlights_from_api(rwsessionid)
# pdf_data = JSON.parse(File.read('pdf-extract/pdf-highlights.json'))
# puts pdf_data_to_tsv(pdf_data)
File.write('import.tsv', readwise_to_tsv(data))
STDERR.puts("#{$total} readwise; #{$total - $skipped} new")
if $total - $skipped == 0
puts "nothing to do"
exit(0)
end
STDERR.puts 'opening anki...'
STDERR.puts 'select "Brain::Highlights" deck'
STDERR.puts 'select "Highlight" type'
system('open', '-a', 'Anki', 'import.tsv')
sleep(8)
STDERR.puts "update last-run with #{$prev} -> #{$highest}? (enter/^C)"
gets
require('fileutils')
FileUtils.mv('last-run', 'prev-last-run')
File.write('last-run', $highest.to_s + "\n")